Skip to content

Commit 9cca697

Browse files
test: extract time-picker i18n tests into separate file (#12487) (#12488)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 1b6cf4a commit 9cca697

2 files changed

Lines changed: 113 additions & 94 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { expect } from '@vaadin/chai-plugins';
2+
import { enter, fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
4+
import '../src/vaadin-time-picker.js';
5+
import { setInputValue, strictAmPmI18n } from './helpers.js';
6+
7+
describe('i18n', () => {
8+
let timePicker, inputElement;
9+
10+
beforeEach(async () => {
11+
timePicker = fixtureSync(`<vaadin-time-picker></vaadin-time-picker>`);
12+
await nextRender();
13+
inputElement = timePicker.inputElement;
14+
});
15+
16+
it('should fallback to default functions if none are provided', () => {
17+
timePicker.i18n = {};
18+
19+
timePicker.value = '12:00';
20+
expect(inputElement.value).to.equal('12:00');
21+
expect(timePicker.value).to.equal('12:00');
22+
});
23+
24+
describe('parseTime', () => {
25+
it('should use custom parser if that exists', () => {
26+
timePicker.i18n = { parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }) };
27+
timePicker.value = '12';
28+
expect(timePicker.i18n.parseTime.args[0][0]).to.be.equal('12:00');
29+
expect(timePicker.value).to.be.equal('12:00');
30+
});
31+
32+
it('should commit the value when the custom parser returns stripped seconds', () => {
33+
// The step defaults to minute precision, so the seconds are stripped
34+
// from the value, while the custom parser keeps returning them.
35+
timePicker.i18n = { parseTime: () => ({ hours: 12, minutes: 0, seconds: 0 }) };
36+
setInputValue(timePicker, 'noon');
37+
enter(inputElement);
38+
expect(timePicker.value).to.be.equal('12:00');
39+
});
40+
41+
it('should not modify the object returned by the custom parser', () => {
42+
const parsed = { hours: 8, minutes: 0, seconds: 0, milliseconds: 0 };
43+
timePicker.i18n = { formatTime: strictAmPmI18n.formatTime, parseTime: () => parsed };
44+
setInputValue(timePicker, '8:00 AM');
45+
enter(inputElement);
46+
expect(parsed).to.deep.equal({ hours: 8, minutes: 0, seconds: 0, milliseconds: 0 });
47+
});
48+
49+
it('should not fail when the custom parser returns a frozen object', () => {
50+
timePicker.i18n = {
51+
formatTime: strictAmPmI18n.formatTime,
52+
parseTime: () => Object.freeze({ hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }),
53+
};
54+
setInputValue(timePicker, '8:00 AM');
55+
enter(inputElement);
56+
expect(timePicker.value).to.be.equal('08:00');
57+
});
58+
});
59+
60+
describe('formatTime', () => {
61+
it('should use custom formatter if that exists', () => {
62+
timePicker.i18n = {
63+
formatTime: sinon.stub().withArgs({ hours: 12, minutes: 0 }).returns('12:00 AM'),
64+
parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }),
65+
};
66+
timePicker.value = '12';
67+
expect(timePicker.value).to.be.equal('12:00');
68+
expect(inputElement.value).to.be.equal('12:00 AM');
69+
});
70+
71+
it('should accept custom time formatter', () => {
72+
timePicker.i18n = {
73+
formatTime: sinon.stub().returns('1200'),
74+
parseTime: sinon.stub().withArgs('1200').returns({ hours: 12, minutes: 0 }),
75+
};
76+
timePicker.value = '12:00';
77+
expect(inputElement.value).to.equal('1200');
78+
expect(timePicker.value).to.equal('12:00');
79+
});
80+
});
81+
82+
describe('reassigned', () => {
83+
it('should align values of dropdown and input when i18n was reassigned', () => {
84+
timePicker.value = '12';
85+
timePicker.i18n = {
86+
formatTime: sinon.stub().withArgs({ hours: 12, minutes: 0 }).returns('12:00 AM'),
87+
parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }),
88+
};
89+
expect(inputElement.value).to.be.equal('12:00 AM');
90+
expect(timePicker.value).to.be.equal('12:00');
91+
});
92+
93+
it('should keep the value when a custom i18n is set after the value', () => {
94+
timePicker.value = '08:00';
95+
timePicker.i18n = strictAmPmI18n;
96+
expect(timePicker.value).to.be.equal('08:00');
97+
expect(inputElement.value).to.be.equal('8:00 AM');
98+
});
99+
100+
['min', 'max', 'step'].forEach((property) => {
101+
const value = property === 'step' ? 1800 : property === 'min' ? '01:00' : '23:00';
102+
103+
it(`should keep the value on ${property} change with a custom i18n`, () => {
104+
timePicker.i18n = strictAmPmI18n;
105+
timePicker.value = '08:00';
106+
timePicker[property] = value;
107+
expect(timePicker.value).to.be.equal('08:00');
108+
expect(inputElement.value).to.be.equal('8:00 AM');
109+
});
110+
});
111+
});
112+
});

