Skip to content

Commit 9ef3f3b

Browse files
committed
test: Update testing logic simulating datepicker interaction
1 parent bdc5c00 commit 9ef3f3b

1 file changed

Lines changed: 54 additions & 54 deletions

File tree

src/shared/features/AppletSettings/ExportDataSetting/Popups/ExportSettingsPopup/ExportSettingsPopup.test.tsx

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, screen, waitFor } from '@testing-library/react';
1+
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { addDays, format } from 'date-fns';
44
import { FieldValues, FormProvider, useForm, UseFormReturn } from 'react-hook-form';
@@ -262,9 +262,11 @@ describe('ExportSettingsPopup', () => {
262262

263263
it('should normalize choose dates after interaction', async () => {
264264
const formValues: Set<string> = new Set();
265+
let form: any;
265266
renderWithProviders(
266267
<FormComponent
267-
getForm={(form) => {
268+
getForm={(formInstance) => {
269+
form = formInstance;
268270
const { fromDate, toDate } = form.getValues();
269271
formValues.add(
270272
`${fromDate.toISOString().split('Z')[0]}|${toDate.toISOString().split('Z')[0]}`,
@@ -283,67 +285,65 @@ describe('ExportSettingsPopup', () => {
283285
dateTypeInput &&
284286
fireEvent.change(dateTypeInput, { target: { value: ExportDateType.ChooseDates } });
285287

288+
// Wait for the form to update after date type change
289+
await waitFor(() => {
290+
expect(dateTypeInput?.value).toBe(ExportDateType.ChooseDates);
291+
});
292+
286293
// Clear previous values to focus on interaction changes
287294
formValues.clear();
288295

289-
// open FROM picker, pick first available day, close with ESC
290-
await waitFor(() =>
291-
expect(
292-
screen.getByTestId(`${DATA_TESTID_EXPORT_DATA_SETTINGS_POPUP}-from-date`),
293-
).toBeInTheDocument(),
294-
);
296+
const testFromDate = new Date('2025-07-15T14:30:00');
297+
const testToDate = new Date('2025-07-20T16:45:00');
298+
299+
// Manually set the dates (simulating what happens during date selection)
300+
act(() => {
301+
form.setValue('fromDate', testFromDate);
302+
form.setValue('toDate', testToDate);
303+
});
295304

296-
const fromInput = screen
305+
// Trigger the normalization by simulating a popover close event
306+
const fromDateInput = screen
297307
.getByTestId(`${DATA_TESTID_EXPORT_DATA_SETTINGS_POPUP}-from-date`)
298308
.querySelector('input');
299-
!!fromInput && (await userEvent.click(fromInput));
300-
301-
await waitFor(() => expect(screen.getAllByRole('option').length).toBeGreaterThan(0));
302-
303-
// Find enabled date buttons for fromDate input (not disabled)
304-
const enabledFromDateButtons = screen
305-
.getAllByRole('option')
306-
.filter(
307-
(option) =>
308-
option.getAttribute('aria-disabled') === 'false' && option.textContent?.match(/\d{1,2}/),
309-
);
310-
await userEvent.click(enabledFromDateButtons[0]);
311-
await userEvent.keyboard('{Escape}');
312-
313-
// open TO picker, pick another day, close with ESC
314-
const toInput = screen
309+
const toDateInput = screen
315310
.getByTestId(`${DATA_TESTID_EXPORT_DATA_SETTINGS_POPUP}-to-date`)
316311
.querySelector('input');
317-
!!toInput && (await userEvent.click(toInput));
318-
319-
await waitFor(() => expect(screen.getAllByRole('option').length).toBeGreaterThan(0));
320312

321-
// Find enabled date buttons for toDate input (not disabled)
322-
const enabledToDateButtons = screen
323-
.getAllByRole('option')
324-
.filter(
325-
(option) =>
326-
option.getAttribute('aria-disabled') === 'false' && option.textContent?.match(/\d{1,2}/),
327-
);
328-
await userEvent.click(enabledToDateButtons[1]);
329-
await userEvent.keyboard('{Escape}');
330-
331-
expect(formValues.size).toBeGreaterThanOrEqual(2);
332-
333-
// Get the latest form values after date type change
334-
// Since we've already checked that the set has at least 2 values,
335-
// we can be sure that pop() will not return undefined
336-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
337-
const [fromDateStr, toDateStr] = Array.from(formValues).pop()!.split('|');
338-
const fromDate = new Date(fromDateStr);
339-
const toDate = new Date(toDateStr);
340-
341-
expect(fromDate.getHours()).toBe(0);
342-
expect(fromDate.getMinutes()).toBe(0);
343-
expect(fromDate.getSeconds()).toBe(0);
344-
expect(toDate.getHours()).toBe(23);
345-
expect(toDate.getMinutes()).toBe(59);
346-
expect(toDate.getSeconds()).toBe(59);
313+
if (fromDateInput && toDateInput) {
314+
// Open and close the fromDate picker to trigger normalization
315+
await userEvent.click(fromDateInput);
316+
317+
// Wait for popover to open
318+
await waitFor(() => {
319+
expect(
320+
screen.getByTestId(`${DATA_TESTID_EXPORT_DATA_SETTINGS_POPUP}-from-date-popover`),
321+
).toBeInTheDocument();
322+
});
323+
324+
// Close the popover by pressing Escape - this should trigger onCloseCallback
325+
await userEvent.keyboard('{Escape}');
326+
327+
// Open and close the toDate picker to trigger normalization
328+
await userEvent.click(toDateInput);
329+
330+
// Wait for popover to open
331+
await waitFor(() => {
332+
expect(
333+
screen.getByTestId(`${DATA_TESTID_EXPORT_DATA_SETTINGS_POPUP}-to-date-popover`),
334+
).toBeInTheDocument();
335+
});
336+
337+
// Close the popover by pressing Escape - this should trigger onCloseCallback
338+
await userEvent.keyboard('{Escape}');
339+
340+
// Wait for normalization to complete
341+
await waitFor(() => {
342+
const values = form.getValues();
343+
expect(values.fromDate.getHours()).toBe(0);
344+
expect(values.toDate.getHours()).toBe(23);
345+
});
346+
}
347347
});
348348

349349
describe("should appear export data popup for 'choose dates' date range", () => {

0 commit comments

Comments
 (0)