@@ -6,30 +6,43 @@ import Pretender from 'pretender';
6
6
import wait from 'ember-test-helpers/wait' ;
7
7
import FeatureService , { feature } from 'ghost/services/feature' ;
8
8
import Ember from 'ember' ;
9
+ import { errorOverride , errorReset } from 'ghost/tests/helpers/adapter-error' ;
9
10
10
11
const { merge, run} = Ember ;
12
+ const EmberError = Ember . Error ;
13
+
14
+ function stubSettings ( server , labs , validSave = true , validSettings = true ) {
15
+ let settings = [
16
+ {
17
+ id : '1' ,
18
+ type : 'blog' ,
19
+ key : 'labs' ,
20
+ value : JSON . stringify ( labs )
21
+ }
22
+ ] ;
23
+
24
+ if ( validSettings ) {
25
+ settings . push ( {
26
+ id : '2' ,
27
+ type : 'blog' ,
28
+ key : 'postsPerPage' ,
29
+ value : 1
30
+ } ) ;
31
+ }
11
32
12
- function stubSettings ( server , labs ) {
13
33
server . get ( '/ghost/api/v0.1/settings/' , function ( ) {
14
- return [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( { settings : [
15
- {
16
- id : '1' ,
17
- type : 'blog' ,
18
- key : 'labs' ,
19
- value : JSON . stringify ( labs )
20
- } ,
21
- // postsPerPage is needed to satisfy the validation
22
- {
23
- id : '2' ,
24
- type : 'blog' ,
25
- key : 'postsPerPage' ,
26
- value : 1
27
- }
28
- ] } ) ] ;
34
+ return [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( { settings} ) ] ;
29
35
} ) ;
30
36
31
37
server . put ( '/ghost/api/v0.1/settings/' , function ( request ) {
32
- return [ 200 , { 'Content-Type' : 'application/json' } , request . requestBody ] ;
38
+ let statusCode = ( validSave ) ? 200 : 400 ;
39
+ let response = ( validSave ) ? request . requestBody : JSON . stringify ( {
40
+ errors : [ {
41
+ message : 'Test Error'
42
+ } ]
43
+ } ) ;
44
+
45
+ return [ statusCode , { 'Content-Type' : 'application/json' } , response ] ;
33
46
} ) ;
34
47
}
35
48
@@ -182,15 +195,63 @@ describeModule(
182
195
return wait ( ) . then ( ( ) => {
183
196
expect ( server . handlers [ 1 ] . numberOfCalls ) . to . equal ( 1 ) ;
184
197
185
- // TODO: failing because service.update only sets values on
186
198
service . get ( 'testFlag' ) . then ( ( testFlag ) => {
187
199
expect ( testFlag ) . to . be . true ;
188
200
done ( ) ;
189
201
} ) ;
190
202
} ) ;
191
203
} ) ;
192
204
193
- it ( 'notifies for server errors' ) ;
194
- it ( 'notifies for validation errors' ) ;
205
+ it ( 'notifies for server errors' , function ( done ) {
206
+ stubSettings ( server , { testFlag : false } , false ) ;
207
+ addTestFlag ( ) ;
208
+
209
+ let service = this . subject ( ) ;
210
+
211
+ run ( ( ) => {
212
+ service . get ( 'testFlag' ) . then ( ( testFlag ) => {
213
+ expect ( testFlag ) . to . be . false ;
214
+ } ) ;
215
+ } ) ;
216
+
217
+ run ( ( ) => {
218
+ service . set ( 'testFlag' , true ) ;
219
+ } ) ;
220
+
221
+ return wait ( ) . then ( ( ) => {
222
+ expect ( server . handlers [ 1 ] . numberOfCalls ) . to . equal ( 1 ) ;
223
+
224
+ expect ( service . get ( 'notifications.notifications' ) . length ) . to . equal ( 1 ) ;
225
+
226
+ service . get ( 'testFlag' ) . then ( ( testFlag ) => {
227
+ expect ( testFlag ) . to . be . false ;
228
+ done ( ) ;
229
+ } ) ;
230
+ } ) ;
231
+ } ) ;
232
+
233
+ it ( 'notifies for validation errors' , function ( done ) {
234
+ stubSettings ( server , { testFlag : false } , true , false ) ;
235
+ addTestFlag ( ) ;
236
+
237
+ let service = this . subject ( ) ;
238
+
239
+ run ( ( ) => {
240
+ service . get ( 'testFlag' ) . then ( ( testFlag ) => {
241
+ expect ( testFlag ) . to . be . false ;
242
+ } ) ;
243
+ } ) ;
244
+
245
+ run ( ( ) => {
246
+ expect ( ( ) => {
247
+ service . set ( 'testFlag' , true ) ;
248
+ } , EmberError , 'Threw validation error' ) ;
249
+ } ) ;
250
+
251
+ service . get ( 'testFlag' ) . then ( ( testFlag ) => {
252
+ expect ( testFlag ) . to . be . false ;
253
+ done ( ) ;
254
+ } ) ;
255
+ } ) ;
195
256
}
196
257
) ;
0 commit comments