Debian Codegraph @

packages/controls/test/src/widget_date_test.ts

raw
1 import { DummyManager } from './dummy-manager';
2
3 import { expect } from 'chai';
4
5 import * as widgets from '../../lib';
6
7 function getDatepicker(parent: Element): HTMLInputElement {
8 const elem = parent.querySelector('input[type="date"]');
9 return elem as HTMLInputElement;
10 }
11
12 describe('DatePickerView', function () {
13 beforeEach(async function () {
14 this.manager = new DummyManager();
15 const modelId = 'u-u-i-d';
16 this.model = await this.manager.new_model(
17 {
18 model_name: 'DatePickerModel',
19 model_module: '@jupyter-widgets/controls',
20 model_module_version: '1.0.0',
21 model_id: modelId,
22 },
23 { description: 'test-date-model' }
24 );
25 });
26
27 it('construction', function () {
28 const options = { model: this.model };
29 const view = new widgets.DatePickerView(options);
30 expect(view).to.not.be.undefined;
31 });
32
33 it('initial date value', function () {
34 const testDate = new Date('2017-02-25');
35 this.model.set('value', testDate);
36 const options = { model: this.model };
37 const view = new widgets.DatePickerView(options);
38 view.render();
39 expect(getDatepicker(view.el).valueAsDate!.getTime()).to.equal(
40 testDate.getTime()
41 );
42 });
43
44 it('no initial value', function () {
45 this.model.set('value', null);
46 const options = { model: this.model };
47 const view = new widgets.DatePickerView(options);
48 view.render();
49 expect(getDatepicker(view.el).valueAsDate).to.be.a('null');
50 });
51
52 it('set the model date', function () {
53 this.model.set('value', null);
54 const options = { model: this.model };
55 const view = new widgets.DatePickerView(options);
56 view.render();
57
58 // Simulate setting the date in the datepicker
59 const testDate = new Date('2015-02-22');
60 const datepicker = getDatepicker(view.el);
61 datepicker.valueAsDate = testDate;
62 datepicker.dispatchEvent(new Event('change', { bubbles: true }));
63
64 expect(this.model.get('value').getTime()).to.equal(testDate.getTime());
65 });
66
67 it('update when the model changes', function () {
68 this.model.set('value', null);
69 const options = { model: this.model };
70 const view = new widgets.DatePickerView(options);
71 view.render();
72
73 const testDate = new Date('2015-02-22');
74 this.model.set('value', testDate);
75 const datepicker = getDatepicker(view.el);
76 expect(datepicker.valueAsDate!.getTime()).to.equal(testDate.getTime());
77 });
78 });
79
80 describe('serialize_date', function () {
81 it('null date', function () {
82 expect(widgets.serialize_date(null)).to.be.a('null');
83 });
84
85 it('UTC date', function () {
86 const date = new Date('Sat May 13 2017 00:00:00 UTC');
87 const expectedSerialization = {
88 year: 2017,
89 month: 4,
90 date: 13,
91 };
92 expect(widgets.serialize_date(date)).to.deep.equal(expectedSerialization);
93 });
94
95 it('date in other locale as UTC', function () {
96 const date = new Date('Sat May 13 2017 00:00:00 GMT+0100 (BST)');
97 const expectedSerialization = {
98 year: 2017,
99 month: 4,
100 date: 12, // still on 12th May in UTC
101 };
102 expect(widgets.serialize_date(date)).to.deep.equal(expectedSerialization);
103 });
104
105 it('date before 100AD', function () {
106 const date = new Date('0005-04-28');
107 const expectedSerialization = {
108 year: 5,
109 month: 3,
110 date: 28,
111 };
112 expect(widgets.serialize_date(date)).to.deep.equal(expectedSerialization);
113 });
114 });
115
116 describe('deserialize_date', function () {
117 it('null date', function () {
118 expect(widgets.deserialize_date(null)).to.be.a('null');
119 });
120
121 it('valid date', function () {
122 const serialized = {
123 year: 2017,
124 month: 4,
125 date: 12,
126 };
127 const expectedDate = new Date('2017-05-12');
128 expect(widgets.deserialize_date(serialized)!.getTime()).to.equal(
129 expectedDate.getTime()
130 );
131 });
132 });