-
Notifications
You must be signed in to change notification settings - Fork 5
/
DateTimeRange.spec.js
145 lines (131 loc) · 4.79 KB
/
DateTimeRange.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { expect } from 'chai';
import { shallow } from 'enzyme';
import React from 'react';
import simple from 'simple-mock';
import DatePicker from 'shared/date-picker';
import DateTimeRange from './DateTimeRange';
import Field from './Field';
import Time from './Time';
const defaults = {
controlProps: {
onChange: () => null,
value: { begin: {}, end: {} },
},
id: '1234',
noLabels: false,
};
function getWrapper(props) {
return shallow(<DateTimeRange {...defaults} {...props} />);
}
describe('shared/form-fields/DateTimeRange', () => {
it('renders a div.date-time-range-field', () => {
const wrapper = getWrapper().find('div.date-time-range-field');
expect(wrapper).to.have.length(1);
});
it('renders a div.date-time-range-field-container', () => {
const wrapper = getWrapper();
expect(wrapper.is('div.date-time-range-field-container')).to.be.true;
});
it('renders correct fields', () => {
const fields = getWrapper().find(Field);
expect(fields).to.have.length(3);
expect(fields.at(0).prop('componentClass')).to.equal(DatePicker);
expect(fields.at(1).prop('componentClass')).to.equal(Time);
expect(fields.at(2).prop('componentClass')).to.equal(Time);
});
it('does not render datepicker if specified', () => {
const fields = getWrapper({
controlProps: {
...defaults.controlProps,
renderDatePicker: false,
},
}).find(Field);
expect(fields).to.have.length(2);
expect(fields.at(0).prop('componentClass')).to.equal(Time);
expect(fields.at(1).prop('componentClass')).to.equal(Time);
});
it('renders labels only when noLabels = false', () => {
const fields = getWrapper().find(Field);
expect(fields.at(0).prop('label')).to.equal('Päivä');
expect(fields.at(1).prop('label')).to.equal('Alkaa');
expect(fields.at(2).prop('label')).to.equal('Päättyy');
const fieldsWithoutLabels = getWrapper({ noLabels: true }).find(Field);
expect(fieldsWithoutLabels.at(0).prop('label')).to.equal('');
expect(fieldsWithoutLabels.at(1).prop('label')).to.equal('');
expect(fieldsWithoutLabels.at(2).prop('label')).to.equal('');
});
describe('handleDateChange', () => {
it('updates begin.date and end.date', () => {
const date = 'date2';
const onChange = simple.mock();
const value = {
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time: 'time2' },
};
const wrapper = getWrapper({ controlProps: { onChange, value } });
wrapper.instance().handleDateChange(date);
expect(onChange.callCount).to.equal(1);
expect(onChange.lastCall.args).to.deep.equal([{
begin: { date, time: 'time1' },
end: { date, time: 'time2' },
}]);
});
});
describe('handleStartTimeChange', () => {
it('updates begin.time', () => {
const time = 'new-start-time';
const onChange = simple.mock();
const value = {
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time: 'time2' },
};
const wrapper = getWrapper({ controlProps: { onChange, value } });
wrapper.instance().handleStartTimeChange(time);
expect(onChange.callCount).to.equal(1);
expect(onChange.lastCall.args).to.deep.equal([{
begin: { date: 'date1', time },
end: { date: 'date1', time: 'time2' },
}]);
});
});
describe('handleEndTimeChange', () => {
it('updates end.time', () => {
const time = 'new-start-time';
const onChange = simple.mock();
const value = {
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time: 'time2' },
};
const wrapper = getWrapper({ controlProps: { onChange, value } });
wrapper.instance().handleEndTimeChange(time);
expect(onChange.callCount).to.equal(1);
expect(onChange.lastCall.args).to.deep.equal([{
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time },
}]);
});
});
describe('handleBlur', () => {
it('works if no props.controlProps.onBlur', () => {
const onChange = simple.mock();
const value = {
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time: 'time2' },
};
const wrapper = getWrapper({ controlProps: { onChange, value, onBlur: null } });
wrapper.instance().handleBlur();
});
it('calls props.controlProps.onBlur', () => {
const onBlur = simple.mock();
const onChange = simple.mock();
const value = {
begin: { date: 'date1', time: 'time1' },
end: { date: 'date1', time: 'time2' },
};
const wrapper = getWrapper({ controlProps: { onChange, value, onBlur } });
wrapper.instance().handleBlur();
expect(onBlur.callCount).to.equal(1);
expect(onBlur.lastCall.args).to.deep.equal([value]);
});
});
});