From b7abb3dd64ac4ea433c91197b1ec949c63bcd669 Mon Sep 17 00:00:00 2001 From: Vasil Chimev Date: Wed, 1 May 2019 14:16:18 +0300 Subject: [PATCH] fix: throw if failed to load component (#7186) --- tests/app/ui/builder/builder-tests.ts | 18 ++++++++++++------ .../xml-declaration/xml-declaration-tests.ts | 9 +++++---- tns-core-modules/ui/builder/builder.ts | 8 +++++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/tests/app/ui/builder/builder-tests.ts b/tests/app/ui/builder/builder-tests.ts index bfb65a2d0..f9cb5885d 100644 --- a/tests/app/ui/builder/builder-tests.ts +++ b/tests/app/ui/builder/builder-tests.ts @@ -1,26 +1,32 @@ import { path } from "tns-core-modules/file-system"; import { loadPage } from "tns-core-modules/ui/builder"; -import { assertEqual, assertNull } from "../../TKUnit"; +import { assertEqual, assertNull, assertThrows } from "../../TKUnit"; const COMPONENT_MODULE = "component-module"; +const MISSING_MODULE = "missing-module"; const LABEL = "label"; -function getViewComponent() { - const moduleNamePath = path.join(__dirname, COMPONENT_MODULE); - const fileName = path.join(__dirname, `${COMPONENT_MODULE}.xml`); +function getViewComponent(componentModule: string) { + const moduleNamePath = path.join(__dirname, componentModule); + const fileName = path.join(__dirname, `${componentModule}.xml`); const view = loadPage(moduleNamePath, fileName); return view; } export function test_view_is_module_root_component() { - const view = getViewComponent(); + const view = getViewComponent(COMPONENT_MODULE); const actualModule = view._moduleName; assertEqual(actualModule, COMPONENT_MODULE, `View<${view}> is NOT root component of module <${COMPONENT_MODULE}>.`); } export function test_view_is_NOT_module_root_component() { - const view = getViewComponent(); + const view = getViewComponent(COMPONENT_MODULE); const nestedView = view.getViewById(`${LABEL}`); const undefinedModule = nestedView._moduleName; assertNull(undefinedModule, `View<${nestedView}> should NOT be a root component of a module.`); } + +export function test_load_component_from_missing_module_throws() { + assertThrows(() => getViewComponent(MISSING_MODULE), + "Loading component from a missing module SHOULD throw an error.") +} diff --git a/tests/app/xml-declaration/xml-declaration-tests.ts b/tests/app/xml-declaration/xml-declaration-tests.ts index 65ff4208f..a6abf5415 100644 --- a/tests/app/xml-declaration/xml-declaration-tests.ts +++ b/tests/app/xml-declaration/xml-declaration-tests.ts @@ -35,9 +35,10 @@ export function test_parse_IsDefined() { TKUnit.assertTrue(types.isFunction(builder.parse), "ui/builder should have parse method!"); }; -export function test_load_ShouldNotCrashWithInvalidFileName() { - var v = builder.load(fs.path.join(__dirname, "mainPage1.xml")); - TKUnit.assertTrue(types.isUndefined(v), "Expected result: undefined; Actual result: " + v + ";"); +export function test_load_ShouldThrowWithInvalidFileName() { + let fileName = fs.path.join(__dirname, "invalid-page.xml"); + TKUnit.assertThrows(() => builder.load(fileName), + "Loading component from a missing module SHOULD throw an error."); }; export function test_load_ShouldNotCrashWithoutExports() { @@ -300,7 +301,7 @@ export function test_parse_ShouldSetCanvasAttachedProperties() { var child = absLayout.getChildAt(0); var left = absoluteLayoutModule.AbsoluteLayout.getLeft(child); - + TKUnit.assert(Length.equals(left, Length.parse("1")), `Expected result for canvas left: 1; Actual result: ${(left).value};`) var top = absoluteLayoutModule.AbsoluteLayout.getTop(child); diff --git a/tns-core-modules/ui/builder/builder.ts b/tns-core-modules/ui/builder/builder.ts index d3dd8e099..454c33971 100644 --- a/tns-core-modules/ui/builder/builder.ts +++ b/tns-core-modules/ui/builder/builder.ts @@ -60,7 +60,9 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View { export function loadPage(moduleNamePath: string, fileName: string, context?: any): View { const componentModule = loadInternal(fileName, context, moduleNamePath); const componentView = componentModule && componentModule.component; - markAsModuleRoot(componentView, moduleNamePath); + if (componentView && moduleNamePath) { + markAsModuleRoot(componentView, moduleNamePath); + } return componentView; } @@ -164,6 +166,10 @@ function loadInternal(fileName: string, context?: any, moduleNamePath?: string): (componentModule.component).exports = context; } + if (!componentModule) { + throw new Error("Failed to load component from module: " + filePathRelativeToApp + " or file: " + fileName); + } + return componentModule; }