Skip to content

Commit

Permalink
Add a simple test for useDownload hook
Browse files Browse the repository at this point in the history
Combine Download with useDownload in a simple test.
  • Loading branch information
bjoernricks committed Jun 17, 2024
1 parent fca2542 commit 77b05c4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/web/components/form/__tests__/useDownload.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/* eslint-disable react/prop-types */

import {describe, test, expect, testing} from '@gsa/testing';

import {fireEvent, render, screen} from 'web/utils/testing';

import useDownload from '../useDownload';
import Download from '../download';

const TestComponent = () => {
const [ref, download] = useDownload();
return (
<>
<Download ref={ref} />
<button
data-testid="download"
onClick={() => download({filename: 'foo', data: 'bar'})}
/>
</>
);
};

describe('useDownload', () => {
test('should download a file', () => {
const createObjectURL = testing.fn().mockReturnValue('foo://bar');
window.URL.createObjectURL = createObjectURL;
window.URL.revokeObjectURL = testing.fn();

render(<TestComponent />);

fireEvent.click(screen.getByTestId('download'));

expect(createObjectURL).toHaveBeenCalled();
});
});

0 comments on commit 77b05c4

Please sign in to comment.