|
| 1 | + |
| 2 | +import { View } from "tns-core-modules/ui/core/view"; |
| 3 | +import { topmost } from "tns-core-modules/ui/frame"; |
| 4 | +import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base"; |
| 5 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 6 | +import { NgModule, Type } from "@angular/core"; |
| 7 | +import { NativeScriptModule } from "../../nativescript.module"; |
| 8 | +import { platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing"; |
| 9 | +import { NS_COMPILER_PROVIDERS } from "../../platform"; |
| 10 | +import { NATIVESCRIPT_TESTING_PROVIDERS, NativeScriptTestingModule } from "../index"; |
| 11 | +import { CommonModule } from "@angular/common"; |
| 12 | +/** |
| 13 | + * Get a reference to the root application view. |
| 14 | + */ |
| 15 | +export function testingRootView(): View { |
| 16 | + return topmost().currentPage.content; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Declared test contexts. When the suite is done this map should be empty if all lifecycle |
| 21 | + * calls have happened as expected. |
| 22 | + * @private |
| 23 | + */ |
| 24 | +const activeTestFixtures: ComponentFixture<any>[][] = []; |
| 25 | + |
| 26 | +/** |
| 27 | + * Return a promise that resolves after (durationMs) milliseconds |
| 28 | + */ |
| 29 | +export function promiseWait(durationMs: number) { |
| 30 | + return () => new Promise((resolve) => setTimeout(() => resolve(), durationMs)); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Perform basic TestBed environment initialization. Call this once in the main entry point to your tests. |
| 35 | + */ |
| 36 | +export function nTestBedInit() { |
| 37 | + TestBed.initTestEnvironment( |
| 38 | + NativeScriptTestingModule, |
| 39 | + platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS) |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Helper for configuring a TestBed instance for rendering components for test. Ideally this |
| 45 | + * would not be needed, and in truth it's just a wrapper to eliminate some boilerplate. It |
| 46 | + * exists because when you need to specify `entryComponents` for a test the setup becomes quite |
| 47 | + * a bit more complex than if you're just doing a basic component test. |
| 48 | + * |
| 49 | + * More about entryComponents complexity: https://github.com/angular/angular/issues/12079 |
| 50 | + * |
| 51 | + * Use: |
| 52 | + * ``` |
| 53 | + * beforeEach(nTestBedBeforeEach([MyComponent,MyFailComponent])); |
| 54 | + * ``` |
| 55 | + * |
| 56 | + * **NOTE*** Remember to pair with {@see nTestBedAfterEach} |
| 57 | + * |
| 58 | + * @param components Any components that you will create during the test |
| 59 | + * @param providers Any services your tests depend on |
| 60 | + * @param imports Any module imports your tests depend on |
| 61 | + * @param entryComponents Any entry components that your tests depend on |
| 62 | + */ |
| 63 | +export function nTestBedBeforeEach( |
| 64 | + components: any[], |
| 65 | + providers: any[] = [], |
| 66 | + imports: any[] = [], |
| 67 | + entryComponents: any[] = []) { |
| 68 | + return (done) => { |
| 69 | + activeTestFixtures.push([]); |
| 70 | + // If there are no entry components we can take the simple path. |
| 71 | + if (entryComponents.length === 0) { |
| 72 | + TestBed.configureTestingModule({ |
| 73 | + declarations: [...components], |
| 74 | + providers: [...providers], |
| 75 | + imports: [NativeScriptModule, ...imports] |
| 76 | + }); |
| 77 | + } else { |
| 78 | + // If there are entry components, we have to reset the testing platform. |
| 79 | + // |
| 80 | + // There's got to be a better way... (o_O) |
| 81 | + TestBed.resetTestEnvironment(); |
| 82 | + @NgModule({ |
| 83 | + declarations: entryComponents, |
| 84 | + exports: entryComponents, |
| 85 | + entryComponents: entryComponents |
| 86 | + }) |
| 87 | + class EntryComponentsTestModule { |
| 88 | + } |
| 89 | + TestBed.initTestEnvironment( |
| 90 | + EntryComponentsTestModule, |
| 91 | + platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS) |
| 92 | + ); |
| 93 | + TestBed.configureTestingModule({ |
| 94 | + declarations: components, |
| 95 | + imports: [ |
| 96 | + NativeScriptModule, NativeScriptTestingModule, CommonModule, |
| 97 | + ...imports |
| 98 | + ], |
| 99 | + providers: [...providers, ...NATIVESCRIPT_TESTING_PROVIDERS], |
| 100 | + }); |
| 101 | + } |
| 102 | + TestBed.compileComponents() |
| 103 | + .then(() => done()) |
| 104 | + .catch((e) => { |
| 105 | + console.log(`Failed to instantiate test component with error: ${e}`); |
| 106 | + console.log(e.stack); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Helper for a basic component TestBed clean up. |
| 114 | + * @param resetEnv When true the testing environment will be reset |
| 115 | + * @param resetFn When resetting the environment, use this init function |
| 116 | + */ |
| 117 | +export function nTestBedAfterEach(resetEnv = true, resetFn = nTestBedInit) { |
| 118 | + return () => { |
| 119 | + if (activeTestFixtures.length === 0) { |
| 120 | + throw new Error( |
| 121 | + `There are no more declared fixtures.` + |
| 122 | + `Did you call "nTestBedBeforeEach" and "nTestBedAfterEach" an equal number of times?` |
| 123 | + ); |
| 124 | + } |
| 125 | + const root = testingRootView() as LayoutBase; |
| 126 | + const fixtures = activeTestFixtures.pop(); |
| 127 | + fixtures.forEach((fixture) => { |
| 128 | + root.removeChild(fixture.nativeElement); |
| 129 | + fixture.destroy(); |
| 130 | + }); |
| 131 | + TestBed.resetTestingModule(); |
| 132 | + if (resetEnv) { |
| 133 | + TestBed.resetTestEnvironment(); |
| 134 | + resetFn(); |
| 135 | + } |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Render a component using the TestBed helper, and return a promise that resolves when the |
| 141 | + * ComponentFixture is fully initialized. |
| 142 | + */ |
| 143 | +export function nTestBedRender<T>(componentType: Type<T>): Promise<ComponentFixture<T>> { |
| 144 | + const fixture = TestBed.createComponent(componentType); |
| 145 | + fixture.detectChanges(); |
| 146 | + return fixture.whenRenderingDone() |
| 147 | + // TODO(jd): it seems that the whenStable and whenRenderingDone utilities of ComponentFixture |
| 148 | + // do not work as expected. I looked at how to fix it and it's not clear how to provide |
| 149 | + // a {N} specific subclass, because ComponentFixture is newed directly rather than injected |
| 150 | + // What to do about it? Maybe fakeAsync can help? For now just setTimeout for 100ms (x_X) |
| 151 | + .then(promiseWait(100)) |
| 152 | + .then(() => { |
| 153 | + const list = activeTestFixtures[activeTestFixtures.length - 1]; |
| 154 | + if (!list) { |
| 155 | + console.warn( |
| 156 | + "nTestBedRender called without nTestBedBeforeEach/nTestBedAfter each. " + |
| 157 | + "You are responsible for calling 'fixture.destroy()' when your test is done " + |
| 158 | + "in order to clean up the components that are created." |
| 159 | + ); |
| 160 | + } else { |
| 161 | + list.push(fixture); |
| 162 | + } |
| 163 | + return fixture; |
| 164 | + }); |
| 165 | +} |
0 commit comments