-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdetached-utils-tests.spec.ts
More file actions
117 lines (108 loc) · 3.9 KB
/
detached-utils-tests.spec.ts
File metadata and controls
117 lines (108 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
Component,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
EmbeddedViewRef,
inject,
Injector,
NgModule,
NO_ERRORS_SCHEMA,
TemplateRef,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { generateDetachedLoader, generateNativeScriptView, NativeScriptCommonModule, NgViewRef } from '@nativescript/angular';
import { GridLayout, ProxyViewContainer } from '@nativescript/core';
@Component({
template: `<ng-container #vc></ng-container><ng-template #template><GridLayout></GridLayout></ng-template>`,
imports: [NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class GenerateViewComponent {
@ViewChild('vc', { read: ViewContainerRef }) vc: ViewContainerRef;
@ViewChild('template', { read: TemplateRef }) template: TemplateRef<void>;
injector = inject(Injector);
}
@Component({
template: `<GridLayout></GridLayout>`,
imports: [NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class GeneratedComponent {}
@NgModule({
imports: [GeneratedComponent, GenerateViewComponent, NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class GeneratedModule {}
describe('generateNativeScriptView', () => {
let fixture: ComponentFixture<GenerateViewComponent>;
let cleanup: Array<NgViewRef<unknown> | ComponentRef<unknown> | EmbeddedViewRef<unknown>> = [];
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [GenerateViewComponent, GeneratedComponent],
}).compileComponents();
fixture = TestBed.createComponent(GenerateViewComponent);
fixture.detectChanges();
await fixture.whenRenderingDone();
});
afterEach(() => {
cleanup.forEach((v) => {
if (v instanceof NgViewRef) {
v.ref.destroy();
}
if (v instanceof ComponentRef || v instanceof EmbeddedViewRef) {
v.destroy();
}
});
cleanup = [];
});
it('should generate a native view', () => {
const ngViewRef = generateNativeScriptView(GeneratedComponent, {
injector: fixture.componentRef.instance.injector,
});
cleanup.push(ngViewRef);
expect(ngViewRef.view).toBeInstanceOf(ProxyViewContainer);
expect(ngViewRef.firstNativeLikeView).toBeInstanceOf(GridLayout);
});
it('should generate a native view from template', () => {
const ngViewRef = generateNativeScriptView(fixture.componentInstance.template, {
injector: fixture.componentRef.instance.injector,
});
cleanup.push(ngViewRef);
expect(ngViewRef.view).toBeInstanceOf(GridLayout);
expect(ngViewRef.firstNativeLikeView).toBeInstanceOf(GridLayout);
});
it('should reuse a DetachedLoaderRef', () => {
const containerRef = generateDetachedLoader(
fixture.componentRef.instance.injector.get(ComponentFactoryResolver),
fixture.componentRef.instance.injector,
);
cleanup.push(containerRef);
const ngViewRef = generateNativeScriptView(GeneratedComponent, {
injector: fixture.componentRef.instance.injector,
detachedLoaderRef: containerRef,
});
cleanup.push(ngViewRef);
ngViewRef.ref.destroy();
expect(containerRef.hostView.destroyed).toBeFalse();
});
it('should destroy a DetachedLoaderRef', () => {
const ngViewRef = generateNativeScriptView(GeneratedComponent, {
injector: fixture.componentRef.instance.injector,
viewContainerRef: fixture.componentInstance.vc,
});
cleanup.push(ngViewRef);
ngViewRef.ref.destroy();
expect((ngViewRef as any).detachedLoaderRef.hostView.destroyed).toBeTrue();
});
it('should destroy a DetachedLoaderRef from template', () => {
const ngViewRef = generateNativeScriptView(fixture.componentInstance.template, {
injector: fixture.componentRef.instance.injector,
});
cleanup.push(ngViewRef);
ngViewRef.ref.destroy();
expect((ngViewRef as any).detachedLoaderRef.hostView.destroyed).toBeTrue();
});
});