|
| 1 | +import { Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, Injector, NgModule, NO_ERRORS_SCHEMA, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { generateDetachedLoader, generateNativeScriptView, NgViewRef } from '@nativescript/angular'; |
| 4 | +import { GridLayout, ProxyViewContainer } from '@nativescript/core'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + template: `<ng-container #vc></ng-container><ng-template #template><GridLayout></GridLayout></ng-template>`, |
| 8 | +}) |
| 9 | +export class GenerateViewComponent { |
| 10 | + @ViewChild('vc', { read: ViewContainerRef }) vc: ViewContainerRef; |
| 11 | + @ViewChild('template', { read: TemplateRef }) template: TemplateRef<void>; |
| 12 | + constructor(public injector: Injector) {} |
| 13 | +} |
| 14 | + |
| 15 | +@Component({ |
| 16 | + template: `<GridLayout></GridLayout>`, |
| 17 | +}) |
| 18 | +export class GeneratedComponent {} |
| 19 | + |
| 20 | +@NgModule({ |
| 21 | + declarations: [GeneratedComponent, GenerateViewComponent], |
| 22 | + schemas: [NO_ERRORS_SCHEMA], |
| 23 | +}) |
| 24 | +export class GeneratedModule {} |
| 25 | + |
| 26 | +describe('generateNativeScriptView', () => { |
| 27 | + let fixture: ComponentFixture<GenerateViewComponent>; |
| 28 | + let cleanup: Array<NgViewRef<unknown> | ComponentRef<unknown> | EmbeddedViewRef<unknown>> = []; |
| 29 | + beforeEach(async () => { |
| 30 | + await TestBed.configureTestingModule({ |
| 31 | + declarations: [GenerateViewComponent, GeneratedComponent], |
| 32 | + }).compileComponents(); |
| 33 | + fixture = TestBed.createComponent(GenerateViewComponent); |
| 34 | + fixture.detectChanges(); |
| 35 | + await fixture.whenRenderingDone(); |
| 36 | + }); |
| 37 | + afterEach(() => { |
| 38 | + cleanup.forEach((v) => { |
| 39 | + if (v instanceof NgViewRef) { |
| 40 | + v.ref.destroy(); |
| 41 | + } |
| 42 | + if (v instanceof ComponentRef || v instanceof EmbeddedViewRef) { |
| 43 | + v.destroy(); |
| 44 | + } |
| 45 | + }); |
| 46 | + cleanup = []; |
| 47 | + }); |
| 48 | + |
| 49 | + it('should generate a native view', () => { |
| 50 | + const ngViewRef = generateNativeScriptView(GeneratedComponent, { |
| 51 | + injector: fixture.componentRef.instance.injector, |
| 52 | + }); |
| 53 | + cleanup.push(ngViewRef); |
| 54 | + expect(ngViewRef.view).toBeInstanceOf(ProxyViewContainer); |
| 55 | + expect(ngViewRef.firstNativeLikeView).toBeInstanceOf(GridLayout); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should generate a native view from template', () => { |
| 59 | + const ngViewRef = generateNativeScriptView(fixture.componentInstance.template, { |
| 60 | + injector: fixture.componentRef.instance.injector, |
| 61 | + }); |
| 62 | + cleanup.push(ngViewRef); |
| 63 | + expect(ngViewRef.view).toBeInstanceOf(GridLayout); |
| 64 | + expect(ngViewRef.firstNativeLikeView).toBeInstanceOf(GridLayout); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should reuse a DetachedLoaderRef', () => { |
| 68 | + const containerRef = generateDetachedLoader(fixture.componentRef.instance.injector.get(ComponentFactoryResolver), fixture.componentRef.instance.injector); |
| 69 | + cleanup.push(containerRef); |
| 70 | + const ngViewRef = generateNativeScriptView(GeneratedComponent, { |
| 71 | + injector: fixture.componentRef.instance.injector, |
| 72 | + detachedLoaderRef: containerRef, |
| 73 | + }); |
| 74 | + cleanup.push(ngViewRef); |
| 75 | + ngViewRef.ref.destroy(); |
| 76 | + expect(containerRef.hostView.destroyed).toBeFalse(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should destroy a DetachedLoaderRef', () => { |
| 80 | + const ngViewRef = generateNativeScriptView(GeneratedComponent, { |
| 81 | + injector: fixture.componentRef.instance.injector, |
| 82 | + viewContainerRef: fixture.componentInstance.vc, |
| 83 | + }); |
| 84 | + cleanup.push(ngViewRef); |
| 85 | + ngViewRef.ref.destroy(); |
| 86 | + expect((ngViewRef as any).detachedLoaderRef.hostView.destroyed).toBeTrue(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should destroy a DetachedLoaderRef from template', () => { |
| 90 | + const ngViewRef = generateNativeScriptView(fixture.componentInstance.template, { |
| 91 | + injector: fixture.componentRef.instance.injector, |
| 92 | + }); |
| 93 | + cleanup.push(ngViewRef); |
| 94 | + ngViewRef.ref.destroy(); |
| 95 | + expect((ngViewRef as any).detachedLoaderRef.hostView.destroyed).toBeTrue(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments