Skip to content

Commit 56131d8

Browse files
PanayotCankovvakrilov
authored and
vakrilov
committed
Add tests to ensure native setters are called exactly once when components are instantiated
1 parent 164f582 commit 56131d8

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

Diff for: tests/app/logo.png

4.8 KB
Loading

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

+91-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// make sure you import mocha-config before @angular/core
22
import { assert } from "./test-config";
3-
import { Component, ElementRef, Renderer2, NgZone } from "@angular/core";
3+
import { Component, ElementRef, Renderer2, NgZone, ViewChild } from "@angular/core";
44
import { ProxyViewContainer } from "ui/proxy-view-container";
55
import { Red } from "color/known-colors";
66
import { dumpView } from "./test-utils";
@@ -10,6 +10,9 @@ import { StackLayout } from "ui/layouts/stack-layout";
1010
import { ContentView } from "ui/content-view";
1111
import { Button } from "ui/button";
1212
import { NgView } from "nativescript-angular/element-registry";
13+
import { registerElement } from "nativescript-angular/element-registry";
14+
import * as button from "tns-core-modules/ui/button";
15+
import * as view from "tns-core-modules/ui/core/view";
1316

1417
@Component({
1518
template: `<StackLayout><Label text="Layout"></Label></StackLayout>`
@@ -169,6 +172,60 @@ export class NgIfThenElseComponent {
169172
}
170173
}
171174

175+
export class ButtonCounter extends button.Button {
176+
nativeBackgroundRedraws = 0;
177+
backgroundInternalSetNativeCount = 0;
178+
fontInternalSetNativeCount = 0;
179+
180+
[view.backgroundInternalProperty.setNative](value) {
181+
this.backgroundInternalSetNativeCount++;
182+
return super[view.backgroundInternalProperty.setNative](value);
183+
}
184+
[view.fontInternalProperty.setNative](value) {
185+
this.fontInternalSetNativeCount++;
186+
return super[view.fontInternalProperty.setNative](value);
187+
}
188+
_redrawNativeBackground(value: any): void {
189+
this.nativeBackgroundRedraws++;
190+
super["_redrawNativeBackground"](value);
191+
}
192+
}
193+
registerElement("ButtonCounter", () => ButtonCounter);
194+
195+
@Component({
196+
selector: "ng-control-setters-count",
197+
template: `
198+
<StackLayout>
199+
<ButtonCounter #btn1 id="btn1" borderWidth="1" borderColor="gray" borderRadius="16" fontWeight="bold" fontSize="16"></ButtonCounter>
200+
<ButtonCounter #btn2 id="btn2"></ButtonCounter>
201+
<ButtonCounter #btn3 id="btn3" borderWidth="1" borderColor="gray" borderRadius="16" fontWeight="bold" fontSize="16"></ButtonCounter>
202+
<ButtonCounter #btn4 id="btn4" borderRadius="3" style="background-image: url('~/logo.png'); background-position: center; background-repeat: no-repeat; background-size: cover;"></ButtonCounter>
203+
</StackLayout>
204+
`,
205+
styles: [`
206+
#btn2, #btn3, #btn4 {
207+
border-width: 2;
208+
border-color: teal;
209+
border-radius: 20;
210+
font-weight: 400;
211+
font-size: 32;
212+
}`]
213+
})
214+
export class NgControlSettersCount {
215+
@ViewChild("btn1") btn1: ElementRef;
216+
@ViewChild("btn2") btn2: ElementRef;
217+
@ViewChild("btn3") btn3: ElementRef;
218+
@ViewChild("btn3") btn4: ElementRef;
219+
220+
get buttons(): ElementRef[] { return [this.btn1, this.btn2, this.btn3, this.btn4]; }
221+
222+
isAfterViewInit: boolean = false;
223+
224+
ngAfterViewInit() {
225+
this.isAfterViewInit = true;
226+
}
227+
}
228+
172229
@Component({
173230
selector: "ng-for-label",
174231
template: `<Label *ngFor="let item of items" [text]="item"></Label>`
@@ -488,7 +545,6 @@ describe("Renderer createElement", () => {
488545
});
489546
});
490547

491-
492548
describe("Renderer attach/detach", () => {
493549
let testApp: TestApp = null;
494550
let renderer: Renderer2 = null;
@@ -545,3 +601,36 @@ describe("Renderer attach/detach", () => {
545601
});
546602
});
547603

604+
describe("Renderer lifecycle", () => {
605+
let testApp: TestApp = null;
606+
let renderer: Renderer2 = null;
607+
608+
before(() => {
609+
return TestApp.create([], [NgControlSettersCount]).then((app) => {
610+
testApp = app;
611+
renderer = testApp.renderer;
612+
});
613+
});
614+
615+
after(() => {
616+
testApp.dispose();
617+
});
618+
619+
afterEach(() => {
620+
testApp.disposeComponents();
621+
});
622+
623+
it("view native setters are called once on startup", () => {
624+
return testApp.loadComponent(NgControlSettersCount).then((componentRef) => {
625+
assert.isTrue(componentRef.instance.isAfterViewInit, "Expected the NgControlSettersCount to have passed its ngAfterViewInit.");
626+
componentRef.instance.buttons.map(btn => btn.nativeElement).forEach(btn => {
627+
assert.isTrue(btn.isLoaded, `Expected ${btn.id} to be allready loaded.`);
628+
assert.isFalse(btn.isLayoutValid, `Expected ${btn.id}'s layout to be invalid.`);
629+
630+
assert.equal(btn.backgroundInternalSetNativeCount, 1, `Expected ${btn.id} backgroundInternalSetNativeCount to be called just once.`);
631+
assert.equal(btn.fontInternalSetNativeCount, 1, `Expected ${btn.id} fontInternalSetNativeCount to be called just once.`);
632+
assert.equal(btn.nativeBackgroundRedraws, 0, `Expected ${btn.id} nativeBackgroundRedraws to be called after its layout pass.`);
633+
});
634+
});
635+
});
636+
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class TestApp {
3030
registerTestApp(TestApp, this, appRef);
3131
}
3232

33-
public loadComponent(componentType: Type<any>): Promise<ComponentRef<any>> {
33+
public loadComponent<T>(componentType: Type<T>): Promise<ComponentRef<T>> {
3434
const factory = this.resolver.resolveComponentFactory(componentType);
3535
const componentRef = this.containerRef.createComponent(
3636
factory, this.containerRef.length, this.containerRef.parentInjector);

0 commit comments

Comments
 (0)