/
TimePicker.test.js
250 lines (204 loc) · 8.12 KB
/
TimePicker.test.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import React from 'react';
import { mount } from 'enzyme';
import TimePicker, {
parseInTimeFormat, H24, H12, toOption, filterTime, createTimeList, makeOptions, inputError
} from '../../src/time-picker/TimePicker';
const exampleProps = {
...TimePicker.defaultProps,
id: '12345',
className: 'fancy-class-name',
placeholder: '--:--',
timeFormat: '24h',
style: { margin: 10, position: 'relative' },
value: { hours: 6, minutes: 30 },
minTime: { hours: 5, minutes: 10 },
maxTime: { hours: 8, minutes: 20 },
onChange: () => {}
};
const timePicker = new TimePicker(exampleProps);
describe('TimePicker', () => {
it('renders correctly', () => {
const component = mount(
<TimePicker {...exampleProps} />
);
expect(component.html()).toMatchSnapshot();
});
describe('getLocals', () => {
it('computes className', () => {
const { className } = timePicker.getLocals();
expect(className).toContain('time-picker');
expect(className).toContain('fancy-class-name');
});
it('passes value properly formatted', () => {
const timePicker = new TimePicker({
...TimePicker.defaultProps,
value: { hours: 15, minutes: 33 }
});
const { value } = timePicker.getLocals();
expect(value).toBe('15:33');
});
it('passes an undefined value when value prop is undefined', () => {
const timePicker = new TimePicker({ ...TimePicker.defaultProps });
const { value } = timePicker.getLocals();
expect(value).toBeUndefined();
});
});
describe('parseInTimeFormat', () => {
it('parses input and returns a valid 24h time', () => {
const input = '13:59';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput.originalInput).toBe(input);
expect(parsedInput.hours).toBe(13);
expect(parsedInput.minutes).toBe(59);
});
it('parses input and returns a valid 12h time', () => {
const input = '06:30';
const parsedInput = parseInTimeFormat(input, H12);
expect(parsedInput.originalInput).toBe(input);
expect(parsedInput.hours).toBe(6);
expect(parsedInput.minutes).toBe(30);
});
it('parses input and returns an error for invalid 24h time', () => {
const input = '36:30';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput).toBe(inputError);
});
it('parses input and returns an error for invalid 12h time', () => {
const input = '18:30';
const parsedInput = parseInTimeFormat(input, H12);
expect(parsedInput).toBe(inputError);
});
it('parses empty input', () => {
const input = '';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput.originalInput).toBe(input);
expect(parsedInput.hours).toBeUndefined();
expect(parsedInput.minutes).toBeUndefined();
});
it('parses input when minutes are not set', () => {
const input = '10:';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput.originalInput).toBe(input);
expect(parsedInput.hours).toBeUndefined();
expect(parsedInput.minutes).toBeUndefined();
});
it('parses input without separator and return a valid time', () => {
const input = '1231';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput.hours).toBe(12);
expect(parsedInput.minutes).toBe(31);
expect(parsedInput.originalInput).toBe('12:31');
});
it('parses input and return error if string is not well formed', () => {
const input = '1a:aa';
const parsedInput = parseInTimeFormat(input, H24);
expect(parsedInput).toBe(inputError);
});
});
describe('createTimeList', () => {
it('returns all the available times', () => {
const time = { };
const timeList24 = createTimeList(time, H24);
expect(timeList24.length).toBe(48);
expect(timeList24[0].hours).toBe(0);
expect(timeList24[0].minutes).toBe(0);
expect(timeList24[0].timeFormat).toBe('24h');
expect(timeList24[26].hours).toBe(13);
expect(timeList24[26].minutes).toBe(0);
expect(timeList24[27].hours).toBe(13);
expect(timeList24[27].minutes).toBe(30);
const timeList12 = createTimeList(time, H12);
expect(timeList24).not.toEqual(timeList12);
expect(timeList12[0].timeFormat).toBe('12h');
});
it('returns an array of a single element for 24h', () => {
const time = { hours: 10, minutes: 31 };
const timeList = createTimeList(time, H24);
expect(timeList.length).toBe(1);
expect(timeList[0]).toEqual({ hours: 10, minutes: 31, timeFormat: H24 });
});
it('returns an array of 2 elements for 12h', () => {
const time = { hours: 10, minutes: 31 };
const timeList = createTimeList(time, H12);
expect(timeList.length).toBe(2);
expect(timeList[0]).toEqual({ hours: 10, minutes: 31, timeFormat: H12 });
expect(timeList[1]).toEqual({ hours: 22, minutes: 31, timeFormat: H12 });
});
});
describe('filterTime', () => {
it('filters based on minTime/maxTime ', () => {
const minTime = { hours: 8, minutes: 0 };
const maxTime = { hours: 9, minutes: 0 };
const timeList = [
{ hours: 7, minutes: 22 },
{ hours: 8, minutes: 33 },
{ hours: 9, minutes: 44 }
];
const timeListFilterd = timeList.filter(filterTime({ originalInput: '', minTime, maxTime }));
expect(timeListFilterd.length).toBe(1);
expect(timeListFilterd[0]).toEqual({ hours: 8, minutes: 33 });
});
it('filters based on original input', () => {
const timeList = [
{ hours: 7, minutes: 22 },
{ hours: 8, minutes: 33 },
{ hours: 9, minutes: 44 }
];
const timeListFilterd = timeList.filter(filterTime({
originalInput: '08', minTime: TimePicker.defaultProps.minTime, maxTime: TimePicker.defaultProps.maxTime
}));
expect(timeListFilterd.length).toBe(1);
expect(timeListFilterd[0]).toEqual({ hours: 8, minutes: 33 });
});
});
describe('toOption', () => {
it('formats into a valid option for 24H time', () => {
const time = { hours: 10, minutes: 20 };
const formattedTime = toOption(time, H24);
expect(formattedTime.label).toBe('10:20');
});
describe('12H time', () => {
it('works 1', () => {
const time = { hours: 13, minutes: 20, timeFormat: H12 };
const formattedTime = toOption(time);
expect(formattedTime.label).toBe('01:20 pm');
});
it('works 2', () => {
const time = { hours: 10, minutes: 20, timeFormat: H12 };
const formattedTime = toOption(time);
expect(formattedTime.label).toBe('10:20 am');
});
it('works 3', () => {
const time = { hours: 12, minutes: 34, timeFormat: H12 };
const formattedTime = toOption(time);
expect(formattedTime.label).toBe('12:34 pm');
});
it('works 4', () => {
const time = { hours: 0, minutes: 34, timeFormat: H12 };
const formattedTime = toOption(time);
expect(formattedTime.label).toBe('12:34 am');
});
it('works 5', () => {
const time = { hours: 24, minutes: 34, timeFormat: H12 };
const formattedTime = toOption(time);
expect(formattedTime.label).toBe('12:34 am');
});
});
});
describe('makeOptions', () => {
it('computes options based on min/max for 24H time', () => {
const inputStr = '';
const results = makeOptions({ timeFormat: H24, minTime: { hours: 5, minutes: 10 }, maxTime: { hours: 7, minutes: 20 } }, inputStr);
expect(results.length).toBe(4);
expect(results[0]).toEqual({ label: '05:30', value: '05:30' });
expect(results[3]).toEqual({ label: '07:00', value: '07:00' });
});
it('computes options based on min/max for 12H time', () => {
const inputStr = '';
const results = makeOptions({ timeFormat: H12, minTime: { hours: 5, minutes: 10 }, maxTime: { hours: 7, minutes: 20 } }, inputStr);
expect(results.length).toBe(4);
expect(results[0]).toEqual({ label: '05:30 am', value: '05:30' });
expect(results[3]).toEqual({ label: '07:00 am', value: '07:00' });
});
});
});