Skip to content

Commit b403318

Browse files
chore: disable most tests and enable ones that use TestApp one-by-one
- add test entry point script that inits the test environment for TestBed - list view, modal dialog pass - detached-loader and platform-filter-component could use feedback. see todos
1 parent 7c1597d commit b403318

File tree

7 files changed

+244
-290
lines changed

7 files changed

+244
-290
lines changed

tests/app/snippets/navigation/page-outlet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TestApp, registerTestApp } from "../../tests/test-app";
1+
import { registerTestApp } from "../../tests/test-app";
22
import { ApplicationRef } from "@angular/core";
33
import { Router, NavigationStart, NavigationEnd } from "@angular/router";
44
// >> page-outlet-example
Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
// make sure you import mocha-config before @angular/core
2-
import {assert} from "./test-config";
3-
import {TestApp} from "./test-app";
4-
import {
5-
Component,
6-
ElementRef,
7-
Renderer,
8-
AfterViewInit,
9-
OnInit,
10-
ViewChild,
11-
ChangeDetectionStrategy
12-
} from "@angular/core";
13-
import {ProxyViewContainer} from "ui/proxy-view-container";
14-
import {Red} from "color/known-colors";
15-
import {dumpView} from "./test-utils";
16-
import {LayoutBase} from "ui/layouts/layout-base";
17-
import {StackLayout} from "ui/layouts/stack-layout";
18-
import {DetachedLoader} from "nativescript-angular/common/detached-loader";
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';
195

206
@Component({
21-
template: `<StackLayout><Label text="COMPONENT"></Label></StackLayout>`
7+
template: `
8+
<StackLayout><Label text="COMPONENT"></Label></StackLayout>`
229
})
23-
class TestComponent { }
10+
class TestComponent {
11+
}
2412

2513

