mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-10-21 11:16:28 +00:00
Each test wrapped its whole sequence in waitFor() and slept 1s inside
every step, so waitFor's 1s timeout always expired ("Timed out in
waitFor"). Steps are now awaited in order, "open" is findBy* and
"closed" a single-condition waitFor, with an explicit cleanup between
tests. Both now pass (4 and 3) and exit, so they leave the yarn test
exclusions.
125 lines
3.0 KiB
TypeScript
125 lines
3.0 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/app-bounties authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
|
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import React, { Suspense } from 'react';
|
|
import { ThemeProvider } from 'styled-components';
|
|
|
|
import { lightTheme } from '@pezkuwi/react-components';
|
|
import { useToggle } from '@pezkuwi/react-hooks';
|
|
|
|
import Button from '../Button/index.js';
|
|
import i18next from '../i18n/index.js';
|
|
import Modal from './index.js';
|
|
|
|
function TestModal () {
|
|
const [isOpen, toggleIsOpen] = useToggle();
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
icon='plus'
|
|
isDisabled={false}
|
|
label='Open Test Modal'
|
|
onClick={toggleIsOpen}
|
|
/>
|
|
{isOpen && (
|
|
<Modal
|
|
header='Test Modal'
|
|
onClose={toggleIsOpen}
|
|
testId='test-modal'
|
|
>
|
|
<Modal.Content>
|
|
Test Modal Content
|
|
</Modal.Content>
|
|
<Modal.Actions>
|
|
<Button
|
|
icon='plus'
|
|
label='Submit Modal'
|
|
/>
|
|
</Modal.Actions>
|
|
</Modal>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
function renderModal () {
|
|
return render(
|
|
<Suspense fallback='...'>
|
|
<ThemeProvider theme={lightTheme}>
|
|
<TestModal />
|
|
</ThemeProvider>
|
|
</Suspense>
|
|
);
|
|
}
|
|
|
|
describe('Modal Component', () => {
|
|
beforeAll(async () => {
|
|
await i18next.changeLanguage('en');
|
|
});
|
|
|
|
afterEach(cleanup);
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('opens and closes modal', async () => {
|
|
renderModal();
|
|
|
|
await expectModalToBeClosed();
|
|
await openModal();
|
|
await expectModalToBeOpen();
|
|
await closeModal();
|
|
await expectModalToBeClosed();
|
|
});
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('renders all modal sections', async () => {
|
|
renderModal();
|
|
|
|
await expectModalToBeClosed();
|
|
await openModal();
|
|
await expectModalToBeOpen();
|
|
|
|
await screen.findByText('Test Modal');
|
|
await screen.findAllByText('Test Modal Content');
|
|
await screen.findByRole('button', { name: 'Submit Modal' });
|
|
});
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('closes modal with ESC button', async () => {
|
|
renderModal();
|
|
|
|
await expectModalToBeClosed();
|
|
await openModal();
|
|
await expectModalToBeOpen();
|
|
|
|
fireEvent.keyDown(window, {
|
|
charCode: 27,
|
|
code: 'Escape',
|
|
key: 'Escape',
|
|
keyCode: 27
|
|
});
|
|
|
|
await expectModalToBeClosed();
|
|
});
|
|
});
|
|
|
|
async function expectModalToBeClosed () {
|
|
await screen.findByRole('button', { name: 'Open Test Modal' });
|
|
await waitFor(() => expect(screen.queryAllByTestId('test-modal')).toHaveLength(0));
|
|
}
|
|
|
|
async function expectModalToBeOpen () {
|
|
await screen.findByTestId('test-modal');
|
|
}
|
|
|
|
async function openModal () {
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Open Test Modal' }));
|
|
}
|
|
|
|
async function closeModal () {
|
|
fireEvent.click(await screen.findByTestId('close-modal'));
|
|
}
|