1
+ import libnut = require( "@nut-tree/libnut" ) ;
2
+ import { WindowAction } from "./libnut-window-action.class" ;
3
+ import { Region } from "../../region.class" ;
4
+
5
+ jest . mock ( "@nut-tree/libnut" ) ;
6
+
7
+ beforeEach ( ( ) => {
8
+ jest . resetAllMocks ( ) ;
9
+ } ) ;
10
+
11
+ describe ( "libnut WindowAction" , ( ) => {
12
+ describe ( "getWindows" , ( ) => {
13
+ it ( "should resolve to a list of numeric window handles via libnut#getWindows" , async ( ) => {
14
+ // GIVEN
15
+ const SUT = new WindowAction ( ) ;
16
+ const windowList = [ 1 , 2 , 3 ] ;
17
+ libnut . getWindows = jest . fn ( ( ) => windowList ) ;
18
+
19
+ // WHEN
20
+ const windows = SUT . getWindows ( ) ;
21
+
22
+ // THEN
23
+ await expect ( libnut . getWindows ) . toBeCalledTimes ( 1 ) ;
24
+ await expect ( windows ) . resolves . toBe ( windowList ) ;
25
+ } ) ;
26
+
27
+ it ( "should reject on errors in libnut#getWindows" , async ( ) => {
28
+ // GIVEN
29
+ const SUT = new WindowAction ( ) ;
30
+ const errorMessage = "getWindows threw" ;
31
+ libnut . getWindows = jest . fn ( ( ) => {
32
+ throw new Error ( errorMessage ) ;
33
+ } ) ;
34
+
35
+ // WHEN
36
+ const windows = SUT . getWindows ( ) ;
37
+
38
+ // THEN
39
+ await expect ( libnut . getWindows ) . toBeCalledTimes ( 1 ) ;
40
+ await expect ( windows ) . rejects . toThrowError ( errorMessage ) ;
41
+ } ) ;
42
+ } ) ;
43
+
44
+ describe ( "getActiveWindow" , ( ) => {
45
+ it ( "should resolve to a numeric window handles via libnut#getActiveWindow" , async ( ) => {
46
+ // GIVEN
47
+ const SUT = new WindowAction ( ) ;
48
+ const activeWindow = 1 ;
49
+ libnut . getActiveWindow = jest . fn ( ( ) => activeWindow ) ;
50
+
51
+ // WHEN
52
+ const window = SUT . getActiveWindow ( ) ;
53
+
54
+ // THEN
55
+ await expect ( libnut . getActiveWindow ) . toBeCalledTimes ( 1 ) ;
56
+ await expect ( window ) . resolves . toBe ( activeWindow ) ;
57
+ } ) ;
58
+
59
+ it ( "should reject on errors in libnut#getActiveWindow" , async ( ) => {
60
+ // GIVEN
61
+ const SUT = new WindowAction ( ) ;
62
+ const errorMessage = "getActiveWindow threw" ;
63
+ libnut . getActiveWindow = jest . fn ( ( ) => {
64
+ throw new Error ( errorMessage ) ;
65
+ } ) ;
66
+
67
+ // WHEN
68
+ const windows = SUT . getActiveWindow ( ) ;
69
+
70
+ // THEN
71
+ await expect ( libnut . getActiveWindow ) . toBeCalledTimes ( 1 ) ;
72
+ await expect ( windows ) . rejects . toThrowError ( errorMessage ) ;
73
+ } ) ;
74
+ } ) ;
75
+
76
+ describe ( "getWindowRegion" , ( ) => {
77
+ it ( "should resolve to a window region via libnut#getWindowRegion" , async ( ) => {
78
+ // GIVEN
79
+ const SUT = new WindowAction ( ) ;
80
+ const windowHandle = 100 ;
81
+ const windowRect = {
82
+ x : 1 ,
83
+ y : 2 ,
84
+ width : 42 ,
85
+ height : 23
86
+ } ;
87
+ const windowRegion = new Region ( windowRect . x , windowRect . y , windowRect . width , windowRect . height ) ;
88
+ libnut . getWindowRect = jest . fn ( ( ) => windowRect ) ;
89
+
90
+ // WHEN
91
+ const wndRegion = SUT . getWindowRegion ( windowHandle ) ;
92
+
93
+ // THEN
94
+ await expect ( libnut . getWindowRect ) . toBeCalledTimes ( 1 ) ;
95
+ await expect ( libnut . getWindowRect ) . toBeCalledWith ( windowHandle ) ;
96
+ await expect ( wndRegion ) . resolves . toStrictEqual ( windowRegion ) ;
97
+ } ) ;
98
+
99
+ it ( "should reject on errors in libnut#getActiveWindow" , async ( ) => {
100
+ // GIVEN
101
+ const SUT = new WindowAction ( ) ;
102
+ const errorMessage = "getWindowRect threw" ;
103
+ const windowHandle = 100 ;
104
+ libnut . getWindowRect = jest . fn ( ( ) => {
105
+ throw new Error ( errorMessage ) ;
106
+ } ) ;
107
+
108
+ // WHEN
109
+ const windows = SUT . getWindowRegion ( windowHandle ) ;
110
+
111
+ // THEN
112
+ await expect ( libnut . getWindowRect ) . toBeCalledTimes ( 1 ) ;
113
+ await expect ( libnut . getWindowRect ) . toBeCalledWith ( windowHandle ) ;
114
+ await expect ( windows ) . rejects . toThrowError ( errorMessage ) ;
115
+ } ) ;
116
+ } ) ;
117
+
118
+ describe ( "getWindowTitle" , ( ) => {
119
+ it ( "should resolve to a window title via libnut#getWindowTitle" , async ( ) => {
120
+ // GIVEN
121
+ const SUT = new WindowAction ( ) ;
122
+ const windowTitle = "test window" ;
123
+ const windowHandle = 42 ;
124
+ libnut . getWindowTitle = jest . fn ( ( ) => windowTitle ) ;
125
+
126
+ // WHEN
127
+ const wndRegion = SUT . getWindowTitle ( windowHandle ) ;
128
+
129
+ // THEN
130
+ await expect ( libnut . getWindowTitle ) . toBeCalledTimes ( 1 ) ;
131
+ await expect ( libnut . getWindowTitle ) . toBeCalledWith ( windowHandle ) ;
132
+ await expect ( wndRegion ) . resolves . toBe ( windowTitle ) ;
133
+ } ) ;
134
+
135
+ it ( "should reject on errors in libnut#getActiveWindow" , async ( ) => {
136
+ // GIVEN
137
+ const SUT = new WindowAction ( ) ;
138
+ const errorMessage = "getWindowRect threw" ;
139
+ const windowHandle = 42 ;
140
+ libnut . getWindowTitle = jest . fn ( ( ) => {
141
+ throw new Error ( errorMessage ) ;
142
+ } ) ;
143
+
144
+ // WHEN
145
+ const windows = SUT . getWindowTitle ( windowHandle ) ;
146
+
147
+ // THEN
148
+ await expect ( libnut . getWindowTitle ) . toBeCalledTimes ( 1 ) ;
149
+ await expect ( libnut . getWindowTitle ) . toBeCalledWith ( windowHandle ) ;
150
+ await expect ( windows ) . rejects . toThrowError ( errorMessage ) ;
151
+ } ) ;
152
+ } ) ;
153
+ } ) ;
0 commit comments