|
| 1 | +import { Injector, runInInjectionContext, signal } from '@angular/core'; |
| 2 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 3 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 5 | +import { RouteTimeline } from '../pages/route-timeline'; |
| 6 | +import type { NavigationRecord, RouterPage } from '../pages/router-types'; |
| 7 | +import { LiveFeed } from '../ui/live-feed'; |
| 8 | + |
| 9 | +interface Entry { |
| 10 | + id: number; |
| 11 | +} |
| 12 | + |
| 13 | +function feed(scope = signal('p1')) { |
| 14 | + const source = signal<Entry[]>([{ id: 1 }, { id: 2 }]); |
| 15 | + const live = runInInjectionContext(TestBed.inject(Injector), () => { |
| 16 | + return new LiveFeed<Entry>({ |
| 17 | + name: 'test', |
| 18 | + source, |
| 19 | + key: (entry) => entry.id, |
| 20 | + newest: 'last', |
| 21 | + scope, |
| 22 | + }); |
| 23 | + }); |
| 24 | + return { source, live, scope }; |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(() => localStorage.clear()); |
| 28 | +afterEach(() => { |
| 29 | + document.body.innerHTML = ''; |
| 30 | + TestBed.resetTestingModule(); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('LiveFeed', () => { |
| 34 | + it('passes the live list through until it is paused', () => { |
| 35 | + const { source, live } = feed(); |
| 36 | + expect(live.items()).toEqual([{ id: 1 }, { id: 2 }]); |
| 37 | + source.set([{ id: 1 }, { id: 2 }, { id: 3 }]); |
| 38 | + expect(live.items().length).toBe(3); |
| 39 | + expect(live.newestKey()).toBe(3); |
| 40 | + expect(live.paused()).toBe(false); |
| 41 | + expect(live.pending()).toBe(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('holds the entries while paused, counts the new ones and shows them on resume', () => { |
| 45 | + const { source, live } = feed(); |
| 46 | + live.pause(); |
| 47 | + source.set([{ id: 2 }, { id: 3 }, { id: 4 }]); |
| 48 | + expect(live.items()).toEqual([{ id: 1 }, { id: 2 }]); |
| 49 | + expect(live.pending()).toBe(2); |
| 50 | + live.toggle(); |
| 51 | + expect(live.paused()).toBe(false); |
| 52 | + expect(live.items()).toEqual([{ id: 2 }, { id: 3 }, { id: 4 }]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('stays paused on another page, holding that page’s entries', () => { |
| 56 | + const { source, live, scope } = feed(); |
| 57 | + live.pause(); |
| 58 | + source.set([{ id: 9 }]); |
| 59 | + scope.set('p2'); |
| 60 | + expect(live.paused()).toBe(true); |
| 61 | + expect(live.items()).toEqual([{ id: 9 }]); |
| 62 | + source.set([{ id: 9 }, { id: 10 }]); |
| 63 | + expect(live.pending()).toBe(1); |
| 64 | + }); |
| 65 | + |
| 66 | + it('saves Follow newest per feed', () => { |
| 67 | + const { live } = feed(); |
| 68 | + expect(live.follow()).toBe(false); |
| 69 | + live.setFollow(true); |
| 70 | + expect(localStorage.getItem('pangular-pref:follow:test')).toBe('true'); |
| 71 | + expect(feed().live.follow()).toBe(true); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('the router timeline live controls', () => { |
| 76 | + function nav(id: number, url: string): NavigationRecord { |
| 77 | + return { id, url, trigger: 'imperative', startedAt: id, endedAt: id + 1, outcome: 'succeeded' }; |
| 78 | + } |
| 79 | + function page(navigations: NavigationRecord[]): RouterPage { |
| 80 | + return { pageId: 'p1', reportedAt: 1, changedAt: 1, snapshot: null, navigations }; |
| 81 | + } |
| 82 | + async function settle(fixture: ComponentFixture<unknown>) { |
| 83 | + for (let i = 0; i < 3; i++) { |
| 84 | + await new Promise((resolve) => setTimeout(resolve)); |
| 85 | + await fixture.whenStable(); |
| 86 | + } |
| 87 | + } |
| 88 | + const client = { |
| 89 | + connectionMeta: {}, |
| 90 | + scope: () => ({ rpc: { call: () => Promise.resolve(null) } }), |
| 91 | + } as unknown as DevframeRpcClient; |
| 92 | + |
| 93 | + it('holds the navigations while paused and shows the held count', async () => { |
| 94 | + const fixture = TestBed.createComponent(RouteTimeline); |
| 95 | + fixture.componentRef.setInput('page', page([nav(1, '/a'), nav(2, '/b')])); |
| 96 | + fixture.componentRef.setInput('rpc', client); |
| 97 | + document.body.append(fixture.nativeElement as HTMLElement); |
| 98 | + await settle(fixture); |
| 99 | + const root = fixture.nativeElement as HTMLElement; |
| 100 | + const urls = () => [...root.querySelectorAll('.navs .head .url')].map((e) => e.textContent); |
| 101 | + expect(urls()).toEqual(['/b', '/a']); |
| 102 | + |
| 103 | + const pause = root.querySelector<HTMLButtonElement>('app-live-controls button')!; |
| 104 | + pause.click(); |
| 105 | + fixture.componentRef.setInput('page', page([nav(1, '/a'), nav(2, '/b'), nav(3, '/c')])); |
| 106 | + await settle(fixture); |
| 107 | + expect(urls()).toEqual(['/b', '/a']); |
| 108 | + expect(root.querySelector('.pending')!.textContent!.trim()).toBe('1 new'); |
| 109 | + expect(pause.getAttribute('aria-describedby')).toBe(root.querySelector('.pending')!.id); |
| 110 | + |
| 111 | + pause.click(); |
| 112 | + await settle(fixture); |
| 113 | + expect(urls()).toEqual(['/c', '/b', '/a']); |
| 114 | + }); |
| 115 | +}); |
0 commit comments