packages/time-picker/test/time-picker.test.js

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { enter, fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpe
33
import sinon from 'sinon';
44
import '../src/vaadin-time-picker.js';
55
import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
6-
import { setInputValue, strictAmPmI18n } from './helpers.js';
6+
import { setInputValue } from './helpers.js';
77

88
describe('time-picker', () => {
99
let timePicker, inputElement;
@@ -350,99 +350,6 @@ describe('time-picker', () => {
350350
});
351351
});
352352

353-
describe('custom functions', () => {
354-
it('should use custom parser if that exists', () => {
355-
timePicker.i18n = { parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }) };
356-
timePicker.value = '12';
357-
expect(timePicker.i18n.parseTime.args[0][0]).to.be.equal('12:00');
358-
expect(timePicker.value).to.be.equal('12:00');
359-
});
360-
361-
it('should align values of dropdown and input when i18n was reassigned', () => {
362-
timePicker.value = '12';
363-
timePicker.i18n = {
364-
formatTime: sinon.stub().withArgs({ hours: 12, minutes: 0 }).returns('12:00 AM'),
365-
parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }),
366-
};
367-
expect(inputElement.value).to.be.equal('12:00 AM');
368-
expect(timePicker.value).to.be.equal('12:00');
369-
});
370-
371-
it('should use custom formatter if that exists', () => {
372-
timePicker.i18n = {
373-
formatTime: sinon.stub().withArgs({ hours: 12, minutes: 0 }).returns('12:00 AM'),
374-
parseTime: sinon.stub().returns({ hours: 12, minutes: 0, seconds: 0 }),
375-
};
376-
timePicker.value = '12';
377-
expect(timePicker.value).to.be.equal('12:00');
378-
expect(inputElement.value).to.be.equal('12:00 AM');
379-
});
380-
381-
it('should accept custom time formatter', () => {
382-
timePicker.i18n = {
383-
formatTime: sinon.stub().returns('1200'),
384-
parseTime: sinon.stub().withArgs('1200').returns({ hours: 12, minutes: 0 }),
385-
};
386-
timePicker.value = '12:00';
387-
expect(inputElement.value).to.equal('1200');
388-
expect(timePicker.value).to.equal('12:00');
389-
});
390-
391-
it('should fallback to default functions if none are provided', () => {
392-
timePicker.i18n = {};
393-
394-
timePicker.value = '12:00';
395-
expect(inputElement.value).to.equal('12:00');
396-
expect(timePicker.value).to.equal('12:00');
397-
});
398-
399-
it('should keep the value when a custom i18n is set after the value', () => {
400-
timePicker.value = '08:00';
401-
timePicker.i18n = strictAmPmI18n;
402-
expect(timePicker.value).to.be.equal('08:00');
403-
expect(inputElement.value).to.be.equal('8:00 AM');
404-
});
405-
406-
['min', 'max', 'step'].forEach((property) => {
407-
const value = property === 'step' ? 1800 : property === 'min' ? '01:00' : '23:00';
408-
409-
it(`should keep the value on ${property} change with a custom i18n`, () => {
410-
timePicker.i18n = strictAmPmI18n;
411-
timePicker.value = '08:00';
412-
timePicker[property] = value;
413-
expect(timePicker.value).to.be.equal('08:00');
414-
expect(inputElement.value).to.be.equal('8:00 AM');
415-
});
416-
});
417-
418-
it('should commit the value when the custom parser returns stripped seconds', () => {
419-
// The step defaults to minute precision, so the seconds are stripped
420-
// from the value, while the custom parser keeps returning them.
421-
timePicker.i18n = { parseTime: () => ({ hours: 12, minutes: 0, seconds: 0 }) };
422-
setInputValue(timePicker, 'noon');
423-
enter(inputElement);
424-
expect(timePicker.value).to.be.equal('12:00');
425-
});
426-
427-
it('should not modify the object returned by the custom parser', () => {
428-
const parsed = { hours: 8, minutes: 0, seconds: 0, milliseconds: 0 };
429-
timePicker.i18n = { formatTime: strictAmPmI18n.formatTime, parseTime: () => parsed };
430-
setInputValue(timePicker, '8:00 AM');
431-
enter(inputElement);
432-
expect(parsed).to.deep.equal({ hours: 8, minutes: 0, seconds: 0, milliseconds: 0 });
433-
});
434-
435-
it('should not fail when the custom parser returns a frozen object', () => {
436-
timePicker.i18n = {
437-
formatTime: strictAmPmI18n.formatTime,
438-
parseTime: () => Object.freeze({ hours: 8, minutes: 0, seconds: 0, milliseconds: 0 }),
439-
};
440-
setInputValue(timePicker, '8:00 AM');
441-
enter(inputElement);
442-
expect(timePicker.value).to.be.equal('08:00');
443-
});
444-
});
445-
446353
describe('helper text', () => {
447354
it('should set helper text content using helperText property', async () => {
448355
timePicker.helperText = 'foo';

0 commit comments

Comments
 (0)