|
| 1 | +import { |
| 2 | + afterEach, describe, expect, test, vi, |
| 3 | +} from 'vitest'; |
| 4 | +import { downloadParticipantsProvenanceZip } from './handleDownloadFiles'; |
| 5 | +import type { StorageEngine } from '../storage/engines/types'; |
| 6 | +import type { StoredAnswer } from '../store/types'; |
| 7 | + |
| 8 | +const mockZipState = vi.hoisted(() => { |
| 9 | + const instances: Array<{ |
| 10 | + files: Record<string, unknown>; |
| 11 | + file: (name: string, content: unknown) => unknown; |
| 12 | + }> = []; |
| 13 | + |
| 14 | + class MockJSZip { |
| 15 | + files: Record<string, unknown> = {}; |
| 16 | + |
| 17 | + file = vi.fn((name: string, content: unknown) => { |
| 18 | + this.files[name] = content; |
| 19 | + return this; |
| 20 | + }); |
| 21 | + |
| 22 | + generateAsync = vi.fn(async () => new Blob(['zip'])); |
| 23 | + |
| 24 | + constructor() { |
| 25 | + instances.push(this); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return { instances, MockJSZip }; |
| 30 | +}); |
| 31 | + |
| 32 | +vi.mock('jszip', () => ({ |
| 33 | + default: mockZipState.MockJSZip, |
| 34 | +})); |
| 35 | + |
| 36 | +function makeStoredAnswer(overrides: Partial<StoredAnswer> = {}): StoredAnswer { |
| 37 | + return { |
| 38 | + answer: {}, |
| 39 | + identifier: 'intro_0', |
| 40 | + componentName: 'intro', |
| 41 | + trialOrder: '0', |
| 42 | + correctAnswer: [], |
| 43 | + incorrectAnswers: {}, |
| 44 | + startTime: 10, |
| 45 | + endTime: 20, |
| 46 | + windowEvents: [], |
| 47 | + timedOut: false, |
| 48 | + helpButtonClickedCount: 0, |
| 49 | + parameters: {}, |
| 50 | + optionOrders: {}, |
| 51 | + questionOrders: {}, |
| 52 | + ...overrides, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +describe('provenance downloads', () => { |
| 57 | + afterEach(() => { |
| 58 | + mockZipState.instances.splice(0, mockZipState.instances.length); |
| 59 | + vi.restoreAllMocks(); |
| 60 | + vi.unstubAllGlobals(); |
| 61 | + }); |
| 62 | + |
| 63 | + test('downloads provenance as a zip alongside the expected per-answer file name', async () => { |
| 64 | + const anchor = { |
| 65 | + href: '', |
| 66 | + download: '', |
| 67 | + click: vi.fn(), |
| 68 | + remove: vi.fn(), |
| 69 | + } as unknown as HTMLAnchorElement; |
| 70 | + const createElement = vi.fn().mockReturnValue(anchor); |
| 71 | + const appendChild = vi.fn(); |
| 72 | + vi.stubGlobal('document', { |
| 73 | + createElement, |
| 74 | + body: { |
| 75 | + appendChild, |
| 76 | + }, |
| 77 | + }); |
| 78 | + vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:zip'); |
| 79 | + vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined); |
| 80 | + |
| 81 | + const provenanceGraph = { |
| 82 | + aboveStimulus: undefined, |
| 83 | + belowStimulus: undefined, |
| 84 | + sidebar: undefined, |
| 85 | + stimulus: { |
| 86 | + root: 'root', |
| 87 | + nodes: { |
| 88 | + root: { |
| 89 | + id: 'root', |
| 90 | + createdOn: 10, |
| 91 | + children: [], |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + const storageEngine = { |
| 98 | + getProvenance: vi.fn().mockResolvedValue(provenanceGraph), |
| 99 | + } as unknown as StorageEngine; |
| 100 | + |
| 101 | + await downloadParticipantsProvenanceZip({ |
| 102 | + storageEngine, |
| 103 | + participants: [ |
| 104 | + { |
| 105 | + participantId: 'p1', |
| 106 | + answers: { |
| 107 | + intro_0: makeStoredAnswer(), |
| 108 | + }, |
| 109 | + }, |
| 110 | + ], |
| 111 | + studyId: 'study-1', |
| 112 | + }); |
| 113 | + |
| 114 | + expect(storageEngine.getProvenance).toHaveBeenCalledWith('intro_0', 'p1'); |
| 115 | + expect(mockZipState.instances).toHaveLength(1); |
| 116 | + expect(mockZipState.instances[0].files['study-1_p1_intro_0_provenance.json']).toBe( |
| 117 | + JSON.stringify(provenanceGraph, null, 2), |
| 118 | + ); |
| 119 | + expect(anchor.download).toBe('study-1_provenance.zip'); |
| 120 | + expect(anchor.click).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments