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