1
1
/*
2
2
Copyright 2022 Šimon Brandner <[email protected] >
3
+ Copyright 2022 The Matrix.org Foundation C.I.C.
3
4
4
5
Licensed under the Apache License, Version 2.0 (the "License");
5
6
you may not use this file except in compliance with the License.
@@ -14,60 +15,76 @@ See the License for the specific language governing permissions and
14
15
limitations under the License.
15
16
*/
16
17
17
- import {
18
- KEYBOARD_SHORTCUTS ,
19
- mock ,
20
- } from "../../src/accessibility/KeyboardShortcuts" ;
21
- import { getKeyboardShortcuts , getKeyboardShortcutsForUI } from "../../src/accessibility/KeyboardShortcutUtils" ;
22
18
import { mockPlatformPeg , unmockPlatformPeg } from "../test-utils" ;
23
19
20
+ const PATH_TO_KEYBOARD_SHORTCUTS = "../../src/accessibility/KeyboardShortcuts" ;
21
+ const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../src/accessibility/KeyboardShortcutUtils" ;
22
+
23
+ const mockKeyboardShortcuts = ( override ) => {
24
+ jest . doMock ( PATH_TO_KEYBOARD_SHORTCUTS , ( ) => {
25
+ const original = jest . requireActual ( PATH_TO_KEYBOARD_SHORTCUTS ) ;
26
+ return {
27
+ ...original ,
28
+ ...override ,
29
+ } ;
30
+ } ) ;
31
+ } ;
32
+ const getFile = async ( ) => await import ( PATH_TO_KEYBOARD_SHORTCUTS ) ;
33
+ const getUtils = async ( ) => await import ( PATH_TO_KEYBOARD_SHORTCUT_UTILS ) ;
34
+
24
35
describe ( "KeyboardShortcutUtils" , ( ) => {
25
- afterEach ( ( ) => {
36
+ beforeEach ( ( ) => {
26
37
unmockPlatformPeg ( ) ;
38
+ jest . resetModules ( ) ;
27
39
} ) ;
28
40
29
41
it ( "doesn't change KEYBOARD_SHORTCUTS when getting shortcuts" , async ( ) => {
30
- mock ( {
31
- keyboardShortcuts : {
42
+ mockKeyboardShortcuts ( {
43
+ KEYBOARD_SHORTCUTS : {
32
44
"Keybind1" : { } ,
33
45
"Keybind2" : { } ,
34
46
} ,
35
- macOnlyShortcuts : [ "Keybind1" ] ,
36
- desktopShortcuts : [ "Keybind2" ] ,
47
+ MAC_ONLY_SHORTCUTS : [ "Keybind1" ] ,
48
+ DESKTOP_SHORTCUTS : [ "Keybind2" ] ,
37
49
} ) ;
38
50
mockPlatformPeg ( { overrideBrowserShortcuts : jest . fn ( ) . mockReturnValue ( false ) } ) ;
39
- const copyKeyboardShortcuts = Object . assign ( { } , KEYBOARD_SHORTCUTS ) ;
51
+ const utils = await getUtils ( ) ;
52
+ const file = await getFile ( ) ;
53
+ const copyKeyboardShortcuts = Object . assign ( { } , file . KEYBOARD_SHORTCUTS ) ;
40
54
41
- getKeyboardShortcuts ( ) ;
42
- expect ( KEYBOARD_SHORTCUTS ) . toEqual ( copyKeyboardShortcuts ) ;
43
- getKeyboardShortcutsForUI ( ) ;
44
- expect ( KEYBOARD_SHORTCUTS ) . toEqual ( copyKeyboardShortcuts ) ;
55
+ utils . getKeyboardShortcuts ( ) ;
56
+ expect ( file . KEYBOARD_SHORTCUTS ) . toEqual ( copyKeyboardShortcuts ) ;
57
+ utils . getKeyboardShortcutsForUI ( ) ;
58
+ expect ( file . KEYBOARD_SHORTCUTS ) . toEqual ( copyKeyboardShortcuts ) ;
45
59
} ) ;
46
60
47
- it ( "correctly filters shortcuts" , async ( ) => {
48
- mock ( {
49
- keyboardShortcuts : {
50
- "Keybind1" : { } ,
51
- "Keybind2" : { } ,
52
- "Keybind3" : { "controller" : { settingDisabled : true } } ,
53
- "Keybind4" : { } ,
54
- } ,
55
- macOnlyShortcuts : [ "Keybind1" ] ,
56
- desktopShortcuts : [ "Keybind2" ] ,
57
-
61
+ describe ( "correctly filters shortcuts" , ( ) => {
62
+ it ( "when on web and not on macOS " , async ( ) => {
63
+ mockKeyboardShortcuts ( {
64
+ KEYBOARD_SHORTCUTS : {
65
+ "Keybind1" : { } ,
66
+ "Keybind2" : { } ,
67
+ "Keybind3" : { "controller" : { settingDisabled : true } } ,
68
+ "Keybind4" : { } ,
69
+ } ,
70
+ MAC_ONLY_SHORTCUTS : [ "Keybind1" ] ,
71
+ DESKTOP_SHORTCUTS : [ "Keybind2" ] ,
72
+ } ) ;
73
+ mockPlatformPeg ( { overrideBrowserShortcuts : jest . fn ( ) . mockReturnValue ( false ) } ) ;
74
+ expect ( ( await getUtils ( ) ) . getKeyboardShortcuts ( ) ) . toEqual ( { "Keybind4" : { } } ) ;
58
75
} ) ;
59
- mockPlatformPeg ( { overrideBrowserShortcuts : jest . fn ( ) . mockReturnValue ( false ) } ) ;
60
- expect ( getKeyboardShortcuts ( ) ) . toEqual ( { "Keybind4" : { } } ) ;
61
76
62
- mock ( {
63
- keyboardShortcuts : {
64
- "Keybind1" : { } ,
65
- "Keybind2" : { } ,
66
- } ,
67
- macOnlyShortcuts : undefined ,
68
- desktopShortcuts : [ "Keybind2" ] ,
77
+ it ( "when on desktop" , async ( ) => {
78
+ mockKeyboardShortcuts ( {
79
+ KEYBOARD_SHORTCUTS : {
80
+ "Keybind1" : { } ,
81
+ "Keybind2" : { } ,
82
+ } ,
83
+ MAC_ONLY_SHORTCUTS : [ ] ,
84
+ DESKTOP_SHORTCUTS : [ "Keybind2" ] ,
85
+ } ) ;
86
+ mockPlatformPeg ( { overrideBrowserShortcuts : jest . fn ( ) . mockReturnValue ( true ) } ) ;
87
+ expect ( ( await getUtils ( ) ) . getKeyboardShortcuts ( ) ) . toEqual ( { "Keybind1" : { } , "Keybind2" : { } } ) ;
69
88
} ) ;
70
- mockPlatformPeg ( { overrideBrowserShortcuts : jest . fn ( ) . mockReturnValue ( true ) } ) ;
71
- expect ( getKeyboardShortcuts ( ) ) . toEqual ( { "Keybind1" : { } , "Keybind2" : { } } ) ;
72
89
} ) ;
73
90
} ) ;
0 commit comments