@@ -4,13 +4,27 @@ jest.dontMock('../decode');
4
4
jest . dontMock ( '../encode' ) ;
5
5
jest . dontMock ( '../ParseError' ) ;
6
6
jest . dontMock ( '../ParseObject' ) ;
7
+ jest . dontMock ( '../RESTController' ) ;
7
8
8
9
const Hooks = require ( '../ParseHooks' ) ;
9
10
const CoreManager = require ( '../CoreManager' ) ;
11
+ const RESTController = require ( '../RESTController' ) ;
10
12
11
13
const defaultController = CoreManager . getHooksController ( ) ;
12
14
const { sendRequest } = defaultController ;
13
15
16
+ CoreManager . setInstallationController ( {
17
+ currentInstallationId ( ) {
18
+ return Promise . resolve ( 'iid' ) ;
19
+ } ,
20
+ currentInstallation ( ) { } ,
21
+ updateInstallationOnDisk ( ) { } ,
22
+ } ) ;
23
+ CoreManager . set ( 'APPLICATION_ID' , 'A' ) ;
24
+ CoreManager . set ( 'JAVASCRIPT_KEY' , 'B' ) ;
25
+ CoreManager . set ( 'MASTER_KEY' , 'C' ) ;
26
+ CoreManager . set ( 'VERSION' , 'V' ) ;
27
+
14
28
describe ( 'Hooks' , ( ) => {
15
29
beforeEach ( ( ) => {
16
30
const run = jest . fn ( ) ;
@@ -19,125 +33,176 @@ describe('Hooks', () => {
19
33
result : { } ,
20
34
} )
21
35
) ;
36
+ const ajax = jest . fn ( ) ;
37
+ ajax . mockReturnValue (
38
+ Promise . resolve ( {
39
+ response : { } ,
40
+ } )
41
+ ) ;
42
+ RESTController . ajax = ajax ;
22
43
defaultController . sendRequest = run ;
23
44
CoreManager . setHooksController ( defaultController ) ;
45
+ CoreManager . setRESTController ( RESTController ) ;
24
46
} ) ;
25
47
26
- it ( 'shoud properly build GET functions' , ( ) => {
48
+ it ( 'should properly build GET functions' , async ( ) => {
27
49
Hooks . getFunctions ( ) ;
28
50
29
51
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
30
52
'GET' ,
31
- '/ hooks/functions' ,
53
+ 'hooks/functions' ,
32
54
] ) ;
55
+ defaultController . sendRequest = sendRequest ;
56
+ await Hooks . getFunctions ( ) ;
57
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'https://api.parse.com/1/hooks/functions' ) ;
33
58
} ) ;
34
59
35
- it ( 'shoud properly build GET triggers' , ( ) => {
60
+ it ( 'should properly build GET triggers' , async ( ) => {
36
61
Hooks . getTriggers ( ) ;
37
62
38
63
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
39
64
'GET' ,
40
- '/ hooks/triggers' ,
65
+ 'hooks/triggers' ,
41
66
] ) ;
67
+ defaultController . sendRequest = sendRequest ;
68
+ await Hooks . getTriggers ( ) ;
69
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'https://api.parse.com/1/hooks/triggers' ) ;
42
70
} ) ;
43
71
44
- it ( 'shoud properly build GET function' , ( ) => {
72
+ it ( 'should properly build GET function' , async ( ) => {
45
73
Hooks . getFunction ( 'functionName' ) ;
46
74
47
75
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
48
76
'GET' ,
49
- '/ hooks/functions/functionName' ,
77
+ 'hooks/functions/functionName' ,
50
78
] ) ;
79
+ defaultController . sendRequest = sendRequest ;
80
+ await Hooks . getFunction ( 'functionName' ) ;
81
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
82
+ 'https://api.parse.com/1/hooks/functions/functionName'
83
+ ) ;
51
84
} ) ;
52
85
53
- it ( 'shoud properly build GET trigger' , ( ) => {
86
+ it ( 'should properly build GET trigger' , async ( ) => {
54
87
Hooks . getTrigger ( 'MyClass' , 'beforeSave' ) ;
55
88
56
89
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
57
90
'GET' ,
58
- '/ hooks/triggers/MyClass/beforeSave' ,
91
+ 'hooks/triggers/MyClass/beforeSave' ,
59
92
] ) ;
93
+ defaultController . sendRequest = sendRequest ;
94
+ await Hooks . getTrigger ( 'MyClass' , 'beforeSave' ) ;
95
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
96
+ 'https://api.parse.com/1/hooks/triggers/MyClass/beforeSave'
97
+ ) ;
60
98
} ) ;
61
99
62
- it ( 'shoud properly build POST function' , ( ) => {
63
- Hooks . createFunction ( 'myFunction' , 'https://dummy .com' ) ;
100
+ it ( 'should properly build POST function' , async ( ) => {
101
+ Hooks . createFunction ( 'myFunction' , 'https://example .com' ) ;
64
102
65
103
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
66
104
'POST' ,
67
- '/ hooks/functions' ,
105
+ 'hooks/functions' ,
68
106
{
69
107
functionName : 'myFunction' ,
70
- url : 'https://dummy .com' ,
108
+ url : 'https://example .com' ,
71
109
} ,
72
110
] ) ;
111
+ defaultController . sendRequest = sendRequest ;
112
+ await Hooks . createFunction ( 'myFunction' , 'https://example.com' ) ;
113
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'https://api.parse.com/1/hooks/functions' ) ;
73
114
} ) ;
74
115
75
- it ( 'shoud properly build POST trigger' , ( ) => {
76
- Hooks . createTrigger ( 'MyClass' , 'beforeSave' , 'https://dummy .com' ) ;
116
+ it ( 'should properly build POST trigger' , async ( ) => {
117
+ Hooks . createTrigger ( 'MyClass' , 'beforeSave' , 'https://example .com' ) ;
77
118
78
119
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
79
120
'POST' ,
80
- '/ hooks/triggers' ,
121
+ 'hooks/triggers' ,
81
122
{
82
123
className : 'MyClass' ,
83
124
triggerName : 'beforeSave' ,
84
- url : 'https://dummy .com' ,
125
+ url : 'https://example .com' ,
85
126
} ,
86
127
] ) ;
128
+ defaultController . sendRequest = sendRequest ;
129
+ await Hooks . createTrigger ( 'MyClass' , 'beforeSave' , 'https://example.com' ) ;
130
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'https://api.parse.com/1/hooks/triggers' ) ;
87
131
} ) ;
88
132
89
- it ( 'shoud properly build PUT function' , ( ) => {
90
- Hooks . updateFunction ( 'myFunction' , 'https://dummy .com' ) ;
133
+ it ( 'should properly build PUT function' , async ( ) => {
134
+ Hooks . updateFunction ( 'myFunction' , 'https://example .com' ) ;
91
135
92
136
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
93
137
'PUT' ,
94
- '/ hooks/functions/myFunction' ,
138
+ 'hooks/functions/myFunction' ,
95
139
{
96
- url : 'https://dummy .com' ,
140
+ url : 'https://example .com' ,
97
141
} ,
98
142
] ) ;
143
+ defaultController . sendRequest = sendRequest ;
144
+ await Hooks . updateFunction ( 'myFunction' , 'https://example.com' ) ;
145
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
146
+ 'https://api.parse.com/1/hooks/functions/myFunction'
147
+ ) ;
99
148
} ) ;
100
149
101
- it ( 'shoud properly build PUT trigger' , ( ) => {
102
- Hooks . updateTrigger ( 'MyClass' , 'beforeSave' , 'https://dummy .com' ) ;
150
+ it ( 'should properly build PUT trigger' , async ( ) => {
151
+ Hooks . updateTrigger ( 'MyClass' , 'beforeSave' , 'https://example .com' ) ;
103
152
104
153
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
105
154
'PUT' ,
106
- '/ hooks/triggers/MyClass/beforeSave' ,
155
+ 'hooks/triggers/MyClass/beforeSave' ,
107
156
{
108
- url : 'https://dummy .com' ,
157
+ url : 'https://example .com' ,
109
158
} ,
110
159
] ) ;
160
+ defaultController . sendRequest = sendRequest ;
161
+ await Hooks . updateTrigger ( 'MyClass' , 'beforeSave' , 'https://example.com' ) ;
162
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
163
+ 'https://api.parse.com/1/hooks/triggers/MyClass/beforeSave'
164
+ ) ;
111
165
} ) ;
112
166
113
- it ( 'shoud properly build removeFunction' , ( ) => {
167
+ it ( 'should properly build removeFunction' , async ( ) => {
114
168
Hooks . removeFunction ( 'myFunction' ) ;
115
169
116
170
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
117
171
'PUT' ,
118
- '/ hooks/functions/myFunction' ,
172
+ 'hooks/functions/myFunction' ,
119
173
{ __op : 'Delete' } ,
120
174
] ) ;
175
+
176
+ defaultController . sendRequest = sendRequest ;
177
+ await Hooks . removeFunction ( 'myFunction' ) ;
178
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
179
+ 'https://api.parse.com/1/hooks/functions/myFunction'
180
+ ) ;
121
181
} ) ;
122
182
123
- it ( 'shoud properly build removeTrigger' , ( ) => {
183
+ it ( 'should properly build removeTrigger' , async ( ) => {
124
184
Hooks . removeTrigger ( 'MyClass' , 'beforeSave' ) ;
125
185
126
186
expect ( CoreManager . getHooksController ( ) . sendRequest . mock . calls [ 0 ] ) . toEqual ( [
127
187
'PUT' ,
128
- '/ hooks/triggers/MyClass/beforeSave' ,
188
+ 'hooks/triggers/MyClass/beforeSave' ,
129
189
{ __op : 'Delete' } ,
130
190
] ) ;
191
+ defaultController . sendRequest = sendRequest ;
192
+ await Hooks . removeTrigger ( 'MyClass' , 'beforeSave' ) ;
193
+ expect ( RESTController . ajax . mock . calls [ 0 ] [ 1 ] ) . toBe (
194
+ 'https://api.parse.com/1/hooks/triggers/MyClass/beforeSave'
195
+ ) ;
131
196
} ) ;
132
197
133
- it ( 'shoud throw invalid create' , async ( ) => {
198
+ it ( 'should throw invalid create' , async ( ) => {
134
199
expect . assertions ( 10 ) ;
135
200
const p1 = Hooks . create ( { functionName : 'myFunction' } ) . catch ( err => {
136
201
expect ( err . code ) . toBe ( 143 ) ;
137
202
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
138
203
} ) ;
139
204
140
- const p2 = Hooks . create ( { url : 'http://dummy .com' } ) . catch ( err => {
205
+ const p2 = Hooks . create ( { url : 'http://example .com' } ) . catch ( err => {
141
206
expect ( err . code ) . toBe ( 143 ) ;
142
207
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
143
208
} ) ;
@@ -147,7 +212,7 @@ describe('Hooks', () => {
147
212
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
148
213
} ) ;
149
214
150
- const p4 = Hooks . create ( { className : 'MyClass' , url : 'http://dummy .com' } ) . catch ( err => {
215
+ const p4 = Hooks . create ( { className : 'MyClass' , url : 'http://example .com' } ) . catch ( err => {
151
216
expect ( err . code ) . toBe ( 143 ) ;
152
217
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
153
218
} ) ;
@@ -160,7 +225,7 @@ describe('Hooks', () => {
160
225
await Promise . all ( [ p1 , p2 , p3 , p4 , p5 ] ) ;
161
226
} ) ;
162
227
163
- it ( 'shoud throw invalid update' , async ( ) => {
228
+ it ( 'should throw invalid update' , async ( ) => {
164
229
expect . assertions ( 6 ) ;
165
230
const p1 = Hooks . update ( { functionssName : 'myFunction' } ) . catch ( err => {
166
231
expect ( err . code ) . toBe ( 143 ) ;
@@ -172,14 +237,14 @@ describe('Hooks', () => {
172
237
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
173
238
} ) ;
174
239
175
- const p3 = Hooks . update ( { className : 'MyClass' , url : 'http://dummy .com' } ) . catch ( err => {
240
+ const p3 = Hooks . update ( { className : 'MyClass' , url : 'http://example .com' } ) . catch ( err => {
176
241
expect ( err . code ) . toBe ( 143 ) ;
177
242
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
178
243
} ) ;
179
244
await Promise . all ( [ p1 , p2 , p3 ] ) ;
180
245
} ) ;
181
246
182
- it ( 'shoud throw invalid remove' , async ( ) => {
247
+ it ( 'should throw invalid remove' , async ( ) => {
183
248
expect . assertions ( 6 ) ;
184
249
const p1 = Hooks . remove ( { functionssName : 'myFunction' } ) . catch ( err => {
185
250
expect ( err . code ) . toBe ( 143 ) ;
@@ -191,7 +256,7 @@ describe('Hooks', () => {
191
256
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
192
257
} ) ;
193
258
194
- const p3 = Hooks . remove ( { className : 'MyClass' , url : 'http://dummy .com' } ) . catch ( err => {
259
+ const p3 = Hooks . remove ( { className : 'MyClass' , url : 'http://example .com' } ) . catch ( err => {
195
260
expect ( err . code ) . toBe ( 143 ) ;
196
261
expect ( err . error ) . toBe ( 'invalid hook declaration' ) ;
197
262
} ) ;
0 commit comments