|
| 1 | +import {beforeEach, describe, expect, it, vi} from 'vitest'; |
| 2 | + |
| 3 | +const openMock = vi.fn(); |
| 4 | +const onChangeRegistrations: Array<(files: FileList | null) => void> = []; |
| 5 | +const resetMock = vi.fn(); |
| 6 | + |
| 7 | +vi.mock('@vueuse/core', () => ({ |
| 8 | + useFileDialog: vi.fn(() => ({ |
| 9 | + open: openMock, |
| 10 | + onChange: (handler: (files: FileList | null) => void) => { |
| 11 | + onChangeRegistrations.push(handler); |
| 12 | + }, |
| 13 | + reset: resetMock, |
| 14 | + })), |
| 15 | +})); |
| 16 | + |
| 17 | +describe('createLazySingleFileDialog', () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.resetModules(); |
| 20 | + vi.clearAllMocks(); |
| 21 | + onChangeRegistrations.length = 0; |
| 22 | + vi.useFakeTimers(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('reuses one dialog registration and routes selection to the latest handler', async () => { |
| 26 | + const {createLazySingleFileDialog} = await import('@/utility/fileDialogUtils.ts'); |
| 27 | + |
| 28 | + const dialog = createLazySingleFileDialog('.csv'); |
| 29 | + const firstHandler = vi.fn(); |
| 30 | + const secondHandler = vi.fn(); |
| 31 | + |
| 32 | + dialog.openForSelection(firstHandler); |
| 33 | + dialog.openForSelection(secondHandler); |
| 34 | + |
| 35 | + expect(onChangeRegistrations).toHaveLength(1); |
| 36 | + expect(openMock).not.toHaveBeenCalled(); |
| 37 | + |
| 38 | + vi.runAllTimers(); |
| 39 | + |
| 40 | + expect(openMock).toHaveBeenCalledTimes(2); |
| 41 | + |
| 42 | + const files = {0: new File(['a'], 'test.csv'), length: 1} as unknown as FileList; |
| 43 | + onChangeRegistrations[0]!(files); |
| 44 | + |
| 45 | + expect(firstHandler).not.toHaveBeenCalled(); |
| 46 | + expect(secondHandler).toHaveBeenCalledWith(files); |
| 47 | + expect(resetMock).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | +}); |
0 commit comments