@@ -163,11 +163,73 @@ describe('Acceptance: Setup', function () {
163
163
// it redirects to the home / "content" screen
164
164
expect ( currentURL ( ) , 'url after submitting invites' )
165
165
. to . equal ( '/' ) ;
166
+
167
+ // it displays success alert
168
+ expect ( find ( '.gh-alert-green' ) . length , 'number of success alerts' )
169
+ . to . equal ( 1 ) ;
166
170
} ) ;
167
171
} ) ;
168
172
169
- it ( 'handles server validation errors in step 2' ) ;
170
- it ( 'handles server validation errors in step 3' ) ;
173
+ it ( 'handles validation errors in step 2' , function ( ) {
174
+ let postCount = 0 ;
175
+
176
+ invalidateSession ( application ) ;
177
+ server . loadFixtures ( 'roles' ) ;
178
+
179
+ server . post ( '/authentication/setup' , function ( ) {
180
+ postCount ++ ;
181
+
182
+ // validation error
183
+ if ( postCount === 1 ) {
184
+ return new Mirage . Response ( 422 , { } , {
185
+ errors : [
186
+ {
187
+ errorType : 'ValidationError' ,
188
+ message : 'Server response message'
189
+ }
190
+ ]
191
+ } ) ;
192
+ }
193
+
194
+ // server error
195
+ if ( postCount === 2 ) {
196
+ return new Mirage . Response ( 500 , { } , null ) ;
197
+ }
198
+ } ) ;
199
+
200
+ visit ( '/setup/two' ) ;
201
+ click ( '.btn-green' ) ;
202
+
203
+ andThen ( ( ) => {
204
+ // non-server validation
205
+ expect ( find ( '.main-error' ) . text ( ) . trim ( ) , 'error text' )
206
+ . to . not . be . blank ;
207
+ } ) ;
208
+
209
+ fillIn ( '[name="email"]' , '[email protected] ' ) ;
210
+ fillIn ( '[name="name"]' , 'Test User' ) ;
211
+ fillIn ( '[name="password"]' , 'password' ) ;
212
+ fillIn ( '[name="blog-title"]' , 'Blog Title' ) ;
213
+
214
+ // first post - simulated validation error
215
+ click ( '.btn-green' ) ;
216
+
217
+ andThen ( ( ) => {
218
+ expect ( find ( '.main-error' ) . text ( ) . trim ( ) , 'error text' )
219
+ . to . equal ( 'Server response message' ) ;
220
+ } ) ;
221
+
222
+ // second post - simulated server error
223
+ click ( '.btn-green' ) ;
224
+
225
+ andThen ( ( ) => {
226
+ expect ( find ( '.main-error' ) . text ( ) . trim ( ) , 'error text' )
227
+ . to . be . blank ;
228
+
229
+ expect ( find ( '.gh-alert-red' ) . length , 'number of alerts' )
230
+ . to . equal ( 1 ) ;
231
+ } ) ;
232
+ } ) ;
171
233
172
234
it ( 'handles invalid origin error on step 2' , function ( ) {
173
235
// mimick the API response for an invalid origin
@@ -201,5 +263,140 @@ describe('Acceptance: Setup', function () {
201
263
. to . equal ( 'Access Denied from url: unknown.com. Please use the url configured in config.js.' ) ;
202
264
} ) ;
203
265
} ) ;
266
+
267
+ it ( 'handles validation errors in step 3' , function ( ) {
268
+ let input = '[name="users"]' ;
269
+ let postCount = 0 ;
270
+ let button , formGroup , user ;
271
+
272
+ invalidateSession ( application ) ;
273
+ server . loadFixtures ( 'roles' ) ;
274
+
275
+ server . post ( '/users' , function ( db , request ) {
276
+ let [ params ] = JSON . parse ( request . requestBody ) . users ;
277
+
278
+ postCount ++ ;
279
+
280
+ // invalid
281
+ if ( postCount === 1 ) {
282
+ return new Mirage . Response ( 422 , { } , {
283
+ errors : [
284
+ {
285
+ errorType : 'ValidationError' ,
286
+ message : 'Dummy validation error'
287
+ }
288
+ ]
289
+ } ) ;
290
+ }
291
+
292
+ // valid
293
+ user = db . users . insert ( params ) ;
294
+ return {
295
+ users : [ user ]
296
+ } ;
297
+ } ) ;
298
+
299
+ // complete step 2 so we can access step 3
300
+ visit ( '/setup/two' ) ;
301
+ fillIn ( '[name="email"]' , '[email protected] ' ) ;
302
+ fillIn ( '[name="name"]' , 'Test User' ) ;
303
+ fillIn ( '[name="password"]' , 'password' ) ;
304
+ fillIn ( '[name="blog-title"]' , 'Blog Title' ) ;
305
+ click ( '.btn-green' ) ;
306
+
307
+ // default field/button state
308
+ andThen ( ( ) => {
309
+ formGroup = find ( '.gh-flow-invite .form-group' ) ;
310
+ button = find ( '.gh-flow-invite button[type="submit"]' ) ;
311
+
312
+ expect ( formGroup . hasClass ( 'error' ) , 'default field has error class' )
313
+ . to . be . false ;
314
+
315
+ expect ( button . text ( ) . trim ( ) , 'default button text' )
316
+ . to . equal ( 'Invite some users' ) ;
317
+
318
+ expect ( button . hasClass ( 'btn-minor' ) , 'default button is disabled' )
319
+ . to . be . true ;
320
+ } ) ;
321
+
322
+ // no users submitted state
323
+ click ( '.gh-flow-invite button[type="submit"]' ) ;
324
+
325
+ andThen ( ( ) => {
326
+ expect ( formGroup . hasClass ( 'error' ) , 'no users submitted field has error class' )
327
+ . to . be . true ;
328
+
329
+ expect ( button . text ( ) . trim ( ) , 'no users submitted button text' )
330
+ . to . equal ( 'No users to invite' ) ;
331
+
332
+ expect ( button . hasClass ( 'btn-minor' ) , 'no users submitted button is disabled' )
333
+ . to . be . true ;
334
+ } ) ;
335
+
336
+ // single invalid email
337
+ fillIn ( input , 'invalid email' ) ;
338
+ triggerEvent ( input , 'blur' ) ;
339
+
340
+ andThen ( ( ) => {
341
+ expect ( formGroup . hasClass ( 'error' ) , 'invalid field has error class' )
342
+ . to . be . true ;
343
+
344
+ expect ( button . text ( ) . trim ( ) , 'single invalid button text' )
345
+ . to . equal ( '1 invalid email address' ) ;
346
+
347
+ expect ( button . hasClass ( 'btn-minor' ) , 'invalid email button is disabled' )
348
+ . to . be . true ;
349
+ } ) ;
350
+
351
+ // multiple invalid emails
352
+ fillIn ( input , 'invalid email\nanother invalid address' ) ;
353
+ triggerEvent ( input , 'blur' ) ;
354
+
355
+ andThen ( ( ) => {
356
+ expect ( button . text ( ) . trim ( ) , 'multiple invalid button text' )
357
+ . to . equal ( '2 invalid email addresses' ) ;
358
+ } ) ;
359
+
360
+ // single valid email
361
+ fillIn ( input , '[email protected] ' ) ;
362
+ triggerEvent ( input , 'blur' ) ;
363
+
364
+ andThen ( ( ) => {
365
+ expect ( formGroup . hasClass ( 'error' ) , 'valid field has error class' )
366
+ . to . be . false ;
367
+
368
+ expect ( button . text ( ) . trim ( ) , 'single valid button text' )
369
+ . to . equal ( 'Invite 1 user' ) ;
370
+
371
+ expect ( button . hasClass ( 'btn-green' ) , 'valid email button is enabled' )
372
+ . to . be . true ;
373
+ } ) ;
374
+
375
+ // multiple valid emails
376
+
377
+ triggerEvent ( input , 'blur' ) ;
378
+
379
+ andThen ( ( ) => {
380
+ expect ( button . text ( ) . trim ( ) , 'multiple valid button text' )
381
+ . to . equal ( 'Invite 2 users' ) ;
382
+ } ) ;
383
+
384
+ // submit invitations with simulated failure on 1 invite
385
+ click ( '.btn-green' ) ;
386
+
387
+ andThen ( ( ) => {
388
+ // it redirects to the home / "content" screen
389
+ expect ( currentURL ( ) , 'url after submitting invites' )
390
+ . to . equal ( '/' ) ;
391
+
392
+ // it displays success alert
393
+ expect ( find ( '.gh-alert-green' ) . length , 'number of success alerts' )
394
+ . to . equal ( 1 ) ;
395
+
396
+ // it displays failure alert
397
+ expect ( find ( '.gh-alert-red' ) . length , 'number of failure alerts' )
398
+ . to . equal ( 1 ) ;
399
+ } ) ;
400
+ } ) ;
204
401
} ) ;
205
402
} ) ;
0 commit comments