import { RenderResult, act, screen, waitFor } from '@testing-library/react'; import…
- User
import { RenderResult, act, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event';
import { mockIpmaClient } from 'api/Client/ClientMock'; import { MarketCardVoidByTimeRange } from 'components/manualActions'; import { manualActionsEndpoints } from 'constants/manualActions'; import { roles } from 'constants/Roles'; import { mockVoidByTimeRangePayload } from 'models/ManualActions'; import { mockMarkets } from 'models/Market'; import { mockStates } from 'models/State'; import { StatusCode } from 'models/StatusCode'; import { ManualActionsWrapper, renderForTest } from 'testUtils'; import en from 'translations/en.json';
const testStrings = { fromDate: '15/02/2022 14:30:00', toDate: '15/02/2022 15:30:00', };
describe('MarketCardVoidByTimeRange.tsx', () => { let fromDate: HTMLElement, toDate: HTMLElement; let wrapper: RenderResult;
describe('default', () => { beforeEach(async () => { await act(async () => { wrapper = renderForTest( , ); }); }); it('should render with the default snapshot', () => { expect(wrapper.container).toMatchSnapshot(); });
it('should select a From date', async () => { await act(async () => { userEvent.type( screen.getByRole(roles.textbox, { name: en.form.from }), testStrings.fromDate, ); }); expect( screen.getByRole(roles.textbox, { name: en.form.from }), ).toHaveValue(testStrings.fromDate); }); // it('should select a To date', async () => { // const input = screen.getByRole(roles.textbox, { name: en.form.to }); // await act(async () => { // userEvent.type(input, testStrings.toDate); // }); // expect(input).toHaveValue(testStrings.toDate); // }); it('should select equal From/To dates and show validation', () => { fromDate = screen.getByRole(roles.textbox, { name: en.form.from }); toDate = screen.getByRole(roles.textbox, { name: en.form.to }); userEvent.type(fromDate, testStrings.fromDate); userEvent.type(toDate, testStrings.fromDate); expect(screen.getByText(en.validations.toMinMessage)).toBeInTheDocument(); });});
// describe('void markets', () => { // beforeAll(() => { // mockIpmaClient // .onPost(manualActionsEndpoints.postVoidByTimeRangeAPI) // .reply(StatusCode.OK, mockVoidByTimeRangePayload); // });
// beforeEach(async () => { // wrapper = renderForTest( // // <MarketCardVoidByTimeRange // markets={mockMarkets} // states={mockStates} // /> // , // );
// fromDate = screen.getByRole(roles.textbox, { name: en.form.from });
// toDate = screen.getByRole(roles.textbox, { name: en.form.to });
// userEvent.type(fromDate, testStrings.fromDate); // userEvent.type(toDate, testStrings.toDate);
// await act(async () => { // userEvent.click( // screen.getByRole(roles.button, { name: en.common.cta.void }), // ); // }); // });
- Assistant
The warning/error is caused by updates to the
MarketCardVoidByTimeRangecomponent's state during test execution not being wrapped in a properactfunction. React needs theseactwrappers to emulate the correct synchronous behavior of browser updates so that you can test the UI behavior as it would appear in the browser.In your case, the relevant portions of your tests are missing an
actwrapper, specifically when you calluserEvent.typeoruserEvent.click.To fix this, wrap the
userEvent.typeanduserEvent.clickcalls inside theactfunction:it('should select equal From/To dates and show validation', async () => { await act(async () => { // Wrap the userEvent calls inside act userEvent.type(screen.getByRole(roles.textbox, { name: en.form.from }), testStrings.fromDate); userEvent.type(screen.getByRole(roles.textbox, { name: en.form.to }), testStrings.fromDate); }); expect(screen.getByText(en.validations.toMinMessage)).toBeInTheDocument(); });Make sure to do this for all other tests where you are causing state updates.
After making these changes, your tests should no longer throw these warnings/errors.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.