Skip to content

Commit bd1d082

Browse files
committed
chore: Fix spec
1 parent ebeb28d commit bd1d082

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

test/helpers.test.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ describe('downloadFile', () => {
151151
remove: jest.fn(),
152152
href: '',
153153
download: '',
154+
style: '',
154155
};
155156
document.createElement = jest.fn().mockReturnValue(mockDOMElement);
156157
document.body.append = jest.fn();
@@ -159,7 +160,7 @@ describe('downloadFile', () => {
159160
afterEach(() => jest.clearAllMocks());
160161

161162
describe('successful downloads', () => {
162-
it('should download PDF file', async () => {
163+
it('should download PDF file with correct fetch options', async () => {
163164
const blob = new Blob(['test'], { type: 'application/pdf' });
164165
mockFetch.mockResolvedValueOnce({
165166
ok: true,
@@ -173,12 +174,16 @@ describe('downloadFile', () => {
173174
extension: 'pdf',
174175
});
175176

176-
expect(mockFetch).toHaveBeenCalledWith('test.com/doc.pdf');
177+
expect(mockFetch).toHaveBeenCalledWith('test.com/doc.pdf', {
178+
method: 'GET',
179+
credentials: 'omit',
180+
mode: 'cors',
181+
});
177182
expect(mockCreateObjectURL).toHaveBeenCalledWith(blob);
178183
expect(mockDOMElement.click).toHaveBeenCalled();
179184
});
180185

181-
it('should download image file with content disposition', async () => {
186+
it('should download file with content disposition filename', async () => {
182187
const blob = new Blob(['test'], { type: 'image/png' });
183188
mockFetch.mockResolvedValueOnce({
184189
ok: true,
@@ -196,24 +201,41 @@ describe('downloadFile', () => {
196201
});
197202

198203
expect(mockDOMElement.download).toBe('test.png');
204+
expect(document.body.append).toHaveBeenCalled();
205+
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-url');
199206
});
200207
});
201208

202209
describe('error handling', () => {
203-
it('should skip if url or type missing', async () => {
204-
await downloadFile({ url: '', type: 'pdf' });
210+
it('should throw error if url or type missing', async () => {
211+
await expect(downloadFile({ url: '', type: 'pdf' })).rejects.toThrow(
212+
'Invalid download parameters'
213+
);
214+
215+
await expect(downloadFile({ url: 'test.com', type: '' })).rejects.toThrow(
216+
'Invalid download parameters'
217+
);
218+
205219
expect(mockFetch).not.toHaveBeenCalled();
206220
});
207221

208-
it('should handle network errors', async () => {
209-
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
222+
it('should throw error on failed response', async () => {
223+
mockFetch.mockResolvedValueOnce({
224+
ok: false,
225+
status: 404,
226+
});
227+
228+
await expect(
229+
downloadFile({ url: 'test.com/file', type: 'pdf' })
230+
).rejects.toThrow('Download failed: 404');
231+
});
232+
233+
it('should throw error on network failure', async () => {
210234
mockFetch.mockRejectedValueOnce(new Error('Network error'));
211235

212-
await downloadFile({ url: 'test.com/file', type: 'pdf' });
213-
expect(consoleSpy).toHaveBeenCalledWith(
214-
'Download failed:',
215-
expect.any(Error)
216-
);
236+
await expect(
237+
downloadFile({ url: 'test.com/file', type: 'pdf' })
238+
).rejects.toThrow('Network error');
217239
});
218240
});
219241
});

0 commit comments

Comments
 (0)