FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: throw if failed to load component (#7186) · NativeScript/NativeScript@b7abb3d · GitHub

Commit b7abb3d

Browse files
authored
fix: throw if failed to load component (#7186)
1 parent 7d3f0d9 commit b7abb3d

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import { path } from "tns-core-modules/file-system";
22
import { loadPage } from "tns-core-modules/ui/builder";
3-
import { assertEqual, assertNull } from "../../TKUnit";
3+
import { assertEqual, assertNull, assertThrows } from "../../TKUnit";
44

55
const COMPONENT_MODULE = "component-module";
6+
const MISSING_MODULE = "missing-module";
67
const LABEL = "label";
78

8-
function getViewComponent() {
9-
const moduleNamePath = path.join(__dirname, COMPONENT_MODULE);
10-
const fileName = path.join(__dirname, `${COMPONENT_MODULE}.xml`);
9+
function getViewComponent(componentModule: string) {
10+
const moduleNamePath = path.join(__dirname, componentModule);
11+
const fileName = path.join(__dirname, `${componentModule}.xml`);
1112
const view = loadPage(moduleNamePath, fileName);
1213
return view;
1314
}
1415

1516
export function test_view_is_module_root_component() {
16-
const view = getViewComponent();
17+
const view = getViewComponent(COMPONENT_MODULE);
1718
const actualModule = view._moduleName;
1819
assertEqual(actualModule, COMPONENT_MODULE, `View<${view}> is NOT root component of module <${COMPONENT_MODULE}>.`);
1920
}
2021

2122
export function test_view_is_NOT_module_root_component() {
22-
const view = getViewComponent();
23+
const view = getViewComponent(COMPONENT_MODULE);
2324
const nestedView = view.getViewById(`${LABEL}`);
2425
const undefinedModule = nestedView._moduleName;
2526
assertNull(undefinedModule, `View<${nestedView}> should NOT be a root component of a module.`);
2627
}
28+
29+
export function test_load_component_from_missing_module_throws() {
30+
assertThrows(() => getViewComponent(MISSING_MODULE),
31+
"Loading component from a missing module SHOULD throw an error.")
32+
}

‎tests/app/xml-declaration/xml-declaration-tests.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export function test_parse_IsDefined() {
3535
TKUnit.assertTrue(types.isFunction(builder.parse), "ui/builder should have parse method!");
3636
};
3737

38-
export function test_load_ShouldNotCrashWithInvalidFileName() {
39-
var v = builder.load(fs.path.join(__dirname, "mainPage1.xml"));
40-
TKUnit.assertTrue(types.isUndefined(v), "Expected result: undefined; Actual result: " + v + ";");
38+
export function test_load_ShouldThrowWithInvalidFileName() {
39+
let fileName = fs.path.join(__dirname, "invalid-page.xml");
40+
TKUnit.assertThrows(() => builder.load(fileName),
41+
"Loading component from a missing module SHOULD throw an error.");
4142
};
4243

4344
export function test_load_ShouldNotCrashWithoutExports() {
@@ -300,7 +301,7 @@ export function test_parse_ShouldSetCanvasAttachedProperties() {
300301
var child = absLayout.getChildAt(0);
301302

302303
var left = absoluteLayoutModule.AbsoluteLayout.getLeft(child);
303-
304+
304305
TKUnit.assert(Length.equals(left, Length.parse("1")), `Expected result for canvas left: 1; Actual result: ${(<any>left).value};`)
305306

306307
var top = absoluteLayoutModule.AbsoluteLayout.getTop(child);

‎tns-core-modules/ui/builder/builder.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View {
6060
export function loadPage(moduleNamePath: string, fileName: string, context?: any): View {
6161
const componentModule = loadInternal(fileName, context, moduleNamePath);
6262
const componentView = componentModule && componentModule.component;
63-
markAsModuleRoot(componentView, moduleNamePath);
63+
if (componentView && moduleNamePath) {
64+
markAsModuleRoot(componentView, moduleNamePath);
65+
}
6466
return componentView;
6567
}
6668

@@ -164,6 +166,10 @@ function loadInternal(fileName: string, context?: any, moduleNamePath?: string):
164166
(<any>componentModule.component).exports = context;
165167
}
166168

169+
if (!componentModule) {
170+
throw new Error("Failed to load component from module: " + filePathRelativeToApp + " or file: " + fileName);
171+
}
172+
167173
return componentModule;
168174
}
169175

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL