Skip to content

Commit 572a2b5

Browse files
committed
Add tests and snapshot!
1 parent f2effab commit 572a2b5

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import DateInput from '../DateInput';
4+
5+
describe('DateInput', () => {
6+
it('matches snapshot', () => {
7+
const component = mount(<DateInput />);
8+
expect(component).toMatchSnapshot();
9+
component.unmount();
10+
});
11+
12+
it('wraps onChange handlers', () => {
13+
const onChange = jest.fn();
14+
const component = mount(<DateInput name="testInput" onChange={onChange} />);
15+
// Day
16+
component
17+
.find('div.nhsuk-date-input')
18+
.simulate('change', { target: { name: 'testInput-day', value: '27' } });
19+
expect(component.state('value')).toEqual({
20+
day: '27',
21+
month: '',
22+
year: '',
23+
});
24+
expect(onChange).toHaveBeenCalledTimes(1);
25+
expect(onChange.mock.calls[0][0].target.value).toEqual({
26+
day: '27',
27+
month: '',
28+
year: '',
29+
});
30+
31+
// Month
32+
component
33+
.find('div.nhsuk-date-input')
34+
.simulate('change', { target: { name: 'testInput-month', value: '06' } });
35+
expect(component.state('value')).toEqual({
36+
day: '27',
37+
month: '06',
38+
year: '',
39+
});
40+
expect(onChange).toHaveBeenCalledTimes(2);
41+
expect(onChange.mock.calls[0][0].target.value).toEqual({
42+
day: '27',
43+
month: '06',
44+
year: '',
45+
});
46+
47+
// Year
48+
component
49+
.find('div.nhsuk-date-input')
50+
.simulate('change', { target: { name: 'testInput-year', value: '2000' } });
51+
expect(component.state('value')).toEqual({
52+
day: '27',
53+
month: '06',
54+
year: '2000',
55+
});
56+
expect(onChange).toHaveBeenCalledTimes(3);
57+
expect(onChange.mock.calls[0][0].target.value).toEqual({
58+
day: '27',
59+
month: '06',
60+
year: '2000',
61+
});
62+
63+
component.unmount();
64+
});
65+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`DateInput matches snapshot 1`] = `
4+
<DateInput>
5+
<LabelBlock />
6+
<div
7+
className="nhsuk-date-input"
8+
onChange={[Function]}
9+
>
10+
<DateInputDay>
11+
<DateInputInput
12+
dateInputType="Day"
13+
pattern="[0-9]*"
14+
type="number"
15+
>
16+
<div
17+
className="nhsuk-date-input__item"
18+
>
19+
<div
20+
className="nhsuk-form-group"
21+
>
22+
<label
23+
className="nhsuk-label nhsuk-date-input__label"
24+
htmlFor="dateinput_01p06-day"
25+
>
26+
Day
27+
</label>
28+
<input
29+
aria-label="dateinput_01p06-day input"
30+
className="nhsuk-input nhsuk-date-input__input nhsuk-input--width-2"
31+
id="dateinput_01p06-day"
32+
name="dateinput_01p06-day"
33+
pattern="[0-9]*"
34+
type="number"
35+
/>
36+
</div>
37+
</div>
38+
</DateInputInput>
39+
</DateInputDay>
40+
<DateInputMonth>
41+
<DateInputInput
42+
dateInputType="Month"
43+
pattern="[0-9]*"
44+
type="number"
45+
>
46+
<div
47+
className="nhsuk-date-input__item"
48+
>
49+
<div
50+
className="nhsuk-form-group"
51+
>
52+
<label
53+
className="nhsuk-label nhsuk-date-input__label"
54+
htmlFor="dateinput_01p06-month"
55+
>
56+
Month
57+
</label>
58+
<input
59+
aria-label="dateinput_01p06-month input"
60+
className="nhsuk-input nhsuk-date-input__input nhsuk-input--width-2"
61+
id="dateinput_01p06-month"
62+
name="dateinput_01p06-month"
63+
pattern="[0-9]*"
64+
type="number"
65+
/>
66+
</div>
67+
</div>
68+
</DateInputInput>
69+
</DateInputMonth>
70+
<DateInputYear>
71+
<DateInputInput
72+
dateInputType="Year"
73+
pattern="[0-9]*"
74+
type="number"
75+
>
76+
<div
77+
className="nhsuk-date-input__item"
78+
>
79+
<div
80+
className="nhsuk-form-group"
81+
>
82+
<label
83+
className="nhsuk-label nhsuk-date-input__label"
84+
htmlFor="dateinput_01p06-year"
85+
>
86+
Year
87+
</label>
88+
<input
89+
aria-label="dateinput_01p06-year input"
90+
className="nhsuk-input nhsuk-date-input__input nhsuk-input--width-4"
91+
id="dateinput_01p06-year"
92+
name="dateinput_01p06-year"
93+
pattern="[0-9]*"
94+
type="number"
95+
/>
96+
</div>
97+
</div>
98+
</DateInputInput>
99+
</DateInputYear>
100+
</div>
101+
</DateInput>
102+
`;

0 commit comments

Comments
 (0)