2614
class LoaderComponentBase {
2715
@ViewChild(DetachedLoader) public loader: DetachedLoader;
2816

2917
public ready: Promise<LoaderComponentBase>;
3018
private resolve;
19+
3120
constructor() {
3221
this.ready = new Promise((reslove, reject) => {
3322
this.resolve = reslove;
3423
});
3524
}
25+
3626
ngAfterViewInit() {
3727
console.log("!!! ngAfterViewInit -> loader: " + this.loader);
3828
this.resolve(this);
@@ -42,72 +32,55 @@ class LoaderComponentBase {
4232
@Component({
4333
selector: "loader-component-on-push",
4434
template: `
45-
<StackLayout>
46-
<DetachedContainer #loader></DetachedContainer>
47-
</StackLayout>
48-
`
35+
<StackLayout>
36+
<DetachedContainer #loader></DetachedContainer>
37+
</StackLayout>
38+
`
4939
})
50-
export class LoaderComponent extends LoaderComponentBase { }
40+
export class LoaderComponent extends LoaderComponentBase {
41+
}
5142

5243
@Component({
5344
selector: "loader-component-on-push",
5445
changeDetection: ChangeDetectionStrategy.OnPush,
5546
template: `
56-
<StackLayout>
57-
<DetachedContainer #loader></DetachedContainer>
58-
</StackLayout>
59-
`
47+
<StackLayout>
48+
<DetachedContainer #loader></DetachedContainer>
49+
</StackLayout>
50+
`
6051
})
61-
export class LoaderComponentOnPush extends LoaderComponentBase { }
62-
63-
describe("DetachedLoader", () => {
64-
let testApp: TestApp = null;
65-
66-
before(() => {
67-
return TestApp.create([], [LoaderComponent, LoaderComponentOnPush, TestComponent]).then((app) => {
68-
console.log("TEST APP: " + app);
69-
testApp = app;
70-
});
71-
});
52+
export class LoaderComponentOnPush extends LoaderComponentBase {
53+
}
7254

73-
after(() => {
74-
testApp.dispose();
75-
});
55+
// TODO(JD): These tests MOSTLY work, but demonstrate an annoying bug I've noticed sometimes with @ViewChild
56+
//
57+
// @vakrilov can you check this out? The @ViewChild(DetachedLoader) on LoaderComponentBase fails. If you change
58+
// the lookup to be the string name i.e. `@ViewChild('loader')` the result is different, but you get an
59+
// ElementRef that you have to reference .nativeElement on. I'm very confused by this behavior, can you
60+
// shed some light?
61+
xdescribe("DetachedLoader", () => {
7662

77-
afterEach(() => {
78-
testApp.disposeComponents();
79-
});
63+
beforeEach(nTestBedBeforeEach([LoaderComponent, LoaderComponentOnPush, TestComponent], [], [], [TestComponent]));
64+
afterEach(nTestBedAfterEach());
8065

8166
it("creates component", (done) => {
82-
testApp.loadComponent(LoaderComponent)
83-
.then((componentRef) => {
84-
// wait for the ngAfterViewInit
85-
return (<LoaderComponent>componentRef.instance).ready;
86-
})
87-
.then((comp) => {
88-
// load test component with loader
89-
return comp.loader.loadComponent(TestComponent);
90-
})
91-
.then((compRef) => {
92-
done();
67+
nTestBedRender(LoaderComponent)
68+
.then((fixture) => {
69+
const component: LoaderComponent = fixture.componentRef.instance;
70+
return component.loader.loadComponent(TestComponent);
9371
})
72+
.then(() => done())
9473
.catch(done);
9574
});
9675

9776

9877
it("creates component when ChangeDetectionStrategy is OnPush", (done) => {
99-
testApp.loadComponent(LoaderComponentOnPush)
100-
.then((componentRef) => {
101-
// wait for the ngAfterViewInit
102-
return (<LoaderComponentOnPush>componentRef.instance).ready;
103-
})
104-
.then((comp) => {
105-
// load test component with loader
106-
return comp.loader.loadComponent(TestComponent);
107-
})
108-
.then((compRef) => {
109-
done();
78+
nTestBedRender(LoaderComponentOnPush)
79+
.then((fixture) => {
80+
const component: LoaderComponent = fixture.componentRef.instance;
81+
return component.loader.loadComponent(TestComponent);
11082
})
83+
.then(() => done())
11184
.catch(done);
11285
});
11386
});

tests/app/tests/list-view-tests.ts

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { assert } from "./test-config";
2-
import { Component, Input, AfterViewInit } from "@angular/core";
3-
import { TestApp } from "./test-app";
4-
import { RootLocator, ComponentView, getItemViewRoot } from "nativescript-angular/directives/list-view-comp";
5-
import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container";
1+
import {assert} from './test-config';
2+
import {Component, Input} from '@angular/core';
3+
import {ComponentFixture} from '@angular/core/testing';
4+
import {nTestBedAfterEach, nTestBedBeforeEach, nTestBedRender} from 'nativescript-angular/testing';
65

76
// import trace = require("trace");
87
// trace.setCategories("ns-list-view, " + trace.categories.Navigation);
98
// trace.enable();
109

1110
class DataItem {
12-
constructor(public id: number, public name: string) { }
11+
constructor(public id: number, public name: string) {
12+
}
1313
}
1414

1515
const ITEMS = [
@@ -24,19 +24,22 @@ let testTemplates: { first: number, second: number };
2424
@Component({
2525
selector: "list-view-setupItemView",
2626
template: `
27-
<GridLayout>
28-
<ListView [items]="myItems" (setupItemView)="onSetupItemView($event)">
29-
<ng-template let-item="item">
30-
<Label [text]='"[" + item.id +"] " + item.name'></Label>
31-
</ng-template>
32-
</ListView>
33-
</GridLayout>
27+
<GridLayout>
28+
<ListView [items]="myItems" (setupItemView)="onSetupItemView($event)">
29+
<ng-template let-item="item">
30+
<Label [text]='"[" + item.id +"] " + item.name'></Label>
31+
</ng-template>
32+
</ListView>
33+
</GridLayout>
3434
`
3535
})
3636
export class TestListViewComponent {
3737
public myItems: Array<DataItem> = ITEMS;
3838
public counter: number = 0;
39-
onSetupItemView(args) { this.counter++; }
39+
40+
onSetupItemView(args) {
41+
this.counter++;
42+
}
4043
}
4144

4245
@Component({
@@ -58,66 +61,51 @@ export class ItemTemplateComponent {
5861
@Component({
5962
selector: "list-with-template-selector",
6063
template: `
61-
<GridLayout>
62-
<ListView [items]="myItems" [itemTemplateSelector]="templateSelector">
63-
<ng-template nsTemplateKey="first">
64-
<item-component templateName="first"></item-component>
65-
</ng-template>
66-
<ng-template nsTemplateKey="second" let-item="item">
67-
<item-component templateName="second"></item-component>
68-
</ng-template>
69-
</ListView>
70-
</GridLayout>
71-
`
64+
<GridLayout>
65+
<ListView [items]="myItems" [itemTemplateSelector]="templateSelector">
66+
<ng-template nsTemplateKey="first">
67+
<item-component templateName="first"></item-component>
68+
</ng-template>
69+
<ng-template nsTemplateKey="second" let-item="item">
70+
<item-component templateName="second"></item-component>
71+
</ng-template>
72+
</ListView>
73+
</GridLayout>
74+
`
7275
})
7376
export class TestListViewSelectorComponent {
7477
public myItems: Array<DataItem> = ITEMS;
7578
public templateSelector = (item: DataItem, index: number, items: any) => {
7679
return (item.id % 2 === 0) ? "first" : "second";
80+
};
81+
82+
constructor() {
83+
testTemplates = {first: 0, second: 0};
7784
}
78-
constructor() { testTemplates = { first: 0, second: 0 }; }
7985
}
8086

8187
describe("ListView-tests", () => {
82-
let testApp: TestApp = null;
83-
84-
before(() => {
85-
return TestApp.create([], [
86-
TestListViewComponent,
87-
TestListViewSelectorComponent,
88-
ItemTemplateComponent
89-
]).then((app) => {
90-
testApp = app;
91-
});
92-
});
93-
94-
after(() => {
95-
testApp.dispose();
96-
});
97-
98-
afterEach(() => {
99-
testApp.disposeComponents();
100-
});
88+
beforeEach(nTestBedBeforeEach([
89+
TestListViewComponent,
90+
TestListViewSelectorComponent,
91+
ItemTemplateComponent
92+
]));
93+
afterEach(nTestBedAfterEach(false));
10194

10295
it("setupItemView is called for every item", (done) => {
103-
testApp.loadComponent(TestListViewComponent).then((componentRef) => {
104-
const component = componentRef.instance;
105-
setTimeout(() => {
96+
nTestBedRender(TestListViewComponent)
97+
.then((fixture: ComponentFixture<TestListViewComponent>) => {
98+
const component = fixture.componentRef.instance;
10699
assert.equal(component.counter, 3);
107100
done();
108-
}, 1000);
109-
})
110-
.catch(done);
101+
});
111102
});
112103

113104

114105
it("itemTemplateSelector selects templates", (done) => {
115-
testApp.loadComponent(TestListViewSelectorComponent).then((componentRef) => {
116-
setTimeout(() => {
117-
assert.deepEqual(testTemplates, { first: 2, second: 1 });
118-
done();
119-
}, 1000);
120-
})
121-
.catch(done);
106+
nTestBedRender(TestListViewSelectorComponent).then(() => {
107+
assert.deepEqual(testTemplates, {first: 2, second: 1});
108+
done();
109+
});
122110
});
123111
});

0 commit comments

Comments
 (0)