-
Notifications
You must be signed in to change notification settings - Fork 78
/
timestamp.spec.ts
454 lines (380 loc) · 15 KB
/
timestamp.spec.ts
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* @license
* Copyright 2019 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// tslint:disable no-lifecycle-call no-use-before-declare no-magic-numbers
// tslint:disable no-any max-file-line-count no-unbound-method use-component-selector
import {
BACKSPACE,
DELETE,
DOWN_ARROW,
END,
HOME,
LEFT_ARROW,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
UP_ARROW,
} from '@angular/cdk/keycodes';
import { Component, DebugElement, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {
DtChartModule,
DtChartTimestamp,
} from '@dynatrace/barista-components/chart';
import {
dispatchEvent,
dispatchKeyboardEvent,
createKeyboardEvent,
createComponent,
} from '@dynatrace/barista-components/testing';
import {
ARIA_DEFAULT_SELECTED_LABEL,
TimestampStateChangedEvent,
} from './timestamp';
const TIMESTAMP_SELECTOR = '.dt-chart-timestamp-selector';
describe('DtChart Timestamp', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [DtChartModule],
declarations: [
TimestampTestComponent,
TimestampTestA11yComponent,
TimestampTestBindingValuesComponent,
],
});
TestBed.compileComponents();
});
describe('Timestamp set programmatically', () => {
let fixture: ComponentFixture<TimestampTestComponent>;
let timestamp: DtChartTimestamp;
beforeEach(() => {
fixture = TestBed.createComponent<TimestampTestComponent>(
TimestampTestComponent,
);
timestamp = fixture.componentInstance.timestamp;
fixture.detectChanges();
});
it('should initialize a hidden timestamp without any style.', () => {
expect(timestamp).toBeDefined();
expect(timestamp._hidden).toBe(true);
expect(timestamp._position).toBe(0);
expect(timestamp.value).toEqual(0);
});
it('should show the timestamp element when hidden is false', () => {
let container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR));
expect(container).not.toBeTruthy();
timestamp._hidden = false;
fixture.detectChanges();
container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR));
expect(container).toBeTruthy();
});
it('should create a timestamp and update it programmatically', () => {
timestamp._position = 20;
timestamp._hidden = false;
fixture.detectChanges();
let container: HTMLElement = fixture.debugElement.query(
By.css(TIMESTAMP_SELECTOR),
).nativeElement;
expect(container.style.transform).toBe('translateX(20px)');
timestamp._position = 100;
fixture.detectChanges();
container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR))
.nativeElement;
expect(container.style.transform).toBe('translateX(100px)');
});
it('should create a timestamp and update it programmatically with chart values', () => {
// mock a fake value to px function
timestamp._valueToPixels = (value: number) => value * 2;
timestamp.value = 10;
fixture.detectChanges();
let container: HTMLElement = fixture.debugElement.query(
By.css(TIMESTAMP_SELECTOR),
).nativeElement;
expect(container.style.transform).toBe('translateX(20px)');
timestamp.value = 100;
fixture.detectChanges();
container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR))
.nativeElement;
expect(container.style.transform).toBe('translateX(200px)');
});
it('should hide the timestamp when falsy values are provided', () => {
// mock a fake value to px function
timestamp._valueToPixels = (value: number) => value * 2;
timestamp.value = 200;
fixture.detectChanges();
let container: DebugElement = fixture.debugElement.query(
By.css(TIMESTAMP_SELECTOR),
);
expect(container).not.toBeNull();
// imitate falsy values
timestamp.value = (undefined as unknown) as number;
fixture.detectChanges();
container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR));
expect(container).toBeNull();
expect(timestamp._hidden).toBe(true);
});
});
describe('Timestamp with binding', () => {
let fixture: ComponentFixture<TimestampTestBindingValuesComponent>;
let timestamp: DtChartTimestamp;
beforeEach(() => {
fixture = createComponent(TimestampTestBindingValuesComponent);
timestamp = fixture.componentInstance.timestamp;
});
it('should emit a close event when the timestamp is destroyed by calling the handle close function', () => {
expect(fixture.componentInstance._closed).toBe(0);
timestamp._handleOverlayClose();
fixture.detectChanges();
expect(fixture.componentInstance._closed).toBe(1);
});
it('should emit a close event when the timestamp is destroyed by pressing delete key', () => {
expect(fixture.componentInstance._closed).toBe(0);
const fakeEvent = createKeyboardEvent('keyupEvent', DELETE);
timestamp._handleKeyUp(fakeEvent);
fixture.detectChanges();
expect(fixture.componentInstance._closed).toBe(1);
});
it('should have initial values from binding and update them', () => {
expect(timestamp._hidden).toBe(false);
let container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR))
.nativeElement;
expect(container.style.transform).toBe('translateX(10px)');
fixture.componentInstance.value = 100;
fixture.detectChanges();
container = fixture.debugElement.query(By.css(TIMESTAMP_SELECTOR))
.nativeElement;
expect(container.style.transform).toBe('translateX(100px)');
});
it('setting the value with the binding should trigger a _stateChanges event', () => {
const stateChangesSpy = jest.fn();
const state1 = new TimestampStateChangedEvent(100, false);
const state2 = new TimestampStateChangedEvent(110, false);
const state3 = new TimestampStateChangedEvent(130, false);
const subscription = timestamp._stateChanges.subscribe(stateChangesSpy);
fixture.componentInstance.value = 100;
fixture.detectChanges();
expect(stateChangesSpy).toHaveBeenCalledTimes(1);
expect(stateChangesSpy.mock.calls[0]).toEqual([state1]);
fixture.componentInstance.value = 110;
fixture.detectChanges();
expect(stateChangesSpy).toHaveBeenCalledTimes(2);
expect(stateChangesSpy.mock.calls[1]).toEqual([state2]);
fixture.componentInstance.value = 130;
fixture.detectChanges();
expect(stateChangesSpy).toHaveBeenCalledTimes(3);
expect(stateChangesSpy.mock.calls[2]).toEqual([state3]);
subscription.unsubscribe();
});
});
describe('accessibility', () => {
let fixture: ComponentFixture<TimestampTestBindingValuesComponent>;
let fixtureA11y: ComponentFixture<TimestampTestA11yComponent>;
let timestamp: DtChartTimestamp;
let spiedDate: jest.SpyInstance;
beforeEach(() => {
fixture = TestBed.createComponent<TimestampTestBindingValuesComponent>(
TimestampTestBindingValuesComponent,
);
fixtureA11y = TestBed.createComponent<TimestampTestA11yComponent>(
TimestampTestA11yComponent,
);
timestamp = fixtureA11y.componentInstance.timestamp;
fixture.detectChanges();
fixtureA11y.detectChanges();
spiedDate = jest
.spyOn(global.Date, 'now')
.mockImplementationOnce(() =>
new Date('2019/06/01 09:33:21').valueOf(),
);
});
afterEach(() => {
spiedDate.mockClear();
});
it('should have default aria-labels on timestamp', () => {
const container = fixture.debugElement.query(
By.css('.dt-chart-timestamp-selector'),
).nativeElement;
expect(container.getAttribute('aria-role')).toBe('slider');
expect(container.getAttribute('aria-orientation')).toBe('horizontal');
expect(container.getAttribute('aria-label')).toBe(
ARIA_DEFAULT_SELECTED_LABEL,
);
expect(container.getAttribute('aria-valuenow')).toBe('10');
expect(container.getAttribute('aria-valuetext')).toMatch(/Jan 1/);
});
it('should have aria min and max values provided on container', () => {
const container = fixture.debugElement.query(
By.css('.dt-chart-timestamp-selector'),
).nativeElement;
expect(container.getAttribute('aria-valuemin')).toBe(
`${new Date('2019/01/01 00:00:00').getTime()}`,
);
expect(container.getAttribute('aria-valuemax')).toBe(
`${new Date('2019/12/31 00:00:00').getTime()}`,
);
});
it('should have updated aria labels on timestamp when input is provided', () => {
const container = fixtureA11y.debugElement.query(
By.css('.dt-chart-timestamp-selector'),
).nativeElement;
expect(container.getAttribute('aria-label')).toBe('SELECTED');
});
it('should have updated aria value labels on timestamp when values changing', () => {
const newDate = new Date('2018/10/11 20:48:11').getTime();
timestamp.value = newDate;
fixtureA11y.detectChanges();
const container = fixtureA11y.debugElement.query(
By.css('.dt-chart-timestamp-selector'),
).nativeElement;
expect(container.getAttribute('aria-valuenow')).toBe(`${newDate}`);
expect(container.getAttribute('aria-valuetext')).toBe('Oct 11, 20:48');
});
});
describe('keyboard support', () => {
let fixture: ComponentFixture<TimestampTestBindingValuesComponent>;
let timestamp: DtChartTimestamp;
let selector: HTMLElement;
beforeEach(() => {
fixture = createComponent(TimestampTestBindingValuesComponent);
timestamp = fixture.componentInstance.timestamp;
const el: HTMLElement = fixture.debugElement.query(
By.css('.dt-chart-timestamp'),
).nativeElement;
jest.spyOn(el, 'getBoundingClientRect').mockReturnValue({
bottom: 100,
height: 100,
left: 0,
right: 100,
top: 0,
width: 100,
});
selector = fixture.debugElement.query(
By.css('.dt-chart-timestamp-selector'),
).nativeElement;
});
it('should trigger a switch to Range event when the shift key was pressed', () => {
const spy = jest.fn();
const subscription = timestamp._switchToRange.subscribe(spy);
const event = createKeyboardEvent('keydown', UP_ARROW);
jest.spyOn(event, 'shiftKey', 'get').mockImplementation(() => true);
dispatchEvent(selector, event);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(10); // value of the timestamp
subscription.unsubscribe();
});
it('should increase the value on right arrow press', () => {
dispatchKeyboardEvent(selector, 'keydown', RIGHT_ARROW);
expect(timestamp.value).toBe(11);
expect(timestamp._position).toBe(11);
});
it('should increase the value on up arrow press', () => {
dispatchKeyboardEvent(selector, 'keydown', UP_ARROW);
expect(timestamp.value).toBe(11);
expect(timestamp._position).toBe(11);
});
it('should decrease the value on left arrow press', () => {
dispatchKeyboardEvent(selector, 'keydown', LEFT_ARROW);
expect(timestamp.value).toBe(9);
expect(timestamp._position).toBe(9);
});
it('should decrease the value on down arrow press', () => {
dispatchKeyboardEvent(selector, 'keydown', DOWN_ARROW);
expect(timestamp.value).toBe(9);
expect(timestamp._position).toBe(9);
});
it('should increase the value in large steps on page up press', () => {
dispatchKeyboardEvent(selector, 'keydown', PAGE_UP);
expect(timestamp.value).toBe(20);
expect(timestamp._position).toBe(20);
});
it('should decrease the value in large steps on page down press', () => {
fixture.debugElement.componentInstance.value = 44;
fixture.detectChanges();
dispatchKeyboardEvent(selector, 'keydown', PAGE_DOWN);
expect(timestamp.value).toBe(34);
expect(timestamp._position).toBe(34);
});
it('should set the value to the min on home press', () => {
fixture.debugElement.componentInstance.value = 44;
fixture.detectChanges();
dispatchKeyboardEvent(selector, 'keydown', HOME);
expect(timestamp.value).toBe(0);
expect(timestamp._position).toBe(0);
});
it('should set the value to the max on end press', () => {
fixture.debugElement.componentInstance.value = 44;
fixture.detectChanges();
dispatchKeyboardEvent(selector, 'keydown', END);
expect(timestamp.value).toBe(100);
expect(timestamp._position).toBe(100);
});
it('should destroy the timestamp on backspace press', () => {
expect(timestamp._hidden).toBe(false);
dispatchKeyboardEvent(selector, 'keydown', BACKSPACE);
expect(timestamp.value).toBe(0);
expect(timestamp._hidden).toBe(true);
});
it('should destroy the timestamp on delete press', () => {
expect(timestamp._hidden).toBe(false);
dispatchKeyboardEvent(selector, 'keydown', DELETE);
expect(timestamp.value).toBe(0);
expect(timestamp._hidden).toBe(true);
});
});
});
@Component({
selector: 'timestamp-test-component',
template: '<dt-chart-timestamp></dt-chart-timestamp>',
})
export class TimestampTestComponent {
@ViewChild(DtChartTimestamp, { static: true }) timestamp: DtChartTimestamp;
}
@Component({
selector: 'timestamp-test-a11y-component',
template: `
<dt-chart-timestamp
[value]="value"
aria-label-close="CLOSE"
aria-label-selected="SELECTED"
></dt-chart-timestamp>
`,
})
export class TimestampTestA11yComponent implements OnInit {
@ViewChild(DtChartTimestamp, { static: true }) timestamp: DtChartTimestamp;
value = new Date('2019/06/01 20:38:14').getTime();
ngOnInit(): void {
this.timestamp._valueToPixels = (value: number) => value;
}
}
@Component({
selector: 'timestamp-test-bind-value-component',
template:
'<dt-chart-timestamp [value]="value" (closed)="closed()"></dt-chart-timestamp>',
})
export class TimestampTestBindingValuesComponent implements OnInit {
@ViewChild(DtChartTimestamp, { static: true }) timestamp: DtChartTimestamp;
value = 10;
_closed = 0;
ngOnInit(): void {
this.timestamp._minValue = new Date('2019/01/01 00:00:00').getTime();
this.timestamp._maxValue = new Date('2019/12/31 00:00:00').getTime();
this.timestamp._valueToPixels = (value: number) => value;
this.timestamp._pixelsToValue = (value: number) => value;
}
closed(): void {
this._closed++;
}
}