fix: throw if failed to load component (#7186)

This commit is contained in:
Vasil Chimev
2019-05-01 14:16:18 +03:00
committed by GitHub
parent 7d3f0d9e96
commit b7abb3dd64
3 changed files with 24 additions and 11 deletions

View File

@ -1,26 +1,32 @@
import { path } from "tns-core-modules/file-system"; import { path } from "tns-core-modules/file-system";
import { loadPage } from "tns-core-modules/ui/builder"; 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 COMPONENT_MODULE = "component-module";
const MISSING_MODULE = "missing-module";
const LABEL = "label"; const LABEL = "label";
function getViewComponent() { function getViewComponent(componentModule: string) {
const moduleNamePath = path.join(__dirname, COMPONENT_MODULE); const moduleNamePath = path.join(__dirname, componentModule);
const fileName = path.join(__dirname, `${COMPONENT_MODULE}.xml`); const fileName = path.join(__dirname, `${componentModule}.xml`);
const view = loadPage(moduleNamePath, fileName); const view = loadPage(moduleNamePath, fileName);
return view; return view;
} }
export function test_view_is_module_root_component() { export function test_view_is_module_root_component() {
const view = getViewComponent(); const view = getViewComponent(COMPONENT_MODULE);
const actualModule = view._moduleName; const actualModule = view._moduleName;
assertEqual(actualModule, COMPONENT_MODULE, `View<${view}> is NOT root component of module <${COMPONENT_MODULE}>.`); assertEqual(actualModule, COMPONENT_MODULE, `View<${view}> is NOT root component of module <${COMPONENT_MODULE}>.`);
} }
export function test_view_is_NOT_module_root_component() { export function test_view_is_NOT_module_root_component() {
const view = getViewComponent(); const view = getViewComponent(COMPONENT_MODULE);
const nestedView = view.getViewById(`${LABEL}`); const nestedView = view.getViewById(`${LABEL}`);
const undefinedModule = nestedView._moduleName; const undefinedModule = nestedView._moduleName;
assertNull(undefinedModule, `View<${nestedView}> should NOT be a root component of a module.`); 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.")
}

View File

@ -35,9 +35,10 @@ export function test_parse_IsDefined() {
TKUnit.assertTrue(types.isFunction(builder.parse), "ui/builder should have parse method!"); TKUnit.assertTrue(types.isFunction(builder.parse), "ui/builder should have parse method!");
}; };
export function test_load_ShouldNotCrashWithInvalidFileName() { export function test_load_ShouldThrowWithInvalidFileName() {
var v = builder.load(fs.path.join(__dirname, "mainPage1.xml")); let fileName = fs.path.join(__dirname, "invalid-page.xml");
TKUnit.assertTrue(types.isUndefined(v), "Expected result: undefined; Actual result: " + v + ";"); TKUnit.assertThrows(() => builder.load(fileName),
"Loading component from a missing module SHOULD throw an error.");
}; };
export function test_load_ShouldNotCrashWithoutExports() { export function test_load_ShouldNotCrashWithoutExports() {

View File

@ -60,7 +60,9 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View {
export function loadPage(moduleNamePath: string, fileName: string, context?: any): View { export function loadPage(moduleNamePath: string, fileName: string, context?: any): View {
const componentModule = loadInternal(fileName, context, moduleNamePath); const componentModule = loadInternal(fileName, context, moduleNamePath);
const componentView = componentModule && componentModule.component; const componentView = componentModule && componentModule.component;
markAsModuleRoot(componentView, moduleNamePath); if (componentView && moduleNamePath) {
markAsModuleRoot(componentView, moduleNamePath);
}
return componentView; return componentView;
} }
@ -164,6 +166,10 @@ function loadInternal(fileName: string, context?: any, moduleNamePath?: string):
(<any>componentModule.component).exports = context; (<any>componentModule.component).exports = context;
} }
if (!componentModule) {
throw new Error("Failed to load component from module: " + filePathRelativeToApp + " or file: " + fileName);
}
return componentModule; return componentModule;
} }