LicenseDialog: fix console errors in tests

useFetch() was trying to actually fetch the license file and mocking
fetch() caused errors about not wrapping code in act(). So instead,
we can avoid all errors by just mocking useFetch() instead.
This commit is contained in:
David Lechner
2023-03-10 18:16:27 -06:00
committed by David Lechner
parent 3114ac4e2b
commit 2f5d11b648
2 changed files with 24 additions and 14 deletions
+8
View File
@@ -3,9 +3,14 @@
import { act, getByLabelText, waitFor } from '@testing-library/react';
import React from 'react';
import * as useHooks from 'usehooks-ts';
import { testRender } from '../../test';
import AboutDialog from './AboutDialog';
afterEach(() => {
jest.restoreAllMocks();
});
it('should close when the button is clicked', async () => {
const close = jest.fn();
@@ -21,6 +26,9 @@ it('should manage license dialog open/close', async () => {
<AboutDialog isOpen={true} onClose={() => undefined} />,
);
// avoid test environment errors by providing fixed response to useFetch()
jest.spyOn(useHooks, 'useFetch').mockImplementation(() => ({}));
expect(
dialog.queryByRole('dialog', { name: 'Open Source Software Licenses' }),
).toBeNull();
+16 -14
View File
@@ -4,9 +4,25 @@
import { Classes } from '@blueprintjs/core';
import { act, cleanup } from '@testing-library/react';
import React from 'react';
import * as useHooks from 'usehooks-ts';
import { testRender } from '../../test';
import LicenseDialog from './LicenseDialog';
beforeEach(() => {
// avoid test environment errors by providing fixed response to useFetch()
jest.spyOn(useHooks, 'useFetch').mockImplementation(() => ({
data: [
{
name: 'super-duper',
version: '1.0.0',
author: 'Joe Somebody',
license: 'MIT',
licenseText: '...',
},
],
}));
});
afterEach(() => {
jest.restoreAllMocks();
cleanup();
@@ -23,20 +39,6 @@ describe('LicenseDialog', () => {
});
it('should show a license', async () => {
jest.spyOn(window, 'fetch').mockResolvedValue(
new Response(
JSON.stringify([
{
name: 'super-duper',
version: '1.0.0',
author: 'Joe Somebody',
license: 'MIT',
licenseText: '...',
},
]),
),
);
const [user, dialog] = testRender(
<LicenseDialog isOpen={true} onClose={() => undefined} />,
);