|
| 1 | +import { provideZonelessChangeDetection } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { LoadingController } from '@ionic/angular/standalone'; |
| 4 | + |
| 5 | +import { KitLoadingController } from './kit-loading.controller'; |
| 6 | + |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// Fake loading element factories |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +function fakeLoading() { |
| 11 | + return { |
| 12 | + present: vi.fn().mockResolvedValue(undefined), |
| 13 | + dismiss: vi.fn().mockResolvedValue(undefined), |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +// A loading whose present() stays pending until we resolve it, to reproduce a dismiss that arrives |
| 18 | +// mid-presentation. |
| 19 | +function deferredLoading() { |
| 20 | + let resolvePresent!: () => void; |
| 21 | + const loading = { |
| 22 | + present: vi.fn().mockReturnValue(new Promise<void>((r) => (resolvePresent = r))), |
| 23 | + dismiss: vi.fn().mockResolvedValue(undefined), |
| 24 | + }; |
| 25 | + return { loading, resolvePresent: () => resolvePresent() }; |
| 26 | +} |
| 27 | + |
| 28 | +function setup(loading: ReturnType<typeof fakeLoading> = fakeLoading()) { |
| 29 | + const loadingCtrl = { create: vi.fn().mockResolvedValue(loading) }; |
| 30 | + |
| 31 | + TestBed.configureTestingModule({ |
| 32 | + providers: [provideZonelessChangeDetection(), KitLoadingController, { provide: LoadingController, useValue: loadingCtrl }], |
| 33 | + }); |
| 34 | + |
| 35 | + return { controller: TestBed.inject(KitLoadingController), loadingCtrl, loading }; |
| 36 | +} |
| 37 | + |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +// Tests |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | +describe('KitLoadingController', () => { |
| 42 | + afterEach(() => { |
| 43 | + TestBed.resetTestingModule(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('presents a single indicator and passes options through to create', async () => { |
| 47 | + const { controller, loadingCtrl, loading } = setup(); |
| 48 | + await controller.presentLoading({ message: 'Loading…' }); |
| 49 | + expect(loadingCtrl.create).toHaveBeenCalledOnce(); |
| 50 | + expect(loadingCtrl.create).toHaveBeenCalledWith({ message: 'Loading…' }); |
| 51 | + expect(loading.present).toHaveBeenCalledOnce(); |
| 52 | + await controller.dismissLoading(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows one indicator for concurrent presents and dismisses only after the last release', async () => { |
| 56 | + const { controller, loadingCtrl, loading } = setup(); |
| 57 | + await controller.presentLoading(); |
| 58 | + await controller.presentLoading(); |
| 59 | + expect(loadingCtrl.create).toHaveBeenCalledOnce(); |
| 60 | + |
| 61 | + await controller.dismissLoading(); |
| 62 | + expect(loading.dismiss).not.toHaveBeenCalled(); // one reference still outstanding |
| 63 | + |
| 64 | + await controller.dismissLoading(); |
| 65 | + expect(loading.dismiss).toHaveBeenCalledOnce(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('is a no-op when dismissing while nothing is loading', async () => { |
| 69 | + const { controller, loadingCtrl, loading } = setup(); |
| 70 | + await controller.dismissLoading(); |
| 71 | + expect(loadingCtrl.create).not.toHaveBeenCalled(); |
| 72 | + expect(loading.dismiss).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('shows nothing when a present is immediately balanced by a dismiss', async () => { |
| 76 | + const { controller, loadingCtrl } = setup(); |
| 77 | + const p = controller.presentLoading(); |
| 78 | + const d = controller.dismissLoading(); |
| 79 | + await Promise.all([p, d]); |
| 80 | + expect(loadingCtrl.create).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not orphan the indicator when dismiss arrives before present() settles', async () => { |
| 84 | + const deferred = deferredLoading(); |
| 85 | + const { controller, loadingCtrl } = setup(deferred.loading); |
| 86 | + |
| 87 | + const p1 = controller.presentLoading(); // create resolves, then present() is left pending |
| 88 | + // Wait until we are genuinely mid-presentation (present() called but not settled), so the dismiss |
| 89 | + // lands inside the create→present window — the exact race a naive ref-count would orphan. |
| 90 | + await vi.waitFor(() => expect(deferred.loading.present).toHaveBeenCalled()); |
| 91 | + |
| 92 | + const p2 = controller.dismissLoading(); // count 0 → queued behind the still-pending present |
| 93 | + deferred.resolvePresent(); // present() settles → present task finishes → queued dismiss runs |
| 94 | + await Promise.all([p1, p2]); |
| 95 | + |
| 96 | + expect(loadingCtrl.create).toHaveBeenCalledOnce(); |
| 97 | + expect(deferred.loading.present).toHaveBeenCalledOnce(); |
| 98 | + // The key assertion: the indicator is torn down after present() settled, not left orphaned. |
| 99 | + expect(deferred.loading.dismiss).toHaveBeenCalledOnce(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('creates a fresh indicator on a new cycle after full dismissal', async () => { |
| 103 | + const { controller, loadingCtrl } = setup(); |
| 104 | + await controller.presentLoading(); |
| 105 | + await controller.dismissLoading(); |
| 106 | + await controller.presentLoading(); |
| 107 | + expect(loadingCtrl.create).toHaveBeenCalledTimes(2); |
| 108 | + await controller.dismissLoading(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('a failed present rolls back its reference and does not wedge later operations', async () => { |
| 112 | + const { controller, loadingCtrl, loading } = setup(); |
| 113 | + loadingCtrl.create.mockRejectedValueOnce(new Error('boom')); |
| 114 | + |
| 115 | + // The failed reference is rolled back internally — no compensating dismissLoading() is needed. |
| 116 | + await expect(controller.presentLoading()).rejects.toThrow('boom'); |
| 117 | + |
| 118 | + loadingCtrl.create.mockResolvedValue(loading); |
| 119 | + await controller.presentLoading(); |
| 120 | + expect(loading.present).toHaveBeenCalledOnce(); |
| 121 | + |
| 122 | + // A single dismiss tears the indicator down: proof the counter is balanced (not stuck at 1). |
| 123 | + await controller.dismissLoading(); |
| 124 | + expect(loading.dismiss).toHaveBeenCalledOnce(); |
| 125 | + }); |
| 126 | +}); |
0 commit comments