-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
datepicker-keyboard-service.spec.ts
133 lines (98 loc) · 4.76 KB
/
datepicker-keyboard-service.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
import {NgbDatepicker, NgbDatepickerState} from './datepicker';
import {NgbDatepickerKeyboardService} from './datepicker-keyboard-service';
import {NgbCalendar, NgbCalendarGregorian} from './ngb-calendar';
import {TestBed} from '@angular/core/testing';
import {NgbDate} from './ngb-date';
import {Key} from '../util/key';
import {Type} from '@angular/core';
const event = (keyCode: number, shift = false) =>
<any>({which: keyCode, shiftKey: shift, preventDefault: () => {}, stopPropagation: () => {}});
describe('ngb-datepicker-keyboard-service', () => {
let service: NgbDatepickerKeyboardService;
let calendar: NgbCalendar;
let mock: Partial<NgbDatepicker>;
let processKey = function(e: KeyboardEvent) { service.processKey(e, mock as NgbDatepicker); };
let state: NgbDatepickerState = Object.assign({focusedDate: {day: 1, month: 1, year: 2018}});
beforeEach(() => {
TestBed.configureTestingModule(
{providers: [{provide: NgbCalendar, useClass: NgbCalendarGregorian}, NgbDatepickerKeyboardService]});
calendar = TestBed.get(NgbCalendar as Type<NgbCalendar>);
service = TestBed.get(NgbDatepickerKeyboardService);
mock = {state, focusDate: () => {}, focusSelect: () => {}, calendar};
spyOn(mock, 'focusDate');
spyOn(mock, 'focusSelect');
spyOn(calendar, 'getNext');
spyOn(calendar, 'getPrev');
});
it('should be instantiated', () => { expect(service).toBeTruthy(); });
it('should move focus by 1 day or 1 week with "Arrow" keys', () => {
processKey(event(Key.ArrowUp));
expect(calendar.getPrev).toHaveBeenCalledWith(state.focusedDate, 'd', 7);
processKey(event(Key.ArrowDown));
expect(calendar.getNext).toHaveBeenCalledWith(state.focusedDate, 'd', 7);
processKey(event(Key.ArrowLeft));
expect(calendar.getPrev).toHaveBeenCalledWith(state.focusedDate, 'd', 1);
processKey(event(Key.ArrowRight));
expect(calendar.getNext).toHaveBeenCalledWith(state.focusedDate, 'd', 1);
expect(calendar.getPrev).toHaveBeenCalledTimes(2);
expect(calendar.getNext).toHaveBeenCalledTimes(2);
});
it('should move focus by 1 month or year "PgUp" and "PageDown"', () => {
processKey(event(Key.PageUp));
expect(calendar.getPrev).toHaveBeenCalledWith(state.focusedDate, 'm', 1);
processKey(event(Key.PageDown));
expect(calendar.getNext).toHaveBeenCalledWith(state.focusedDate, 'm', 1);
processKey(event(Key.PageUp, true));
expect(calendar.getPrev).toHaveBeenCalledWith(state.focusedDate, 'y', 1);
processKey(event(Key.PageDown, true));
expect(calendar.getNext).toHaveBeenCalledWith(state.focusedDate, 'y', 1);
expect(calendar.getPrev).toHaveBeenCalledTimes(2);
expect(calendar.getNext).toHaveBeenCalledTimes(2);
});
it('should select focused date with "Space" and "Enter"', () => {
processKey(event(Key.Enter));
processKey(event(Key.Space));
expect(mock.focusSelect).toHaveBeenCalledTimes(2);
});
it('should move focus to the first and last days in the view with "Home" and "End"', () => {
processKey(event(Key.Home));
expect(mock.focusDate).toHaveBeenCalledWith(undefined);
processKey(event(Key.End));
expect(mock.focusDate).toHaveBeenCalledWith(undefined);
Object.assign(state, {firstDate: new NgbDate(2017, 1, 1)});
Object.assign(state, {lastDate: new NgbDate(2017, 12, 1)});
processKey(event(Key.Home));
expect(mock.focusDate).toHaveBeenCalledWith(new NgbDate(2017, 1, 1));
processKey(event(Key.End));
expect(mock.focusDate).toHaveBeenCalledWith(new NgbDate(2017, 12, 1));
expect(mock.focusDate).toHaveBeenCalledTimes(4);
});
it('should move focus to the "min" and "max" dates with "Home" and "End"', () => {
processKey(event(Key.Home, true));
expect(mock.focusDate).toHaveBeenCalledWith(undefined);
processKey(event(Key.End, true));
expect(mock.focusDate).toHaveBeenCalledWith(undefined);
Object.assign(state, {minDate: new NgbDate(2017, 1, 1)});
Object.assign(state, {maxDate: new NgbDate(2017, 12, 1)});
processKey(event(Key.Home, true));
expect(mock.focusDate).toHaveBeenCalledWith(new NgbDate(2017, 1, 1));
processKey(event(Key.End, true));
expect(mock.focusDate).toHaveBeenCalledWith(new NgbDate(2017, 12, 1));
expect(mock.focusDate).toHaveBeenCalledTimes(4);
});
it('should prevent default and stop propagation of the known key', () => {
let e = event(Key.ArrowUp);
spyOn(e, 'preventDefault');
spyOn(e, 'stopPropagation');
processKey(e);
expect(e.preventDefault).toHaveBeenCalled();
expect(e.stopPropagation).toHaveBeenCalled();
// unknown key
e = event(23);
spyOn(e, 'preventDefault');
spyOn(e, 'stopPropagation');
processKey(e);
expect(e.preventDefault).not.toHaveBeenCalled();
expect(e.stopPropagation).not.toHaveBeenCalled();
});
});