Files
pybricks-code/src/licenses/LicenseDialog.test.tsx
T
David Lechner 7cb0cf3151 test: clean up restoreAllMocks()
clearAllMocks() does not remove mock implementations, so we need to be
using restoreAllMocks() instead. Also ensure that it is called first
in case the other cleanup code needs to call a mocked function.
2022-04-04 15:51:57 -05:00

57 lines
1.7 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { Classes } from '@blueprintjs/core';
import { cleanup } from '@testing-library/react';
import React from 'react';
import { testRender } from '../../test';
import LicenseDialog from './LicenseDialog';
afterEach(() => {
jest.restoreAllMocks();
cleanup();
localStorage.clear();
});
describe('LicenseDialog', () => {
it('should show placeholder if no license is selected', () => {
const [dialog] = testRender(
<LicenseDialog isOpen={true} onClose={() => undefined} />,
);
expect(dialog.getByText('Select a package to view the license.')).toBeDefined();
});
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 [dialog] = testRender(
<LicenseDialog isOpen={true} onClose={() => undefined} />,
);
// have to wait for async fetch
const treeNode = await dialog.findByText('super-duper', {
selector: `.${Classes.TREE_NODE} *`,
});
// when the dialog is first shown, no license is selected
expect(dialog.queryByText('Joe Somebody')).toBeNull();
// then when you click on a license name, the license is shown
treeNode.click();
expect(dialog.getByText('Joe Somebody')).toBeDefined();
});
});