summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/components/datepicker_tests.js
blob: 643bcdb5188bc05c9d75ff7dee2db6cad70ec879 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
odoo.define('web.datepicker_tests', function (require) {
    "use strict";

    const { DatePicker, DateTimePicker } = require('web.DatePickerOwl');
    const testUtils = require('web.test_utils');
    const time = require('web.time');

    const { createComponent } = testUtils;

    QUnit.module('Components', {}, function () {

        QUnit.module('DatePicker');

        QUnit.test("basic rendering", async function (assert) {
            assert.expect(8);

            const picker = await createComponent(DatePicker, {
                props: { date: moment('01/09/1997') },
            });

            assert.containsOnce(picker, 'input.o_input.o_datepicker_input');
            assert.containsOnce(picker, 'span.o_datepicker_button');
            assert.containsNone(document.body, 'div.bootstrap-datetimepicker-widget');

            const input = picker.el.querySelector('input.o_input.o_datepicker_input');
            assert.strictEqual(input.value, '01/09/1997',
                "Value should be the one given")
                ;
            assert.strictEqual(input.dataset.target, `#${picker.el.id}`,
                "DatePicker id should match its input target");

            await testUtils.dom.click(input);

            assert.containsOnce(document.body, 'div.bootstrap-datetimepicker-widget .datepicker');
            assert.containsNone(document.body, 'div.bootstrap-datetimepicker-widget .timepicker');
            assert.strictEqual(
                document.querySelector('.datepicker .day.active').dataset.day,
                '01/09/1997',
                "Datepicker should have set the correct day"
            );

            picker.destroy();
        });

        QUnit.test("pick a date", async function (assert) {
            assert.expect(5);

            const picker = await createComponent(DatePicker, {
                props: { date: moment('01/09/1997') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY'), '02/08/1997',
                            "Event should transmit the correct date");
                    },
                }
            });
            const input = picker.el.querySelector('.o_datepicker_input');

            await testUtils.dom.click(input);
            await testUtils.dom.click(document.querySelector('.datepicker th.next')); // next month

            assert.verifySteps([]);

            await testUtils.dom.click(document.querySelectorAll('.datepicker table td')[15]); // previous day

            assert.strictEqual(input.value, '02/08/1997');
            assert.verifySteps(['datetime-changed']);

            picker.destroy();
        });

        QUnit.test("pick a date with locale", async function (assert) {
            assert.expect(4);

            // weird shit of moment https://github.com/moment/moment/issues/5600
            // When month regex returns undefined, january is taken (first month of the default "nameless" locale)
            const originalLocale = moment.locale();
            // Those parameters will make Moment's internal compute stuff that are relevant to the bug
            const months = 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_');
            const monthsShort = 'janv._févr._mars_avr._mai_juin_juil._août_custSept._oct._nov._déc.'.split('_');
            moment.defineLocale('frenchForTests', { months, monthsShort, code: 'frTest' , monthsParseExact: true});

            const hasChanged = testUtils.makeTestPromise();
            const picker = await createComponent(DatePicker, {
                translateParameters: {
                    date_format: "%d %b, %Y", // Those are important too
                    time_format: "%H:%M:%S",
                },
                props: { date: moment('09/01/1997', 'MM/DD/YYYY') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY'), '09/02/1997',
                            "Event should transmit the correct date");
                        hasChanged.resolve();
                    },
                }
            });
            const input = picker.el.querySelector('.o_datepicker_input');
            await testUtils.dom.click(input);

            await testUtils.dom.click(document.querySelectorAll('.datepicker table td')[3]); // next day

            assert.strictEqual(input.value, '02 custSept., 1997');
            assert.verifySteps(['datetime-changed']);

            moment.locale(originalLocale);
            moment.updateLocale('englishForTest', null);

            picker.destroy();
        });

        QUnit.test("enter a date value", async function (assert) {
            assert.expect(5);

            const picker = await createComponent(DatePicker, {
                props: { date: moment('01/09/1997') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY'), '02/08/1997',
                            "Event should transmit the correct date");
                    },
                }
            });
            const input = picker.el.querySelector('.o_datepicker_input');

            assert.verifySteps([]);

            await testUtils.fields.editAndTrigger(input, '02/08/1997', ['change']);

            assert.verifySteps(['datetime-changed']);

            await testUtils.dom.click(input);

            assert.strictEqual(
                document.querySelector('.datepicker .day.active').dataset.day,
                '02/08/1997',
                "Datepicker should have set the correct day"
            );

            picker.destroy();
        });

        QUnit.test("Date format is correctly set", async function (assert) {
            assert.expect(2);

            testUtils.patch(time, { getLangDateFormat: () => "YYYY/MM/DD" });
            const picker = await createComponent(DatePicker, {
                props: { date: moment('01/09/1997') },
            });
            const input = picker.el.querySelector('.o_datepicker_input');

            assert.strictEqual(input.value, '1997/01/09');

            // Forces an update to assert that the registered format is the correct one
            await testUtils.dom.click(input);

            assert.strictEqual(input.value, '1997/01/09');

            picker.destroy();
            testUtils.unpatch(time);
        });

        QUnit.module('DateTimePicker');

        QUnit.test("basic rendering", async function (assert) {
            assert.expect(11);

            const picker = await createComponent(DateTimePicker, {
                props: { date: moment('01/09/1997 12:30:01') },
            });

            assert.containsOnce(picker, 'input.o_input.o_datepicker_input');
            assert.containsOnce(picker, 'span.o_datepicker_button');
            assert.containsNone(document.body, 'div.bootstrap-datetimepicker-widget');

            const input = picker.el.querySelector('input.o_input.o_datepicker_input');
            assert.strictEqual(input.value, '01/09/1997 12:30:01', "Value should be the one given");
            assert.strictEqual(input.dataset.target, `#${picker.el.id}`,
                "DateTimePicker id should match its input target");

            await testUtils.dom.click(input);

            assert.containsOnce(document.body, 'div.bootstrap-datetimepicker-widget .datepicker');
            assert.containsOnce(document.body, 'div.bootstrap-datetimepicker-widget .timepicker');
            assert.strictEqual(
                document.querySelector('.datepicker .day.active').dataset.day,
                '01/09/1997',
                "Datepicker should have set the correct day");

            assert.strictEqual(document.querySelector('.timepicker .timepicker-hour').innerText.trim(), '12',
                "Datepicker should have set the correct hour");
            assert.strictEqual(document.querySelector('.timepicker .timepicker-minute').innerText.trim(), '30',
                "Datepicker should have set the correct minute");
            assert.strictEqual(document.querySelector('.timepicker .timepicker-second').innerText.trim(), '01',
                "Datepicker should have set the correct second");

            picker.destroy();
        });

        QUnit.test("pick a date and time", async function (assert) {
            assert.expect(5);

            const picker = await createComponent(DateTimePicker, {
                props: { date: moment('01/09/1997 12:30:01') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY HH:mm:ss'), '02/08/1997 15:45:05',
                            "Event should transmit the correct date");
                    },
                }
            });
            const input = picker.el.querySelector('input.o_input.o_datepicker_input');

            await testUtils.dom.click(input);
            await testUtils.dom.click(document.querySelector('.datepicker th.next')); // February
            await testUtils.dom.click(document.querySelectorAll('.datepicker table td')[15]); // 08
            await testUtils.dom.click(document.querySelector('a[title="Select Time"]'));
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-hour'));
            await testUtils.dom.click(document.querySelectorAll('.timepicker .hour')[15]); // 15h
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-minute'));
            await testUtils.dom.click(document.querySelectorAll('.timepicker .minute')[9]); // 45m
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-second'));

            assert.verifySteps([]);

            await testUtils.dom.click(document.querySelectorAll('.timepicker .second')[1]); // 05s

            assert.strictEqual(input.value, '02/08/1997 15:45:05');
            assert.verifySteps(['datetime-changed']);

            picker.destroy();
        });

        QUnit.test("pick a date and time with locale", async function (assert) {
            assert.expect(5);

            // weird shit of moment https://github.com/moment/moment/issues/5600
            // When month regex returns undefined, january is taken (first month of the default "nameless" locale)
            const originalLocale = moment.locale();
            // Those parameters will make Moment's internal compute stuff that are relevant to the bug
            const months = 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_');
            const monthsShort = 'janv._févr._mars_avr._mai_juin_juil._août_custSept._oct._nov._déc.'.split('_');
            moment.defineLocale('frenchForTests', { months, monthsShort, code: 'frTest' , monthsParseExact: true});

            const hasChanged = testUtils.makeTestPromise();
            const picker = await createComponent(DateTimePicker, {
                translateParameters: {
                    date_format: "%d %b, %Y", // Those are important too
                    time_format: "%H:%M:%S",
                },
                props: { date: moment('09/01/1997 12:30:01', 'MM/DD/YYYY HH:mm:ss') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY HH:mm:ss'), '09/02/1997 15:45:05',
                            "Event should transmit the correct date");
                        hasChanged.resolve();
                    },
                }
            });

            const input = picker.el.querySelector('input.o_input.o_datepicker_input');

            await testUtils.dom.click(input);
            await testUtils.dom.click(document.querySelectorAll('.datepicker table td')[3]); // next day
            await testUtils.dom.click(document.querySelector('a[title="Select Time"]'));
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-hour'));
            await testUtils.dom.click(document.querySelectorAll('.timepicker .hour')[15]); // 15h
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-minute'));
            await testUtils.dom.click(document.querySelectorAll('.timepicker .minute')[9]); // 45m
            await testUtils.dom.click(document.querySelector('.timepicker .timepicker-second'));

            assert.verifySteps([]);
            await testUtils.dom.click(document.querySelectorAll('.timepicker .second')[1]); // 05s

            assert.strictEqual(input.value, '02 custSept., 1997 15:45:05');
            assert.verifySteps(['datetime-changed']);

            await hasChanged;

            moment.locale(originalLocale);
            moment.updateLocale('frenchForTests', null);

            picker.destroy();
        });

        QUnit.test("enter a datetime value", async function (assert) {
            assert.expect(9);

            const picker = await createComponent(DateTimePicker, {
                props: { date: moment('01/09/1997 12:30:01') },
                intercepts: {
                    'datetime-changed': ev => {
                        assert.step('datetime-changed');
                        assert.strictEqual(ev.detail.date.format('MM/DD/YYYY HH:mm:ss'), '02/08/1997 15:45:05',
                            "Event should transmit the correct date");
                    },
                }
            });
            const input = picker.el.querySelector('.o_datepicker_input');

            assert.verifySteps([]);

            await testUtils.fields.editAndTrigger(input, '02/08/1997 15:45:05', ['change']);

            assert.verifySteps(['datetime-changed']);

            await testUtils.dom.click(input);

            assert.strictEqual(input.value, '02/08/1997 15:45:05');
            assert.strictEqual(
                document.querySelector('.datepicker .day.active').dataset.day,
                '02/08/1997',
                "Datepicker should have set the correct day"
            );
            assert.strictEqual(document.querySelector('.timepicker .timepicker-hour').innerText.trim(), '15',
                "Datepicker should have set the correct hour");
            assert.strictEqual(document.querySelector('.timepicker .timepicker-minute').innerText.trim(), '45',
                "Datepicker should have set the correct minute");
            assert.strictEqual(document.querySelector('.timepicker .timepicker-second').innerText.trim(), '05',
                "Datepicker should have set the correct second");

            picker.destroy();
        });

        QUnit.test("Date time format is correctly set", async function (assert) {
            assert.expect(2);

            testUtils.patch(time, { getLangDatetimeFormat: () => "hh:mm:ss YYYY/MM/DD" });
            const picker = await createComponent(DateTimePicker, {
                props: { date: moment('01/09/1997 12:30:01') },
            });
            const input = picker.el.querySelector('.o_datepicker_input');

            assert.strictEqual(input.value, '12:30:01 1997/01/09');

            // Forces an update to assert that the registered format is the correct one
            await testUtils.dom.click(input);

            assert.strictEqual(input.value, '12:30:01 1997/01/09');

            picker.destroy();
            testUtils.unpatch(time);
        });
    });
});