@@ -3,17 +3,17 @@ import chai, { expect, assert } from 'chai';
3
3
import chaiExclude from 'chai-exclude' ;
4
4
import chaiAsPromised from 'chai-as-promised' ;
5
5
import { sleep , makeConnectionTests } from './helpers' ;
6
+ import sinon from 'sinon' ;
6
7
7
8
chai . use ( chaiExclude ) ;
8
9
chai . use ( sinonChai ) ;
9
10
chai . use ( chaiAsPromised ) ;
10
11
11
12
makeConnectionTests ( 'Initalize Tests' , function ( ) {
12
13
it ( 'initalize request constructed correctly' , async function ( ) {
13
- this . test . module . initialize ( 'test-module' , '1.0.0' , { } ) ;
14
+ this . test . module . initialize ( 'test-module' , '1.0.0' ) ;
14
15
await sleep ( 0 ) ;
15
- assert ( this . test . sendFunc . calledOnce ) ;
16
- const message = this . test . sendFunc . getCall ( 0 ) . args [ 0 ] ;
16
+ const message = this . test . getLatestSent ( ) ;
17
17
expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
18
18
type : 'moduleRegistration' ,
19
19
moduleId : 'test-module' ,
@@ -22,6 +22,24 @@ makeConnectionTests('Initalize Tests', function () {
22
22
} ) ;
23
23
} ) ;
24
24
25
+ it ( 'initialize with deps constructed correctly' , async function ( ) {
26
+ this . test . module . initialize ( 'test-module' , '1.0.0' , {
27
+ 'test-module2' : '1.0.1' ,
28
+ 'test-module3' : '1.0.2'
29
+ } ) ;
30
+ await sleep ( 0 ) ;
31
+ const message = this . test . getLatestSent ( ) ;
32
+ expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
33
+ type : 'moduleRegistration' ,
34
+ moduleId : 'test-module' ,
35
+ version : '1.0.0' ,
36
+ dependencies : {
37
+ 'test-module2' : '1.0.1' ,
38
+ 'test-module3' : '1.0.2'
39
+ }
40
+ } ) ;
41
+ } ) ;
42
+
25
43
it ( 'Does not allow to send other request without initializing' , async function ( ) {
26
44
await expect (
27
45
this . test . module . declareFunction ( 'test' , ( ) => { } )
@@ -36,35 +54,49 @@ makeConnectionTests('Initalize Tests', function () {
36
54
this . test . module . triggerHook ( 'test' )
37
55
) . to . be . rejectedWith ( Error ) ;
38
56
} ) ;
57
+
58
+ it ( 'Initialize resolves correctly/Cannot initalize twice' , async function ( ) {
59
+ const p = this . test . module . initialize ( 'test-module1' , '1.0.0' , { } ) ;
60
+ await sleep ( 0 ) ;
61
+ const requestId = this . test . getLatestSent ( ) . requestId ;
62
+ this . test . conn . sendResponse ( {
63
+ requestId,
64
+ type : 'moduleRegistered'
65
+ } ) ;
66
+ expect ( p ) . to . eventually . equal ( true ) ;
67
+ return await expect (
68
+ this . test . module . initialize ( 'test-module2' , '1.0.0' , { } )
69
+ ) . to . be . rejectedWith ( Error ) ;
70
+ } ) ;
39
71
} , false ) ;
40
72
41
73
makeConnectionTests ( 'Test if requests constructed correctly' , function ( ) {
42
74
it ( 'declareFunction' , async function ( ) {
43
75
this . test . module . declareFunction ( 'test_fn' , ( ) => { } ) ;
44
- const message = this . test . sendFunc . getCall ( 0 ) . args [ 0 ] ;
76
+ const message = this . test . getLatestSent ( ) ;
45
77
expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
46
- function : "test_fn" ,
47
- type : "declareFunction"
78
+ function : "test_fn" ,
79
+ type : "declareFunction"
48
80
} ) ;
49
81
} ) ;
50
82
51
83
52
- it ( 'functionCall with empty args' , async function ( ) {
84
+ it ( 'functionCall with empty args' , async function ( ) {
53
85
this . test . module . functionCall ( 'module.test_fn' ) ;
54
- const message = this . test . sendFunc . getCall ( 0 ) . args [ 0 ] ;
86
+ const message = this . test . getLatestSent ( ) ;
55
87
expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
56
- function : "module.test_fn" ,
57
- type : "functionCall" ,
58
- arguments : { }
88
+ function : "module.test_fn" ,
89
+ type : "functionCall" ,
90
+ arguments : { }
59
91
} ) ;
60
92
} ) ;
61
93
62
- it ( 'functionCall with args' , async function ( ) {
94
+ it ( 'functionCall with args' , async function ( ) {
63
95
this . test . module . functionCall ( 'module.test_fn' , {
64
96
a : 1 ,
65
97
b : 2
66
98
} ) ;
67
- const message = this . test . sendFunc . getCall ( 0 ) . args [ 0 ] ;
99
+ const message = this . test . getLatestSent ( ) ;
68
100
expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
69
101
function : "module.test_fn" ,
70
102
type : "functionCall" ,
@@ -74,4 +106,104 @@ makeConnectionTests('Test if requests constructed correctly', function () {
74
106
}
75
107
} ) ;
76
108
} ) ;
109
+
110
+ it ( 'registerHook' , async function ( ) {
111
+ this . test . module . registerHook ( 'test_hook' , ( ) => { } ) ;
112
+ const message = this . test . getLatestSent ( ) ;
113
+ expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
114
+ hook : "test_hook" ,
115
+ type : "registerHook" ,
116
+ } ) ;
117
+ } ) ;
118
+
119
+ it ( 'triggerHook' , async function ( ) {
120
+ this . test . module . triggerHook ( 'test_hook' ) ;
121
+ const message = this . test . getLatestSent ( ) ;
122
+ expect ( message ) . excluding ( 'requestId' ) . to . deep . equal ( {
123
+ type : "triggerHook" ,
124
+ hook : 'test_hook'
125
+ } ) ;
126
+ } ) ;
127
+ } ) ;
128
+
129
+
130
+ makeConnectionTests ( 'Test if responses from gotham parsed correctly' , async function ( ) {
131
+ it ( 'declareFunction' , async function ( ) {
132
+ const p = this . test . module . declareFunction ( 'test_fn' , ( ) => { } ) ;
133
+ const requestId = this . test . getLatestSent ( ) . requestId ;
134
+ await sleep ( 0 ) ;
135
+ this . test . conn . sendResponse ( {
136
+ requestId,
137
+ type : 'functionDeclared' ,
138
+ function : 'test_fn'
139
+ } ) ;
140
+ return expect ( p ) . to . eventually . equal ( true ) ;
141
+ } ) ;
142
+ it ( 'hookRegistered' , async function ( ) {
143
+ const p = this . test . module . registerHook ( 'test_hook' , ( ) => { } ) ;
144
+ await sleep ( 0 ) ;
145
+ const requestId = this . test . getLatestSent ( ) . requestId ;
146
+ this . test . conn . sendResponse ( {
147
+ requestId,
148
+ type : 'hookRegistered' ,
149
+ } ) ;
150
+
151
+ return expect ( p ) . to . eventually . equal ( true ) ;
152
+ } ) ;
153
+
154
+ it ( 'hookTriggered' , async function ( ) {
155
+ const fn = sinon . fake ( ) ;
156
+ const p = this . test . module . registerHook ( 'test_hook' , fn ) ;
157
+ await sleep ( 0 ) ;
158
+ this . test . conn . sendResponse ( {
159
+ requestId : '12345' ,
160
+ type : 'hookTriggered' ,
161
+ hook : 'test_hook'
162
+ } ) ;
163
+ // await sleep(0);
164
+ assert ( fn . calledOnce ) ;
165
+ } ) ;
166
+
167
+ it ( 'functionCall' , async function ( ) {
168
+ const fn = sinon . fake ( ) ;
169
+ const p = this . test . module . declareFunction ( 'test_fn' , fn ) ;
170
+ await sleep ( 0 ) ;
171
+
172
+ this . test . conn . sendResponse ( {
173
+ requestId : '12345' ,
174
+ type : 'functionCall' ,
175
+ function : 'test_fn'
176
+ } ) ;
177
+
178
+ assert ( fn . calledOnce ) ;
179
+
180
+ this . test . conn . sendResponse ( {
181
+ requestId : '12345' ,
182
+ type : 'functionCall' ,
183
+ function : 'test_fn' ,
184
+ arguments : { a : 1 , b : 2 }
185
+ } ) ;
186
+
187
+ expect ( fn . getCall ( 1 ) . args [ 0 ] ) . to . deep . equal ( { a : 1 , b : 2 } ) ;
188
+ } ) ;
189
+
190
+ it ( 'functionResponse' , async function ( ) {
191
+ const p = this . test . module . functionCall ( 'module.test_fn' ) ;
192
+ await sleep ( 0 ) ;
193
+ const requestId = this . test . getLatestSent ( ) . requestId ;
194
+
195
+ this . test . conn . sendResponse ( {
196
+ requestId,
197
+ type : 'functionResponse' ,
198
+ data : {
199
+ a :1 ,
200
+ b : 2
201
+ }
202
+ } ) ;
203
+
204
+ return expect ( p ) . to . eventually . deep . equal ( {
205
+ a :1 ,
206
+ b :2
207
+ } ) ;
208
+ } ) ;
77
209
} ) ;
0 commit comments