Skip to content

Commit b2de69d

Browse files
chore: fix issue where nTestBedBeforeEach overwrote its own imports
- Fixes the DetachedLoader tests, and makes them MUCH simpler. - When you configure the test bed module, you need to specify a full list of imports, because they completely overwrite the imports array that is used. - That's yet another reason to use the provided helper functions, they merge in the common {N} imports for you. - ... and some lint cleanup
1 parent 1a29f13 commit b2de69d

File tree

4 files changed

+24
-52
lines changed

4 files changed

+24
-52
lines changed

Diff for: nativescript-angular/testing/src/util.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ export function nTestBedBeforeEach(
8080
// There's got to be a better way... (o_O)
8181
TestBed.resetTestEnvironment();
8282
@NgModule({
83-
imports: [
84-
NativeScriptModule, NativeScriptTestingModule, CommonModule
85-
],
86-
providers: NATIVESCRIPT_TESTING_PROVIDERS,
87-
exports: entryComponents,
8883
declarations: entryComponents,
84+
exports: entryComponents,
8985
entryComponents: entryComponents
9086
})
9187
class EntryComponentsTestModule {
@@ -95,9 +91,12 @@ export function nTestBedBeforeEach(
9591
platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS)
9692
);
9793
TestBed.configureTestingModule({
98-
declarations: [...components],
99-
imports: [...imports],
100-
providers: [...providers],
94+
declarations: components,
95+
imports: [
96+
NativeScriptModule, NativeScriptTestingModule, CommonModule,
97+
...imports
98+
],
99+
providers: [...providers, ...NATIVESCRIPT_TESTING_PROVIDERS],
101100
});
102101
}
103102
TestBed.compileComponents()

Diff for: tests/app/tests/detached-loader-tests.ts

+14-38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// make sure you import mocha-config before @angular/core
2-
import {ChangeDetectionStrategy, Component, ViewChild} from '@angular/core';
3-
import {DetachedLoader} from 'nativescript-angular/common/detached-loader';
4-
import {nTestBedAfterEach, nTestBedBeforeEach, nTestBedRender} from 'nativescript-angular/testing';
2+
import { ChangeDetectionStrategy, Component, ViewChild } from "@angular/core";
3+
import { DetachedLoader } from "nativescript-angular";
4+
import { nTestBedAfterEach, nTestBedBeforeEach, nTestBedRender } from "nativescript-angular/testing";
55

66
@Component({
77
template: `<StackLayout><Label text="COMPONENT"></Label></StackLayout>`
@@ -11,18 +11,6 @@ class TestComponent { }
1111

1212
class LoaderComponentBase {
1313
@ViewChild(DetachedLoader) public loader: DetachedLoader;
14-
15-
public ready: Promise<LoaderComponentBase>;
16-
private resolve;
17-
constructor() {
18-
this.ready = new Promise((reslove, reject) => {
19-
this.resolve = reslove;
20-
});
21-
}
22-
ngAfterViewInit() {
23-
console.log("!!! ngAfterViewInit -> loader: " + this.loader);
24-
this.resolve(this);
25-
}
2614
}
2715

2816
@Component({
@@ -46,35 +34,23 @@ export class LoaderComponent extends LoaderComponentBase {}
4634
})
4735
export class LoaderComponentOnPush extends LoaderComponentBase { }
4836

49-
// TODO(JD): These tests should work, but demonstrate an annoying bug I've noticed sometimes with @ViewChild
50-
//
51-
// The @ViewChild(DetachedLoader) on LoaderComponentBase fails. Can anyone shed some light on why?
52-
xdescribe("DetachedLoader", () => {
37+
describe("DetachedLoader", () => {
5338

5439
beforeEach(nTestBedBeforeEach([LoaderComponent, LoaderComponentOnPush], [], [], [TestComponent]));
5540
afterEach(nTestBedAfterEach());
5641

57-
it("creates component", (done) => {
58-
let component: LoaderComponent;
59-
60-
nTestBedRender(LoaderComponent)
61-
.then((fixture) => {
62-
component = fixture.componentRef.instance;
63-
return component.ready;
64-
})
65-
.then(() => component.loader.loadComponent(TestComponent))
66-
.then(() => done())
67-
.catch(done);
42+
it("creates component", () => {
43+
return nTestBedRender(LoaderComponent).then((fixture) => {
44+
const component: LoaderComponent = fixture.componentRef.instance;
45+
return component.loader.loadComponent(TestComponent);
46+
});
6847
});
6948

7049

71-
it("creates component when ChangeDetectionStrategy is OnPush", (done) => {
72-
nTestBedRender(LoaderComponentOnPush)
73-
.then((fixture) => {
74-
const component: LoaderComponent = fixture.componentRef.instance;
75-
return component.loader.loadComponent(TestComponent);
76-
})
77-
.then(() => done())
78-
.catch(done);
50+
it("creates component when ChangeDetectionStrategy is OnPush", () => {
51+
return nTestBedRender(LoaderComponentOnPush).then((fixture) => {
52+
const component: LoaderComponentOnPush = fixture.componentRef.instance;
53+
return component.loader.loadComponent(TestComponent);
54+
});
7955
});
8056
});

Diff for: tests/app/tests/modal-dialog.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export class FailComponent {
3939
selector: "sucess-comp",
4040
providers: [ModalDialogService],
4141
template: `
42-
<GridLayout margin="20">
43-
<Label text="Modal dialogs"></Label>
44-
</GridLayout>`
42+
<GridLayout margin="20">
43+
<Label text="Modal dialogs"></Label>
44+
</GridLayout>`
4545
})
4646
export class SuccessComponent {
4747
constructor(public service: ModalDialogService, public vcRef: ViewContainerRef) {

Diff for: tests/app/tests/platform-filter-components.ts

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ export class PlatformSpecificAttributeComponent {
3737
}
3838

3939
describe("Platform filter directives", () => {
40-
// TODO: Something is different in dumpView. I suspect it has to do with the fact that the old
41-
// test app class rendered directly into a view container and returned the exact component, whereas
42-
// testbed appears to return a reference to the root component? Needs investigation.
4340
describe("on IOS device", () => {
4441
beforeEach(nTestBedBeforeEach(
4542
[PlatformSpecificAttributeComponent, AndroidSpecificComponent, IosSpecificComponent],

0 commit comments

Comments
 (0)