Skip to content

Commit 22cf5c9

Browse files
chore: make renderer lifecyle test more robust
- the first assertion is that the view after init has been called. rather than assert it, just wait for it using an observable and avoid asserting about timing and implementation specific details of the system. Specifically this removes the assumption that `app.tick()` will advance time and call `ngAfterViewInit` on the component.
1 parent b2de69d commit 22cf5c9

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

Diff for: tests/app/tests/renderer-tests.ts

+41-28
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// make sure you import mocha-config before @angular/core
2-
import {assert} from './test-config';
3-
import {Component, ComponentRef, ElementRef, NgZone, Renderer2, ViewChild} from '@angular/core';
4-
import {ProxyViewContainer} from 'ui/proxy-view-container';
5-
import {Red} from 'color/known-colors';
6-
import {dumpView} from './test-utils';
7-
import {LayoutBase} from 'ui/layouts/layout-base';
8-
import {StackLayout} from 'ui/layouts/stack-layout';
9-
import {ContentView} from 'ui/content-view';
10-
import {Button} from 'ui/button';
11-
import {registerElement} from 'nativescript-angular/element-registry';
12-
import * as button from 'tns-core-modules/ui/button';
13-
import * as view from 'tns-core-modules/ui/core/view';
14-
import {nTestBedAfterEach, nTestBedBeforeEach, nTestBedRender} from 'nativescript-angular/testing';
15-
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {assert} from "./test-config";
3+
import {Component, ComponentRef, ElementRef, NgZone, Renderer2, ViewChild} from "@angular/core";
4+
import {ProxyViewContainer} from "ui/proxy-view-container";
5+
import {Red} from "color/known-colors";
6+
import {dumpView} from "./test-utils";
7+
import {LayoutBase} from "ui/layouts/layout-base";
8+
import {StackLayout} from "ui/layouts/stack-layout";
9+
import {ContentView} from "ui/content-view";
10+
import {Button} from "ui/button";
11+
import {registerElement} from "nativescript-angular/element-registry";
12+
import * as button from "tns-core-modules/ui/button";
13+
import * as view from "tns-core-modules/ui/core/view";
14+
import {nTestBedAfterEach, nTestBedBeforeEach, nTestBedRender} from "nativescript-angular/testing";
15+
import {ComponentFixture, TestBed} from "@angular/core/testing";
16+
import {Observable} from "rxjs/Observable";
17+
import {ReplaySubject} from "rxjs/ReplaySubject";
1618

1719
@Component({
1820
template: `<StackLayout><Label text="Layout"></Label></StackLayout>`
@@ -226,10 +228,10 @@ export class NgControlSettersCount {
226228

227229
get buttons(): ElementRef[] { return [this.btn1, this.btn2, this.btn3, this.btn4]; }
228230

229-
isAfterViewInit: boolean = false;
231+
ready$: Observable<boolean> = new ReplaySubject<boolean>(1);
230232

231233
ngAfterViewInit() {
232-
this.isAfterViewInit = true;
234+
(this.ready$ as ReplaySubject<boolean>).next(true);
233235
}
234236
}
235237

@@ -609,8 +611,7 @@ describe("Renderer attach/detach", () => {
609611
});
610612
});
611613

612-
// TODO: I'm not sure about this lifecycle test.
613-
xdescribe("Renderer lifecycle", () => {
614+
describe("Renderer lifecycle", () => {
614615
let renderer: Renderer2 = null;
615616
beforeEach(nTestBedBeforeEach([ZonedRenderer, NgControlSettersCount]));
616617
afterEach(nTestBedAfterEach(false));
@@ -624,16 +625,28 @@ xdescribe("Renderer lifecycle", () => {
624625

625626
it("view native setters are called once on startup", () => {
626627
const fixture = TestBed.createComponent(NgControlSettersCount);
627-
fixture.detectChanges();
628-
const componentRef: ComponentRef<NgControlSettersCount> = fixture.componentRef;
629-
assert.isTrue(componentRef.instance.isAfterViewInit, "Expected the NgControlSettersCount to have passed its ngAfterViewInit.");
630-
componentRef.instance.buttons.map(btn => btn.nativeElement).forEach(btn => {
631-
assert.isTrue(btn.isLoaded, `Expected ${btn.id} to be allready loaded.`);
632-
assert.isFalse(btn.isLayoutValid, `Expected ${btn.id}'s layout to be invalid.`);
633-
634-
assert.equal(btn.backgroundInternalSetNativeCount, 1, `Expected ${btn.id} backgroundInternalSetNativeCount to be called just once.`);
635-
assert.equal(btn.fontInternalSetNativeCount, 1, `Expected ${btn.id} fontInternalSetNativeCount to be called just once.`);
636-
assert.equal(btn.nativeBackgroundRedraws, 0, `Expected ${btn.id} nativeBackgroundRedraws to be called after its layout pass.`);
628+
const component: NgControlSettersCount = fixture.componentInstance;
629+
return component.ready$.subscribe(() => {
630+
component.buttons.map(btn => btn.nativeElement).forEach(btn => {
631+
assert.isTrue(btn.isLoaded, `Expected ${btn.id} to be loaded.`);
632+
assert.isFalse(
633+
btn.isLayoutValid,
634+
`Expected ${btn.id}'s layout to be invalid because it is uninitialized.`
635+
);
636+
637+
assert.equal(
638+
btn.backgroundInternalSetNativeCount, 1,
639+
`Expected ${btn.id} backgroundInternalSetNativeCount to be called just once.`
640+
);
641+
assert.equal(
642+
btn.fontInternalSetNativeCount, 1,
643+
`Expected ${btn.id} fontInternalSetNativeCount to be called just once.`
644+
);
645+
assert.equal(
646+
btn.nativeBackgroundRedraws, 0,
647+
`Expected ${btn.id} nativeBackgroundRedraws to be called after its layout pass.`
648+
);
649+
});
637650
});
638651
});
639652
});

0 commit comments

Comments
 (0)