Skip to content

Commit a30554f

Browse files
authored
fix(date): update onChange to handle null value when date input is cleared
1 parent dbea64a commit a30554f

File tree

5 files changed

+207
-2
lines changed

5 files changed

+207
-2
lines changed

packages/Form/Input/date/src/Date.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { ChangeEvent, ComponentPropsWithRef, forwardRef } from 'react';
21
import { getComponentClassName } from '@axa-fr/react-toolkit-core';
32
import { withInput } from '@axa-fr/react-toolkit-form-core';
3+
import React, { ChangeEvent, ComponentPropsWithRef, forwardRef } from 'react';
44

55
type Props = Omit<ComponentPropsWithRef<'input'>, 'value'> & {
66
/** A modifier for specified className */
@@ -47,7 +47,6 @@ const handlers = {
4747
(e: ChangeEvent<HTMLInputElement>) => {
4848
const newValue = e.currentTarget.valueAsDate;
4949
onChange &&
50-
newValue &&
5150
onChange({
5251
value: newValue,
5352
name,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { render } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import React from 'react';
4+
import CustomDate from '../Date';
5+
6+
describe('Date component', () => {
7+
it('renders with initial value', () => {
8+
const initialValue = new Date('2024-03-04');
9+
const { asFragment, getByDisplayValue } = render(
10+
<CustomDate value={initialValue} />
11+
);
12+
13+
getByDisplayValue('2024-03-04');
14+
15+
expect(asFragment()).toMatchSnapshot();
16+
});
17+
18+
it('calls onChange handler when input value changes', () => {
19+
const onChangeMock = jest.fn();
20+
const initialValue = new Date('2024-03-04');
21+
const { getByLabelText, asFragment } = render(
22+
<>
23+
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
24+
<label htmlFor="date">Date:</label>
25+
<CustomDate
26+
onChange={onChangeMock}
27+
name="date"
28+
id="date"
29+
value={initialValue}
30+
/>
31+
</>
32+
);
33+
const inputElement = getByLabelText('Date:') as HTMLInputElement;
34+
35+
userEvent.type(inputElement, '2024-03-05');
36+
37+
expect(onChangeMock).toHaveBeenCalledWith({
38+
value: new Date('2024-03-05'),
39+
name: 'date',
40+
id: 'date',
41+
});
42+
43+
expect(inputElement.value).toBe('2024-03-05');
44+
45+
expect(asFragment()).toMatchSnapshot();
46+
});
47+
48+
it('calls onChange handler when input value changes although target value is null', () => {
49+
const initialValue = new Date('2024-03-04');
50+
const onChangeMock = jest.fn();
51+
const { getByLabelText, asFragment } = render(
52+
<>
53+
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
54+
<label htmlFor="date">Date:</label>
55+
<CustomDate
56+
onChange={onChangeMock}
57+
name="date"
58+
id="date"
59+
value={initialValue}
60+
/>
61+
</>
62+
);
63+
const inputElement = getByLabelText('Date:') as HTMLInputElement;
64+
65+
userEvent.clear(inputElement);
66+
67+
expect(inputElement.value).toBe('');
68+
69+
expect(onChangeMock).toHaveBeenCalledWith({
70+
value: null,
71+
name: 'date',
72+
id: 'date',
73+
});
74+
75+
expect(asFragment()).toMatchSnapshot();
76+
});
77+
78+
it('applies class modifier', () => {
79+
const { container, asFragment } = render(
80+
<CustomDate classModifier="required" />
81+
);
82+
container.querySelector('.af-form__input-date--required');
83+
84+
expect(asFragment()).toMatchSnapshot();
85+
});
86+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { render } from '@testing-library/react';
2+
import React from 'react';
3+
import DateInput from '../DateInput';
4+
5+
describe('<NumberInput>', () => {
6+
it('renders DateInput correctly', () => {
7+
const { asFragment, getByLabelText } = render(
8+
<DateInput
9+
label="Date début"
10+
id="iddateinput"
11+
name="namedateinput"
12+
value={new Date('2024-03-04')}
13+
onChange={() => {}}
14+
/>
15+
);
16+
17+
expect(asFragment()).toMatchSnapshot();
18+
19+
const inputElement = getByLabelText('Date début');
20+
expect(inputElement).toHaveAttribute('type', 'date');
21+
expect(inputElement).toHaveValue('2024-03-04');
22+
expect(inputElement).toHaveClass('af-form__input-date');
23+
24+
expect(inputElement.id).toBe('iddateinput');
25+
});
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Date component applies class modifier 1`] = `
4+
<DocumentFragment>
5+
<input
6+
class="af-form__input-date af-form__input-date--required"
7+
required=""
8+
type="date"
9+
value=""
10+
/>
11+
</DocumentFragment>
12+
`;
13+
14+
exports[`Date component calls onChange handler when input value changes 1`] = `
15+
<DocumentFragment>
16+
<label
17+
for="date"
18+
>
19+
Date:
20+
</label>
21+
<input
22+
class="af-form__input-date"
23+
id="date"
24+
name="date"
25+
type="date"
26+
value="2024-03-04"
27+
/>
28+
</DocumentFragment>
29+
`;
30+
31+
exports[`Date component calls onChange handler when input value changes although target value is null 1`] = `
32+
<DocumentFragment>
33+
<label
34+
for="date"
35+
>
36+
Date:
37+
</label>
38+
<input
39+
class="af-form__input-date"
40+
id="date"
41+
name="date"
42+
type="date"
43+
value="2024-03-04"
44+
/>
45+
</DocumentFragment>
46+
`;
47+
48+
exports[`Date component renders with initial value 1`] = `
49+
<DocumentFragment>
50+
<input
51+
class="af-form__input-date"
52+
type="date"
53+
value="2024-03-04"
54+
/>
55+
</DocumentFragment>
56+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<NumberInput> renders DateInput correctly 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="row af-form__group"
7+
>
8+
<div
9+
class="col-md-2"
10+
>
11+
<label
12+
class="af-form__group-label"
13+
for="iddateinput"
14+
>
15+
Date début
16+
</label>
17+
</div>
18+
<div
19+
class="col-md-10"
20+
>
21+
<div
22+
class="af-form__date"
23+
>
24+
<input
25+
class="af-form__input-date"
26+
id="iddateinput"
27+
name="namedateinput"
28+
type="date"
29+
value="2024-03-04"
30+
/>
31+
</div>
32+
<small
33+
class="af-form__help"
34+
/>
35+
</div>
36+
</div>
37+
</DocumentFragment>
38+
`;

0 commit comments

Comments
 (0)