|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 3 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 4 | +import { CapacitorIonic, directionText, type IonicPage } from '../pages/capacitor-ionic'; |
| 5 | + |
| 6 | +function ionicPage(pageId = 'tab1'): IonicPage { |
| 7 | + return { |
| 8 | + pageId, |
| 9 | + url: '/tabs/tab1/detail', |
| 10 | + title: 'Ionic app', |
| 11 | + framework: '@ionic/angular', |
| 12 | + mode: 'ios', |
| 13 | + reportedAt: 1, |
| 14 | + outlets: [ |
| 15 | + { |
| 16 | + id: 'c1-1', |
| 17 | + name: 'primary', |
| 18 | + tabs: '/tabs/tab1', |
| 19 | + source: 'angular', |
| 20 | + canGoBack: true, |
| 21 | + direction: 'back', |
| 22 | + directionSource: 'event', |
| 23 | + animated: true, |
| 24 | + pages: [ |
| 25 | + { |
| 26 | + component: 'HomePage', |
| 27 | + tag: 'app-home', |
| 28 | + url: '/tabs/tab1', |
| 29 | + active: true, |
| 30 | + elementId: 'e1', |
| 31 | + }, |
| 32 | + { |
| 33 | + component: 'DetailPage', |
| 34 | + tag: 'app-detail', |
| 35 | + url: '/tabs/tab1/detail', |
| 36 | + active: false, |
| 37 | + elementId: 'e2', |
| 38 | + }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + ], |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function fakeClient(pages: IonicPage[]) { |
| 46 | + const calls: { name: string; arg: unknown }[] = []; |
| 47 | + const rpc = { |
| 48 | + call: (name: string, arg: unknown) => { |
| 49 | + calls.push({ name, arg }); |
| 50 | + return Promise.resolve(null); |
| 51 | + }, |
| 52 | + callEvent: () => Promise.resolve(), |
| 53 | + sharedState: (name: string) => |
| 54 | + Promise.resolve({ |
| 55 | + value: () => |
| 56 | + name === 'ionic' ? { pages: Object.fromEntries(pages.map((p) => [p.pageId, p])) } : {}, |
| 57 | + on: () => () => {}, |
| 58 | + }), |
| 59 | + }; |
| 60 | + const client = { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient; |
| 61 | + return { client, calls }; |
| 62 | +} |
| 63 | + |
| 64 | +async function settle(fixture: ComponentFixture<unknown>) { |
| 65 | + for (let i = 0; i < 3; i++) { |
| 66 | + await new Promise((resolve) => setTimeout(resolve)); |
| 67 | + await fixture.whenStable(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +afterEach(() => { |
| 72 | + document.body.innerHTML = ''; |
| 73 | +}); |
| 74 | + |
| 75 | +describe('CapacitorIonic', () => { |
| 76 | + it('lists the page stack of each outlet with the active page and the last direction', async () => { |
| 77 | + const { client, calls } = fakeClient([ionicPage()]); |
| 78 | + const fixture = TestBed.createComponent(CapacitorIonic); |
| 79 | + fixture.componentRef.setInput('rpc', client); |
| 80 | + fixture.componentRef.setInput('pageId', 'tab1'); |
| 81 | + document.body.append(fixture.nativeElement); |
| 82 | + await settle(fixture); |
| 83 | + const host = fixture.nativeElement as HTMLElement; |
| 84 | + expect(host.querySelector('h2')?.textContent).toBe('Ionic page stacks'); |
| 85 | + const rows = Array.from(host.querySelectorAll<HTMLButtonElement>('.stack-page')); |
| 86 | + expect(rows.map((row) => row.querySelector('.component')?.textContent)).toEqual([ |
| 87 | + 'HomePage', |
| 88 | + 'DetailPage', |
| 89 | + ]); |
| 90 | + expect(rows[0].getAttribute('aria-current')).toBe('page'); |
| 91 | + expect(rows[1].getAttribute('aria-current')).toBeNull(); |
| 92 | + expect(host.textContent).toContain('/tabs/tab1'); |
| 93 | + expect(host.textContent).toContain('Back'); |
| 94 | + |
| 95 | + rows[1].dispatchEvent(new Event('focus')); |
| 96 | + expect(calls.at(-1)).toEqual({ |
| 97 | + name: 'request-page-highlight', |
| 98 | + arg: { pageId: 'tab1', id: 'e2', reveal: false }, |
| 99 | + }); |
| 100 | + rows[1].dispatchEvent(new Event('blur')); |
| 101 | + expect(calls.at(-1)).toEqual({ name: 'request-page-highlight', arg: null }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('shows nothing for a page that does not use Ionic', async () => { |
| 105 | + const { client } = fakeClient([ionicPage('other')]); |
| 106 | + const fixture = TestBed.createComponent(CapacitorIonic); |
| 107 | + fixture.componentRef.setInput('rpc', client); |
| 108 | + fixture.componentRef.setInput('pageId', 'tab1'); |
| 109 | + await settle(fixture); |
| 110 | + expect((fixture.nativeElement as HTMLElement).querySelector('section')).toBeNull(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('words the direction with its source', () => { |
| 114 | + const outlet = ionicPage().outlets[0]; |
| 115 | + expect(directionText({ ...outlet, direction: undefined })).toBe('Not seen yet'); |
| 116 | + expect(directionText({ ...outlet, directionSource: 'inferred', direction: 'forward' })).toBe( |
| 117 | + 'Forward (from the stack)', |
| 118 | + ); |
| 119 | + expect(directionText({ ...outlet, direction: 'root', tabSwitch: true, animated: false })).toBe( |
| 120 | + 'Root (tab switch, no animation)', |
| 121 | + ); |
| 122 | + }); |
| 123 | +}); |
0 commit comments