Skip to content

Commit 20d45ee

Browse files
chore: replace the remaining TestApp usages in test suite
- xdescribe the failing tests. - I think the remaining problems boil down to `dumpView` indicating the ComponentFixture comes back with the root components, and `@ViewChild` not finding DetachedLoader by its class. - remove some duplication in testing utilities
1 parent b1affca commit 20d45ee

File tree

8 files changed

+189
-205
lines changed

8 files changed

+189
-205
lines changed

nativescript-angular/testing/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import {TestComponentRenderer} from '@angular/core/testing';
33
import {NativeScriptTestComponentRenderer} from './src/nativescript_test_component_renderer';
44
import {COMMON_PROVIDERS} from '../platform-common';
55
import {APP_ROOT_VIEW} from '../platform-providers';
6-
import {testingRootView} from './src/util';
6+
import {View} from 'tns-core-modules/ui/core/view';
7+
import {topmost} from 'tns-core-modules/ui/frame';
78
export * from './src/util';
89

10+
/**
11+
* Get a reference to the root application view.
12+
*/
13+
export function testingRootView(): View {
14+
return topmost().currentPage.content;
15+
}
16+
917
/**
1018
* Providers array is exported for cases where a custom module has to be constructed
1119
* to test a particular piece of code. This can happen, for example, if you are trying

nativescript-angular/testing/src/util.ts

+28-47
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {NgModule, Type} from '@angular/core';
3-
import {topmost} from 'tns-core-modules/ui/frame';
4-
import {View} from 'tns-core-modules/ui/core/view';
53
import {NativeScriptModule} from '../../nativescript.module';
64
import {platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
75
import {NS_COMPILER_PROVIDERS} from '../../platform';
86
import {NATIVESCRIPT_TESTING_PROVIDERS, NativeScriptTestingModule} from '../index';
97
import {CommonModule} from '@angular/common';
10-
/**
11-
* Get a reference to the root application view.
12-
*/
13-
export function testingRootView(): View {
14-
return topmost().currentPage.layoutView;
15-
}
168

179
/**
1810
* Return a promise that resolves after (durationMs) milliseconds
@@ -64,47 +56,39 @@ export function nTestBedBeforeEach(
6456
entryComponents: any[] = []) {
6557
return (done) => {
6658
// If there are no entry components we can take the simple path.
67-
const hasEntryComponents: boolean = entryComponents.length > 0;
68-
if (!hasEntryComponents) {
59+
if (entryComponents.length === 0) {
6960
TestBed.configureTestingModule({
7061
declarations: [...components],
7162
providers: [...providers],
7263
imports: [NativeScriptModule, ...imports]
7364
});
74-
TestBed.compileComponents()
75-
.then(() => done())
76-
.catch((e) => {
77-
console.log(`Failed to instantiate test component with error: ${e}`);
78-
console.log(e.stack);
79-
done(e);
80-
});
81-
return;
8265
}
83-
// If there are entry components, we have to reset the entire freakin' testing platform
84-
// in order to make test bed aware of them. There's got to be a better way... (o_O)
85-
TestBed.resetTestEnvironment();
86-
@NgModule({
87-
imports: [
88-
NativeScriptModule,
89-
NativeScriptTestingModule,
90-
CommonModule,
91-
],
92-
providers: [
93-
...NATIVESCRIPT_TESTING_PROVIDERS,
94-
],
95-
entryComponents: [...entryComponents]
96-
})
97-
class EntryComponentsTestModule {
66+
else {
67+
// If there are entry components, we have to reset the testing platform.
68+
//
69+
// There's got to be a better way... (o_O)
70+
TestBed.resetTestEnvironment();
71+
@NgModule({
72+
imports: [
73+
NativeScriptModule, NativeScriptTestingModule, CommonModule
74+
],
75+
providers: [
76+
...NATIVESCRIPT_TESTING_PROVIDERS,
77+
],
78+
entryComponents: [...entryComponents]
79+
})
80+
class EntryComponentsTestModule {
81+
}
82+
TestBed.initTestEnvironment(
83+
EntryComponentsTestModule,
84+
platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS)
85+
);
86+
TestBed.configureTestingModule({
87+
declarations: [...components, ...entryComponents],
88+
imports: [...imports],
89+
providers: [...providers],
90+
});
9891
}
99-
TestBed.initTestEnvironment(
100-
EntryComponentsTestModule,
101-
platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS)
102-
);
103-
TestBed.configureTestingModule({
104-
declarations: [...components, ...entryComponents],
105-
imports: [...imports],
106-
providers: [...providers],
107-
});
10892
TestBed.compileComponents()
10993
.then(() => done())
11094
.catch((e) => {
@@ -133,11 +117,8 @@ export function nBasicTestBedInit() {
133117
*/
134118
export function nTestBedAfterEach(resetEnv = true, resetFn = nBasicTestBedInit) {
135119
return () => {
136-
if (!resetEnv) {
137-
TestBed.resetTestingModule();
138-
}
139-
else {
140-
TestBed.resetTestingModule();
120+
TestBed.resetTestingModule();
121+
if (resetEnv) {
141122
TestBed.resetTestEnvironment();
142123
resetFn();
143124
}

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

+1-1
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
// >> router-outlet-example
44
import { Component, NgModule } from "@angular/core";

tests/app/tests/platform-filter-components.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export class PlatformSpecificAttributeComponent {
3939
}
4040
}
4141

42-
// TODO: Something is different in dumpView. I suspect it has to do with the fact that TestApp rendered directly
43-
// into a view container and returned the exact component, whereas testbed appears to return a reference to the root
44-
// component? Needs investigation.
45-
describe("Platform filter directives", () => {
42+
// TODO: Something is different in dumpView. I suspect it has to do with the fact that the old
43+
// test app class rendered directly into a view container and returned the exact component, whereas
44+
// testbed appears to return a reference to the root component? Needs investigation.
45+
xdescribe("Platform filter directives", () => {
4646

4747
describe("on IOS device", () => {
4848
beforeEach(nTestBedBeforeEach(

0 commit comments

Comments
 (0)