Skip to content

Commit 159d69c

Browse files
authored
feat(router): several apps on one page, and Ionic stacks in the Capacitor tab (#485)
Refs #368, #307
1 parent 5681d65 commit 159d69c

36 files changed

Lines changed: 3410 additions & 262 deletions
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 { LiveRoute } from '../pages/live-route';
5+
import type { RouterApp, RouterPage } from '../pages/router-types';
6+
7+
const root = { path: '', url: '', outlet: 'primary', params: {}, data: {}, children: [] };
8+
9+
function routerPage(extra: Partial<RouterPage> = {}): RouterPage {
10+
return {
11+
pageId: 'p1',
12+
reportedAt: 1,
13+
snapshot: { url: '/home', queryParams: {}, fragment: null, root },
14+
navigations: [
15+
{
16+
id: 1,
17+
url: '/home',
18+
trigger: 'imperative',
19+
startedAt: 1,
20+
endedAt: 2,
21+
outcome: 'succeeded',
22+
},
23+
],
24+
...extra,
25+
};
26+
}
27+
28+
const apps: RouterApp[] = [
29+
{ id: 'app-root', name: 'ShellApp', selector: 'app-root', primary: true },
30+
{
31+
id: 'cart-widget',
32+
name: 'CartWidget',
33+
selector: 'cart-widget',
34+
snapshot: { url: '/checkout', queryParams: {}, fragment: null, root },
35+
navigations: [
36+
{
37+
id: 4,
38+
url: '/checkout',
39+
trigger: 'imperative',
40+
startedAt: 3,
41+
endedAt: 4,
42+
outcome: 'failed',
43+
},
44+
],
45+
},
46+
];
47+
48+
function fakeClient(pages: RouterPage[]) {
49+
const calls: { name: string; arg: unknown }[] = [];
50+
const rpc = {
51+
call: (name: string, arg: unknown) => {
52+
calls.push({ name, arg });
53+
return Promise.resolve(null);
54+
},
55+
callEvent: () => Promise.resolve(),
56+
sharedState: () =>
57+
Promise.resolve({
58+
value: () => ({ pages }),
59+
on: () => () => {},
60+
}),
61+
};
62+
const client = { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
63+
return { client, calls };
64+
}
65+
66+
async function settle(fixture: ComponentFixture<unknown>) {
67+
for (let i = 0; i < 3; i++) {
68+
await new Promise((resolve) => setTimeout(resolve));
69+
await fixture.whenStable();
70+
}
71+
}
72+
73+
afterEach(() => {
74+
document.body.innerHTML = '';
75+
});
76+
77+
describe('LiveRoute on a page with several apps', () => {
78+
it('offers an app picker and shows the picked app', async () => {
79+
const { client } = fakeClient([routerPage({ apps })]);
80+
const fixture = TestBed.createComponent(LiveRoute);
81+
fixture.componentRef.setInput('rpc', client);
82+
document.body.append(fixture.nativeElement);
83+
await settle(fixture);
84+
const host = fixture.nativeElement as HTMLElement;
85+
expect(host.querySelector('#live-route-app-label')?.textContent).toBe('App');
86+
expect(fixture.componentInstance.appId()).toBe('app-root');
87+
expect(fixture.componentInstance.page()?.snapshot?.url).toBe('/home');
88+
expect(fixture.componentInstance.appOptions().map((o) => o.label)).toEqual([
89+
'ShellApp <app-root>',
90+
'CartWidget <cart-widget>',
91+
]);
92+
93+
fixture.componentInstance.appId.set('cart-widget');
94+
await settle(fixture);
95+
const view = fixture.componentInstance.page();
96+
expect(view?.appId).toBe('cart-widget');
97+
expect(view?.pageId).toBe('p1');
98+
expect(view?.snapshot?.url).toBe('/checkout');
99+
expect(fixture.componentInstance.problems()).toBe(1);
100+
});
101+
102+
it('shows no app picker on a page with one app', async () => {
103+
const page = routerPage();
104+
const { client } = fakeClient([page]);
105+
const fixture = TestBed.createComponent(LiveRoute);
106+
fixture.componentRef.setInput('rpc', client);
107+
document.body.append(fixture.nativeElement);
108+
await settle(fixture);
109+
const host = fixture.nativeElement as HTMLElement;
110+
expect(host.querySelector('#live-route-app-label')).toBeNull();
111+
expect(fixture.componentInstance.appId()).toBeNull();
112+
expect(fixture.componentInstance.page()).toBe(page);
113+
});
114+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { routerAppView, type RouterPage } from '../pages/router-types';
3+
4+
describe('routerAppView', () => {
5+
it('carries the loops of a secondary app into its view', () => {
6+
const loop = {
7+
kind: 'redirect' as const,
8+
ids: [1, 2],
9+
cycle: ['/a', '/b'],
10+
hops: [],
11+
guards: [],
12+
bounces: 1,
13+
end: '/b',
14+
};
15+
const page = {
16+
pageId: 'p',
17+
reportedAt: 1,
18+
changedAt: 1,
19+
snapshot: null,
20+
navigations: [],
21+
apps: [
22+
{ id: 'app-root', name: 'Shell', selector: 'app-root', primary: true },
23+
{ id: 'cart', name: 'Cart', selector: 'cart-widget', navigations: [], loops: [loop] },
24+
],
25+
} as unknown as RouterPage;
26+
expect(routerAppView(page, 'cart').loops).toEqual([loop]);
27+
expect(routerAppView(page, 'app-root').loops).toBeUndefined();
28+
});
29+
});

‎app/src/pages/capacitor-inspector.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { LimitNote } from '../ui/limit-note';
1919
import { Select, type SelectOption } from '../ui/select';
2020
import { panelConfig } from '../devtools-config';
2121
import { CapacitorCallDetail } from './capacitor-call-detail';
22+
import { CapacitorIonic } from './capacitor-ionic';
2223
import { CapacitorState, pluginStateCards } from './capacitor-state';
2324
import {
2425
SLOW_MS,
@@ -54,7 +55,7 @@ const FILTER_LABELS: Record<CallFilter, string> = {
5455

5556
@Component({
5657
selector: 'app-capacitor-inspector',
57-
imports: [SearchField, LimitNote, Select, CapacitorCallDetail, CapacitorState],
58+
imports: [SearchField, LimitNote, Select, CapacitorCallDetail, CapacitorIonic, CapacitorState],
5859
template: `
5960
@if (!loaded()) {
6061
<p class="state" role="status">Loading Capacitor data…</p>
@@ -164,6 +165,8 @@ const FILTER_LABELS: Record<CallFilter, string> = {
164165
</section>
165166
}
166167
168+
<app-capacitor-ionic [rpc]="rpc()" [pageId]="page.pageId" />
169+
167170
<section aria-labelledby="cap-calls-title">
168171
<h2 id="cap-calls-title">Plugin calls</h2>
169172
@if (!page.info.bridge && !page.info.webCalls) {

0 commit comments

Comments
 (0)