11import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
2- import { bulkExport , downloadSession } from './export.js' ;
32
43vi . mock ( './projects.js' , ( ) => ( { showProjects : vi . fn ( ) } ) ) ;
4+ vi . mock ( './shared/utils.js' , async ( importOriginal ) => {
5+ const actual = await importOriginal ( ) ;
6+ return {
7+ ...actual ,
8+ showConfirm : vi . fn ( ( _ , cb ) => { cb ( ) ; } ) ,
9+ } ;
10+ } ) ;
511
12+ import { bulkExport , downloadSession } from './export.js' ;
613import { showProjects } from './projects.js' ;
714
815const mockWritable = {
@@ -30,30 +37,28 @@ describe('export', () => {
3037 } ) ;
3138
3239 afterEach ( ( ) => {
40+ vi . unstubAllGlobals ( ) ;
3341 vi . restoreAllMocks ( ) ;
3442 } ) ;
3543
36- async function confirmExport ( ) {
37- const ok = document . querySelector ( '.confirm-ok' ) ;
38- expect ( ok ) . not . toBeNull ( ) ;
39- ok . click ( ) ;
40- await vi . waitFor ( ( ) => expect ( fetch ) . toHaveBeenCalled ( ) ) ;
41- }
42-
4344 it ( 'bulkExport shows progress then completes on success' , async ( ) => {
44- fetch . mockResolvedValue ( {
45+ let resolveFetch ;
46+ const pending = new Promise ( ( resolve ) => { resolveFetch = resolve ; } ) ;
47+ fetch . mockImplementation ( ( ) => pending . then ( ( ) => ( {
4548 ok : true ,
4649 headers : { get : ( ) => 'application/zip' } ,
4750 blob : ( ) => Promise . resolve ( new Blob ( [ 'zip' ] , { type : 'application/zip' } ) ) ,
48- } ) ;
51+ } ) ) ) ;
4952
5053 bulkExport ( 'all' ) ;
5154 const btn = document . getElementById ( 'btn-export-all' ) ;
52- expect ( btn . disabled ) . toBe ( false ) ;
53- await confirmExport ( ) ;
55+ await vi . waitFor ( ( ) => expect ( btn . disabled ) . toBe ( true ) ) ;
56+ expect ( btn . textContent ) . toContain ( 'Exporting' ) ;
57+
58+ resolveFetch ( ) ;
59+ await vi . waitFor ( ( ) => expect ( btn . disabled ) . toBe ( false ) ) ;
5460
5561 expect ( btn . textContent . trim ( ) ) . toBe ( 'Export all' ) ;
56- expect ( btn . disabled ) . toBe ( false ) ;
5762 expect ( mockHandle . createWritable ) . toHaveBeenCalled ( ) ;
5863 expect ( showProjects ) . toHaveBeenCalled ( ) ;
5964 expect ( fetch ) . toHaveBeenCalledWith ( '/api/export' , expect . objectContaining ( {
@@ -71,7 +76,6 @@ describe('export', () => {
7176 } ) ;
7277
7378 bulkExport ( 'all' ) ;
74- await confirmExport ( ) ;
7579 await vi . waitFor ( ( ) => expect ( document . querySelector ( '.toast-error' ) ) . not . toBeNull ( ) ) ;
7680
7781 expect ( document . querySelector ( '.toast-error' ) . textContent ) . toContain ( 'export failed' ) ;
@@ -86,7 +90,6 @@ describe('export', () => {
8690 } ) ;
8791
8892 bulkExport ( 'incremental' ) ;
89- await confirmExport ( ) ;
9093 await vi . waitFor ( ( ) => expect ( document . querySelector ( '.toast-error' ) ) . not . toBeNull ( ) ) ;
9194
9295 expect ( document . querySelector ( '.toast-error' ) . textContent ) . toContain ( 'Export failed: 403' ) ;
0 commit comments