Skip to content

Commit 5d69119

Browse files
committed
refactor unit test
1 parent 5858657 commit 5d69119

1 file changed

Lines changed: 29 additions & 55 deletions

File tree

packages/components/src/DateTimeInput.test.tsx

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -160,55 +160,42 @@ it('onSubmit works correctly', async () => {
160160
});
161161

162162
describe('normalizeText', () => {
163-
it('replaces T separator with space for ISO 8601 format', async () => {
163+
it.each([
164+
[
165+
'replaces T separator with space for ISO 8601 format',
166+
'2022-02-22T12:30:45.123456789',
167+
'2022-02-22 12:30:45.123456789',
168+
],
169+
[
170+
'removes timezone information (Z)',
171+
'2022-02-22T12:30:45.123456789Z',
172+
'2022-02-22 12:30:45.123456789',
173+
],
174+
[
175+
'removes timezone information (offset)',
176+
'2022-02-22T12:30:45.123456789+05:00',
177+
'2022-02-22 12:30:45.123456789',
178+
],
179+
[
180+
'removes timezone information (named)',
181+
'2022-02-22 12:30:45.123456789 EDT',
182+
'2022-02-22 12:30:45.123456789',
183+
],
184+
[
185+
'handles datetime without fractional seconds',
186+
'2022-02-22T12:30:45',
187+
'2022-02-22 12:30:45.000000000',
188+
],
189+
])('%s', async (_, pastedText, expectedValue) => {
164190
const user = userEvent.setup();
165191
const onChange = jest.fn();
166192
const { unmount } = makeDateTimeInput({ onChange });
167193
const input: HTMLInputElement = screen.getByRole('textbox');
168194

169195
input.focus();
170-
await user.paste('2022-02-22T12:30:45.123456789');
196+
await user.paste(pastedText);
171197

172-
expect(onChange).toHaveBeenCalledWith('2022-02-22 12:30:45.123456789');
173-
unmount();
174-
});
175-
176-
it('removes timezone information (Z)', async () => {
177-
const user = userEvent.setup();
178-
const onChange = jest.fn();
179-
const { unmount } = makeDateTimeInput({ onChange });
180-
const input: HTMLInputElement = screen.getByRole('textbox');
181-
182-
input.focus();
183-
await user.paste('2022-02-22T12:30:45.123456789Z');
184-
185-
expect(onChange).toHaveBeenCalledWith('2022-02-22 12:30:45.123456789');
186-
unmount();
187-
});
188-
189-
it('removes timezone information (offset)', async () => {
190-
const user = userEvent.setup();
191-
const onChange = jest.fn();
192-
const { unmount } = makeDateTimeInput({ onChange });
193-
const input: HTMLInputElement = screen.getByRole('textbox');
194-
195-
input.focus();
196-
await user.paste('2022-02-22T12:30:45.123456789+05:00');
197-
198-
expect(onChange).toHaveBeenCalledWith('2022-02-22 12:30:45.123456789');
199-
unmount();
200-
});
201-
202-
it('removes timezone information (named)', async () => {
203-
const user = userEvent.setup();
204-
const onChange = jest.fn();
205-
const { unmount } = makeDateTimeInput({ onChange });
206-
const input: HTMLInputElement = screen.getByRole('textbox');
207-
208-
input.focus();
209-
await user.paste('2022-02-22 12:30:45.123456789 EDT');
210-
211-
expect(onChange).toHaveBeenCalledWith('2022-02-22 12:30:45.123456789');
198+
expect(onChange).toHaveBeenCalledWith(expectedValue);
212199
unmount();
213200
});
214201

@@ -224,17 +211,4 @@ describe('normalizeText', () => {
224211
expect(input.value).toBe(`2022-02-22 12:30:45.123${Z}456${Z}789`);
225212
unmount();
226213
});
227-
228-
it('handles datetime without fractional seconds', async () => {
229-
const user = userEvent.setup();
230-
const onChange = jest.fn();
231-
const { unmount } = makeDateTimeInput({ onChange });
232-
const input: HTMLInputElement = screen.getByRole('textbox');
233-
234-
input.focus();
235-
await user.paste('2022-02-22T12:30:45');
236-
237-
expect(onChange).toHaveBeenCalledWith('2022-02-22 12:30:45.000000000');
238-
unmount();
239-
});
240214
});

0 commit comments

Comments
 (0)