blob: deb04b76b3dcfd4f6c0657f2006a7386218c6bff [file] [log] [blame]
Matteo Scandoloc8178492017-04-11 17:55:13 -07001import * as angular from 'angular';
2import 'angular-mocks';
3import {IXosKeyboardShortcutService, XosKeyboardShortcut, IXosKeyboardShortcutBinding} from './keyboard-shortcut';
4import {IXosSidePanelService} from '../side-panel/side-panel.service';
5
6let service: IXosKeyboardShortcutService;
7let $log: ng.ILogService;
8let $transitions: any;
9let XosSidePanel: IXosSidePanelService;
Matteo Scandolo9b460042017-04-14 16:24:45 -070010let logSpy: any;
Matteo Scandoloc8178492017-04-11 17:55:13 -070011
12const baseGlobalModifiers: IXosKeyboardShortcutBinding[] = [
13 {
14 key: 'a',
15 cb: 'cb'
16 },
17 {
18 key: 'a',
19 cb: 'modified',
20 modifiers: ['alt']
21 },
22 {
23 key: 'a',
24 cb: 'modified',
25 modifiers: ['meta']
26 }
27];
28
29const baseLocalModifiers: IXosKeyboardShortcutBinding[] = [
30 {
31 key: 'b',
32 cb: 'cb'
33 },
34 {
35 key: 'b',
36 cb: 'modified',
37 modifiers: ['meta', 'alt']
38 }
39];
40
41describe('The XosKeyboardShortcut service', () => {
42
43 beforeEach(() => {
44 angular.module('leyBinding', ['ui.router'])
45 .service('XosKeyboardShortcut', XosKeyboardShortcut)
46 .value('XosSidePanel', {
47
48 });
49 angular.mock.module('leyBinding');
50
51 angular.mock.inject((
52 _$log_: ng.ILogService,
53 _$transitions_: any,
54 _XosSidePanel_: IXosSidePanelService
55 ) => {
56 $log = _$log_;
57 $transitions = _$transitions_;
58 XosSidePanel = _XosSidePanel_;
Matteo Scandolo9b460042017-04-14 16:24:45 -070059 logSpy = spyOn($log, 'warn');
Matteo Scandoloc8178492017-04-11 17:55:13 -070060 });
61
62 service = new XosKeyboardShortcut($log, $transitions, XosSidePanel);
63 });
64
65 it('should have a setup method', () => {
66 expect(service.setup).toBeDefined();
67 });
68
69 describe('the addActiveModifierKey method', () => {
70 beforeEach(() => {
71 service['activeModifiers'] = [];
72 });
73 it('should add an active modifier', () => {
74 service['addActiveModifierKey']('shift');
75 expect(service['activeModifiers']).toEqual(['shift']);
76 });
77
78 it('should not add a modifier twice', () => {
79 service['addActiveModifierKey']('shift');
80 service['addActiveModifierKey']('shift');
81 expect(service['activeModifiers']).toEqual(['shift']);
82 });
83 });
84
85 describe('the removeActiveModifierKey method', () => {
86 beforeEach(() => {
87 service['activeModifiers'] = ['shift', 'meta'];
88 });
89 it('should remove an active modifier', () => {
90 service['removeActiveModifierKey']('shift');
91 expect(service['activeModifiers']).toEqual(['meta']);
92 });
93 });
94
95 describe('the findBindedShortcut method', () => {
96 beforeEach(() => {
97 service['activeModifiers'] = [];
98 service['keyMapping']['global'] = baseGlobalModifiers;
99 service['keyMapping']['view'] = baseLocalModifiers;
100 });
101
102 it('should find a global keybinding', () => {
103 const binding = service['findBindedShortcut']('a');
104 expect(binding).toEqual({key: 'a', cb: 'cb'});
105 });
106
107 it('should find a global keybinding with modifiers', () => {
108 service['activeModifiers'] = ['meta'];
109 const binding = service['findBindedShortcut']('a');
110 expect(binding).toEqual({key: 'a', cb: 'modified', modifiers: ['meta']});
111 });
112
113 it('should find a view keybinding', () => {
114 const binding = service['findBindedShortcut']('b');
115 expect(binding).toEqual({key: 'b', cb: 'cb'});
116 });
117
118 it('should find a view keybinding with modifiers', () => {
119 service['activeModifiers'] = ['meta', 'alt'];
120 const binding = service['findBindedShortcut']('b');
121 expect(binding).toEqual({key: 'b', cb: 'modified', modifiers: ['meta', 'alt']});
122 });
123
124 it('should not care about binding key case', () => {
125 const binding = service['findBindedShortcut']('A');
126 expect(binding).toEqual({key: 'a', cb: 'cb'});
127 });
128 });
129
130 describe('the registerKeyBinding method', () => {
131
132 const binding = {
133 key: 'B',
134 cb: 'callback'
135 };
136
137 beforeEach(() => {
138 service['keyMapping'] = {
139 global: [
140 {
141 key: 'a',
142 cb: 'cb'
143 }
144 ],
145 view: []
146 };
147 });
148
149 it('should add a new global keybinding', () => {
150 service['registerKeyBinding'](binding, 'global');
151 expect(service['keyMapping']['global'].length).toBe(2);
152 expect(service['keyMapping']['global'][1].key).toBe('b');
153 });
154
155 it('should add a new view keybinding', () => {
156 service['registerKeyBinding'](binding);
157 expect(service['keyMapping']['view'].length).toBe(1);
158 expect(service['keyMapping']['view'][0].key).toBe('b');
159 });
160
161 it('should not add binding that is not registered as "global" or "view"', () => {
162 function errorFunctionWrapper() {
163 service['registerKeyBinding']({
164 key: 'z',
165 cb: 'cb'
166 }, 'something');
167 }
168 expect(errorFunctionWrapper).toThrow(new Error('[XosKeyboardShortcut] A shortcut can be registered with scope "global" or "view" only'));
169 });
170
171 it('should not add binding that has an already registered key', () => {
Matteo Scandolo9b460042017-04-14 16:24:45 -0700172 service['registerKeyBinding']({
173 key: 'A',
174 cb: 'cb'
175 }, 'global');
176 expect(logSpy).toHaveBeenCalledWith('[XosKeyboardShortcut] A shortcut for key "a" has already been registered');
Matteo Scandoloc8178492017-04-11 17:55:13 -0700177 });
178 });
179});