@@ -9,110 +9,170 @@ beforeEach(() => {
99} ) ;
1010
1111describe ( "robotjs keyboard action" , ( ) => {
12- it ( "should forward the keyTap call to robotjs for a known key" , ( ) => {
13- // GIVEN
14- const SUT = new KeyboardAction ( ) ;
12+ describe ( "click" , ( ) => {
13+ it ( "should forward the keyTap call to robotjs for a known key" , ( ) => {
14+ // GIVEN
15+ const SUT = new KeyboardAction ( ) ;
1516
16- // WHEN
17- SUT . click ( Key . A ) ;
17+ // WHEN
18+ SUT . click ( Key . A ) ;
1819
19- // THEN
20- expect ( robot . keyTap ) . toBeCalledTimes ( 1 ) ;
21- } ) ;
20+ // THEN
21+ expect ( robot . keyTap ) . toBeCalledTimes ( 1 ) ;
22+ } ) ;
2223
23- it ( "should not forward the keyTap call to robotjs for an unknown key" , ( ) => {
24- // GIVEN
25- const SUT = new KeyboardAction ( ) ;
24+ it ( "should reject on robotjs errors" , async ( ) => {
25+ // GIVEN
26+ const SUT = new KeyboardAction ( ) ;
27+ robot . keyTap = jest . fn ( ( ) => {
28+ throw new Error ( "Test error" ) ;
29+ } ) ;
2630
27- // WHEN
28- SUT . click ( Key . Add ) ;
31+ // WHEN
2932
30- // THEN
31- expect ( robot . keyTap ) . not . toBeCalled ( ) ;
32- } ) ;
33+ // THEN
34+ expect ( SUT . click ( Key . A ) ) . rejects . toThrowError ( "Test error" ) ;
35+ } ) ;
3336
34- it ( "should forward the type call to robotjs" , ( ) => {
35- // GIVEN
36- const SUT = new KeyboardAction ( ) ;
37- const payload = "testInput" ;
37+ it ( "should not forward the keyTap call to robotjs for an unknown key" , ( ) => {
38+ // GIVEN
39+ const SUT = new KeyboardAction ( ) ;
3840
39- // WHEN
40- SUT . type ( payload ) ;
41+ // WHEN
42+ SUT . click ( Key . Add ) ;
4143
42- // THEN
43- expect ( robot . typeString ) . toBeCalledTimes ( 1 ) ;
44- expect ( robot . typeString ) . toBeCalledWith ( payload ) ;
44+ // THEN
45+ expect ( robot . keyTap ) . not . toBeCalled ( ) ;
46+ } ) ;
4547 } ) ;
4648
47- it ( "should forward the pressKey call to robotjs for a known key" , ( ) => {
48- // GIVEN
49- const SUT = new KeyboardAction ( ) ;
49+ describe ( "type" , ( ) => {
50+ it ( "should forward the type call to robotjs" , ( ) => {
51+ // GIVEN
52+ const SUT = new KeyboardAction ( ) ;
53+ const payload = "testInput" ;
5054
51- // WHEN
52- SUT . pressKey ( Key . A ) ;
55+ // WHEN
56+ SUT . type ( payload ) ;
5357
54- // THEN
55- expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
56- expect ( robot . keyToggle ) . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "down" , [ ] ) ;
57- } ) ;
58+ // THEN
59+ expect ( robot . typeString ) . toBeCalledTimes ( 1 ) ;
60+ expect ( robot . typeString ) . toBeCalledWith ( payload ) ;
61+ } ) ;
5862
59- it ( "should treat a list of keys as modifiers + the actual key to press" , ( ) => {
60- // GIVEN
61- const SUT = new KeyboardAction ( ) ;
63+ it ( "should reject on robotjs errors" , async ( ) => {
64+ // GIVEN
65+ const SUT = new KeyboardAction ( ) ;
66+ robot . typeString = jest . fn ( ( ) => {
67+ throw new Error ( "Test error" ) ;
68+ } ) ;
6269
63- // WHEN
64- SUT . pressKey ( Key . LeftControl , Key . A ) ;
70+ // WHEN
6571
66- // THEN
67- expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
68- expect ( robot . keyToggle )
69- . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "down" , [ KeyboardAction . keyLookup ( Key . LeftControl ) ] ) ;
72+ // THEN
73+ expect ( SUT . type ( "foo" ) ) . rejects . toThrowError ( "Test error" ) ;
74+ } ) ;
7075 } ) ;
7176
72- it ( "should not forward the pressKey call to robotjs for an unknown key" , ( ) => {
73- // GIVEN
74- const SUT = new KeyboardAction ( ) ;
77+ describe ( "pressKey" , ( ) => {
78+ it ( "should forward the pressKey call to robotjs for a known key" , ( ) => {
79+ // GIVEN
80+ const SUT = new KeyboardAction ( ) ;
7581
76- // WHEN
77- SUT . pressKey ( Key . Add ) ;
82+ // WHEN
83+ SUT . pressKey ( Key . A ) ;
7884
79- // THEN
80- expect ( robot . keyToggle ) . not . toBeCalled ( ) ;
81- } ) ;
85+ // THEN
86+ expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
87+ expect ( robot . keyToggle ) . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "down" , [ ] ) ;
88+ } ) ;
8289
83- it ( "should forward the releaseKey call to robotjs for a known key" , ( ) => {
84- // GIVEN
85- const SUT = new KeyboardAction ( ) ;
90+ it ( "should treat a list of keys as modifiers + the actual key to press " , ( ) => {
91+ // GIVEN
92+ const SUT = new KeyboardAction ( ) ;
8693
87- // WHEN
88- SUT . releaseKey ( Key . A ) ;
94+ // WHEN
95+ SUT . pressKey ( Key . LeftControl , Key . A ) ;
8996
90- // THEN
91- expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
92- expect ( robot . keyToggle ) . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "up" , [ ] ) ;
93- } ) ;
97+ // THEN
98+ expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
99+ expect ( robot . keyToggle )
100+ . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "down" , [ KeyboardAction . keyLookup ( Key . LeftControl ) ] ) ;
101+ } ) ;
94102
95- it ( "should treat a list of keys as modifiers + the actual key to release " , ( ) => {
96- // GIVEN
97- const SUT = new KeyboardAction ( ) ;
103+ it ( "should not forward the pressKey call to robotjs for an unknown key" , ( ) => {
104+ // GIVEN
105+ const SUT = new KeyboardAction ( ) ;
98106
99- // WHEN
100- SUT . releaseKey ( Key . LeftControl , Key . A ) ;
107+ // WHEN
108+ SUT . pressKey ( Key . Add ) ;
101109
102- // THEN
103- expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
104- expect ( robot . keyToggle )
105- . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "up" , [ KeyboardAction . keyLookup ( Key . LeftControl ) ] ) ;
110+ // THEN
111+ expect ( robot . keyToggle ) . not . toBeCalled ( ) ;
112+ } ) ;
113+
114+ it ( "should reject on robotjs errors" , async ( ) => {
115+ // GIVEN
116+ const SUT = new KeyboardAction ( ) ;
117+ robot . keyToggle = jest . fn ( ( ) => {
118+ throw new Error ( "Test error" ) ;
119+ } ) ;
120+
121+ // WHEN
122+
123+ // THEN
124+ expect ( SUT . pressKey ( Key . A ) ) . rejects . toThrowError ( "Test error" ) ;
125+ } ) ;
106126 } ) ;
107127
108- it ( "should not forward the releaseKey call to robotjs for an unknown key" , ( ) => {
109- // GIVEN
110- const SUT = new KeyboardAction ( ) ;
128+ describe ( "releaseKey" , ( ) => {
129+ it ( "should forward the releaseKey call to robotjs for a known key" , ( ) => {
130+ // GIVEN
131+ const SUT = new KeyboardAction ( ) ;
132+
133+ // WHEN
134+ SUT . releaseKey ( Key . A ) ;
135+
136+ // THEN
137+ expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
138+ expect ( robot . keyToggle ) . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "up" , [ ] ) ;
139+ } ) ;
140+
141+ it ( "should treat a list of keys as modifiers + the actual key to release" , ( ) => {
142+ // GIVEN
143+ const SUT = new KeyboardAction ( ) ;
144+
145+ // WHEN
146+ SUT . releaseKey ( Key . LeftControl , Key . A ) ;
147+
148+ // THEN
149+ expect ( robot . keyToggle ) . toBeCalledTimes ( 1 ) ;
150+ expect ( robot . keyToggle )
151+ . toBeCalledWith ( KeyboardAction . keyLookup ( Key . A ) , "up" , [ KeyboardAction . keyLookup ( Key . LeftControl ) ] ) ;
152+ } ) ;
153+
154+ it ( "should not forward the releaseKey call to robotjs for an unknown key" , ( ) => {
155+ // GIVEN
156+ const SUT = new KeyboardAction ( ) ;
157+
158+ // WHEN
159+ SUT . releaseKey ( Key . Add ) ;
160+
161+ // THEN
162+ expect ( robot . keyToggle ) . not . toBeCalled ( ) ;
163+ } ) ;
164+
165+ it ( "should reject on robotjs errors" , async ( ) => {
166+ // GIVEN
167+ const SUT = new KeyboardAction ( ) ;
168+ robot . keyToggle = jest . fn ( ( ) => {
169+ throw new Error ( "Test error" ) ;
170+ } ) ;
111171
112- // WHEN
113- SUT . releaseKey ( Key . Add ) ;
172+ // WHEN
114173
115- // THEN
116- expect ( robot . keyToggle ) . not . toBeCalled ( ) ;
174+ // THEN
175+ expect ( SUT . releaseKey ( Key . A ) ) . rejects . toThrowError ( "Test error" ) ;
176+ } ) ;
117177 } ) ;
118178} ) ;
0 commit comments