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 { 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.")
}

View File

@ -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: ${(<any>left).value};`)
var top = absoluteLayoutModule.AbsoluteLayout.getTop(child);

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 {
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):
(<any>componentModule.component).exports = context;
}
if (!componentModule) {
throw new Error("Failed to load component from module: " + filePathRelativeToApp + " or file: " + fileName);
}
return componentModule;
}