|
| 1 | +import { Component, inject, NO_ERRORS_SCHEMA, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { FrameService, NativeDialogRef, NativeDialogService, NativeScriptCommonModule, NSLocationStrategy } from '@nativescript/angular'; |
| 4 | +import { View } from '@nativescript/core'; |
| 5 | +import { firstValueFrom } from 'rxjs'; |
| 6 | + |
| 7 | +import { FakeFrameService } from './ns-location-strategy.spec'; |
| 8 | +import { closeRemainingModals, isPresentingModally, topRootModalView } from './test-utils.spec'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'dialog-content-comp', |
| 12 | + template: `<GridLayout><Label text="dialog content"></Label></GridLayout>`, |
| 13 | + imports: [NativeScriptCommonModule], |
| 14 | + schemas: [NO_ERRORS_SCHEMA], |
| 15 | +}) |
| 16 | +export class DialogContentComponent { |
| 17 | + ref = inject(NativeDialogRef<DialogContentComponent>); |
| 18 | +} |
| 19 | + |
| 20 | +@Component({ |
| 21 | + selector: 'dialog-host-comp', |
| 22 | + template: `<GridLayout> |
| 23 | + <Label text="dialog host"></Label> |
| 24 | + <ng-template #dialogTemplate><Label text="template dialog content"></Label></ng-template> |
| 25 | + </GridLayout>`, |
| 26 | + imports: [NativeScriptCommonModule], |
| 27 | + schemas: [NO_ERRORS_SCHEMA], |
| 28 | +}) |
| 29 | +export class DialogHostComponent { |
| 30 | + dialog = inject(NativeDialogService); |
| 31 | + vcRef = inject(ViewContainerRef); |
| 32 | + @ViewChild('dialogTemplate', { static: true }) dialogTemplate: TemplateRef<unknown>; |
| 33 | +} |
| 34 | + |
| 35 | +describe('native-dialog', () => { |
| 36 | + beforeEach(() => { |
| 37 | + return TestBed.configureTestingModule({ |
| 38 | + imports: [DialogHostComponent, DialogContentComponent, NativeScriptCommonModule], |
| 39 | + providers: [{ provide: FrameService, useValue: new FakeFrameService() }, NSLocationStrategy], |
| 40 | + }).compileComponents(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => closeRemainingModals()); |
| 44 | + |
| 45 | + async function createHost(): Promise<DialogHostComponent> { |
| 46 | + const fixture = TestBed.createComponent(DialogHostComponent); |
| 47 | + fixture.detectChanges(); |
| 48 | + await fixture.whenRenderingDone(); |
| 49 | + return fixture.componentRef.instance; |
| 50 | + } |
| 51 | + |
| 52 | + it( |
| 53 | + 'afterOpened emits once the modal is fully presented', |
| 54 | + async () => { |
| 55 | + const host = await createHost(); |
| 56 | + const ref = host.dialog.open(DialogContentComponent, { viewContainerRef: host.vcRef }); |
| 57 | + await firstValueFrom(ref.afterOpened()); |
| 58 | + const closed = firstValueFrom(ref.afterClosed()); |
| 59 | + ref.close(); |
| 60 | + await closed; |
| 61 | + }, |
| 62 | + 10000, |
| 63 | + ); |
| 64 | + |
| 65 | + it( |
| 66 | + 'afterClosed emits only after the native dismissal completes, so a new dialog can open immediately', |
| 67 | + async () => { |
| 68 | + const host = await createHost(); |
| 69 | + const hostView: View = host.vcRef.element.nativeElement; |
| 70 | + |
| 71 | + const firstRef = host.dialog.open(DialogContentComponent, { viewContainerRef: host.vcRef }); |
| 72 | + await firstValueFrom(firstRef.afterOpened()); |
| 73 | + |
| 74 | + const events: string[] = []; |
| 75 | + firstRef.beforeClosed().subscribe(() => events.push('beforeClosed')); |
| 76 | + firstRef.afterClosed().subscribe(() => events.push('afterClosed')); |
| 77 | + |
| 78 | + const closed = firstValueFrom(firstRef.afterClosed()); |
| 79 | + firstRef.close('first result'); |
| 80 | + expect(await closed).toEqual('first result'); |
| 81 | + expect(events).toEqual(['beforeClosed', 'afterClosed']); |
| 82 | + // On iOS a premature afterClosed would leave the parent still presenting the old |
| 83 | + // view controller, which is what makes the next showModal fail. |
| 84 | + expect(isPresentingModally(hostView)).toBe(false); |
| 85 | + |
| 86 | + const secondRef = host.dialog.open(DialogContentComponent, { viewContainerRef: host.vcRef }); |
| 87 | + await firstValueFrom(secondRef.afterOpened()); |
| 88 | + const secondClosed = firstValueFrom(secondRef.afterClosed()); |
| 89 | + secondRef.close(); |
| 90 | + await secondClosed; |
| 91 | + }, |
| 92 | + 15000, |
| 93 | + ); |
| 94 | + |
| 95 | + it( |
| 96 | + 'afterClosed emits when a component dialog is dismissed natively', |
| 97 | + async () => { |
| 98 | + const host = await createHost(); |
| 99 | + const ref = host.dialog.open(DialogContentComponent, { viewContainerRef: host.vcRef }); |
| 100 | + await firstValueFrom(ref.afterOpened()); |
| 101 | + |
| 102 | + const closed = firstValueFrom(ref.afterClosed()); |
| 103 | + // Dismiss through core, like the user swiping down or pressing back. |
| 104 | + topRootModalView().closeModal(); |
| 105 | + await closed; |
| 106 | + }, |
| 107 | + 10000, |
| 108 | + ); |
| 109 | + |
| 110 | + it( |
| 111 | + 'afterClosed emits when a template dialog is dismissed natively', |
| 112 | + async () => { |
| 113 | + const host = await createHost(); |
| 114 | + const ref = host.dialog.open(host.dialogTemplate, { viewContainerRef: host.vcRef }); |
| 115 | + await firstValueFrom(ref.afterOpened()); |
| 116 | + |
| 117 | + const closed = firstValueFrom(ref.afterClosed()); |
| 118 | + topRootModalView().closeModal(); |
| 119 | + await closed; |
| 120 | + }, |
| 121 | + 10000, |
| 122 | + ); |
| 123 | +}); |
0 commit comments