@@ -24,7 +24,7 @@ describe('Test Save Client', () => {
2424 let axios = require ( 'axios' ) ;
2525 let MockAdapter = require ( 'axios-mock-adapter' ) ;
2626
27- it ( 'Client should show error when fails to create client' , ( done ) => {
27+ it ( 'Client should show error when fails to save client' , ( done ) => {
2828
2929 let Client ;
3030 let component ;
@@ -83,7 +83,7 @@ describe('Test Save Client', () => {
8383 setTimeout ( ( ) => {
8484
8585 try {
86- expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error trying create client' ) ;
86+ expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error trying save client' ) ;
8787 done ( ) ;
8888 } catch ( e ) {
8989 console . log ( e ) ;
@@ -92,7 +92,7 @@ describe('Test Save Client', () => {
9292 } ) ;
9393
9494
95- it ( 'Client should show error when fails to create client' , ( done ) => {
95+ it ( 'Client should show error when fails to save client' , ( done ) => {
9696
9797 let Client ;
9898 let component ;
@@ -121,7 +121,7 @@ describe('Test Save Client', () => {
121121
122122 mockAdapter
123123 . onGet ( HOST + '/api/v1/area' ) . reply ( 200 , response )
124- . onPost ( HOST + '/api/v1/client' ) . reply ( 503 , { error : 'Error create client' } ) ;
124+ . onPost ( HOST + '/api/v1/client' ) . reply ( 503 , { error : 'Error save client' } ) ;
125125
126126 Client = require ( 'components/Client/Save/Client' ) . default ;
127127
@@ -151,7 +151,7 @@ describe('Test Save Client', () => {
151151 setTimeout ( ( ) => {
152152
153153 try {
154- expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error create client' ) ;
154+ expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error save client' ) ;
155155 done ( ) ;
156156 } catch ( e ) {
157157 console . log ( e ) ;
@@ -336,5 +336,255 @@ describe('Test Save Client', () => {
336336 } , 0 ) ;
337337 } ) ;
338338
339+ it ( 'Client should show client loaded' , ( done ) => {
340+
341+ let Client ;
342+ let component ;
343+ let mockAdapter = new MockAdapter ( axios ) ;
344+ let response = {
345+ areas : [
346+ {
347+ _id : "Center" ,
348+ parent : "" ,
349+ __v : 0 ,
350+ ancestors : [ ]
351+ } ,
352+ {
353+ _id : "South" ,
354+ parent : "" ,
355+ __v : 0 ,
356+ ancestors : [ ]
357+ }
358+ ]
359+ } ;
360+ let clientResponse = {
361+ client : [
362+ {
363+ _id : "58407735f7b50400132d64e7" ,
364+ updatedAt : "2016-12-01T19:17:09.262Z" ,
365+ createdAt : "2016-12-01T19:17:09.262Z" ,
366+ name : "lfdjal" ,
367+ address : "fdjaljfdal" ,
368+ city : "jfladkjl" ,
369+ area : {
370+ _id : "Test" ,
371+ parent : "South" ,
372+ __v : 0 ,
373+ ancestors : [ ]
374+ } ,
375+ frequency : 11 ,
376+ ability : 300 ,
377+ __v : 0
378+ }
379+ ]
380+ } ;
381+ let expectedClientState = Object . assign ( { } , ...clientResponse . client ) ;
382+ expectedClientState . updatedAt = '' ;
383+ expectedClientState . createdAt = '' ;
384+ expectedClientState . area = 'Test' ;
385+
386+ mockAdapter
387+ . onGet ( HOST + '/api/v1/area' ) . reply ( 200 , response )
388+ . onGet ( HOST + '/api/v1/client/1' ) . reply ( 200 , clientResponse )
389+
390+ Client = require ( 'components/Client/Save/Client' ) . default ;
391+
392+ component = shallow (
393+ < Client params = { { id : 1 } } /> ,
394+ { context }
395+ ) ;
396+
397+ setTimeout ( ( ) => {
398+
399+ try {
400+ component . update ( ) ;
401+ expect ( component . state ( ) . client ) . toEqual ( expectedClientState ) ;
402+ expect ( component . find ( 'input' ) . at ( 0 ) . props ( ) . value ) . toEqual ( 'lfdjal' ) ;
403+ expect ( component . find ( 'input' ) . at ( 1 ) . props ( ) . value ) . toEqual ( undefined ) ;
404+ expect ( component . find ( 'input' ) . at ( 3 ) . props ( ) . value ) . toEqual ( 'jfladkjl' ) ;
405+ expect ( component . find ( 'input' ) . at ( 4 ) . props ( ) . value ) . toEqual ( 11 ) ;
406+ expect ( component . find ( 'input' ) . at ( 5 ) . props ( ) . value ) . toEqual ( 300 ) ;
407+ done ( ) ;
408+ } catch ( e ) {
409+ console . log ( e ) ;
410+ }
411+ } , 0 ) ;
412+ } ) ;
413+
414+ it ( 'Client should show error message' , ( done ) => {
415+
416+ let Client ;
417+ let component ;
418+ let mockAdapter = new MockAdapter ( axios ) ;
419+ let response = {
420+ areas : [
421+ {
422+ _id : "Center" ,
423+ parent : "" ,
424+ __v : 0 ,
425+ ancestors : [ ]
426+ } ,
427+ {
428+ _id : "South" ,
429+ parent : "" ,
430+ __v : 0 ,
431+ ancestors : [ ]
432+ }
433+ ]
434+ } ;
435+
436+ mockAdapter
437+ . onGet ( HOST + '/api/v1/area' ) . reply ( 200 , response )
438+ . onGet ( HOST + '/api/v1/client/1' ) . reply ( 503 , { error : 'Error on server' } )
439+
440+ Client = require ( 'components/Client/Save/Client' ) . default ;
441+
442+ component = shallow (
443+ < Client params = { { id : 1 } } /> ,
444+ { context }
445+ ) ;
446+
447+ setTimeout ( ( ) => {
448+
449+ try {
450+ component . update ( ) ;
451+ expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error on server' ) ;
452+ done ( ) ;
453+ } catch ( e ) {
454+ console . log ( e ) ;
455+ }
456+ } , 0 ) ;
457+ } ) ;
458+
459+ it ( 'Client should show default error message when error to find client' , ( done ) => {
460+
461+ let Client ;
462+ let component ;
463+ let mockAdapter = new MockAdapter ( axios ) ;
464+ let response = {
465+ areas : [
466+ {
467+ _id : "Center" ,
468+ parent : "" ,
469+ __v : 0 ,
470+ ancestors : [ ]
471+ } ,
472+ {
473+ _id : "South" ,
474+ parent : "" ,
475+ __v : 0 ,
476+ ancestors : [ ]
477+ }
478+ ]
479+ } ;
480+
481+ mockAdapter
482+ . onGet ( HOST + '/api/v1/area' ) . reply ( 200 , response )
483+ . onGet ( HOST + '/api/v1/client/1' ) . reply ( 503 ) ;
484+
485+ Client = require ( 'components/Client/Save/Client' ) . default ;
486+
487+ component = shallow (
488+ < Client params = { { id : 1 } } /> ,
489+ { context }
490+ ) ;
491+
492+ setTimeout ( ( ) => {
493+
494+ try {
495+ component . update ( ) ;
496+ expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error Found: Trying get client 1' ) ;
497+ done ( ) ;
498+ } catch ( e ) {
499+ console . log ( e ) ;
500+ }
501+ } , 0 ) ;
502+ } ) ;
503+
504+ it ( 'Client should update client' , ( done ) => {
505+
506+ let Client ;
507+ let component ;
508+ let selectArea ;
509+ let mockAdapter = new MockAdapter ( axios ) ;
510+ let response = {
511+ areas : [
512+ {
513+ _id : "Center" ,
514+ parent : "" ,
515+ __v : 0 ,
516+ ancestors : [ ]
517+ } ,
518+ {
519+ _id : "South" ,
520+ parent : "" ,
521+ __v : 0 ,
522+ ancestors : [ ]
523+ }
524+ ]
525+ } ;
526+ let clientResponse = {
527+ client : [
528+ {
529+ _id : "58407735f7b50400132d64e7" ,
530+ updatedAt : "2016-12-01T19:17:09.262Z" ,
531+ createdAt : "2016-12-01T19:17:09.262Z" ,
532+ name : "lfdjal" ,
533+ address : "fdjaljfdal" ,
534+ city : "jfladkjl" ,
535+ area : {
536+ _id : "Test" ,
537+ parent : "South" ,
538+ __v : 0 ,
539+ ancestors : [ ]
540+ } ,
541+ frequency : 11 ,
542+ ability : 300 ,
543+ __v : 0
544+ }
545+ ]
546+ } ;
547+ let expectedClientState = Object . assign ( { } , ...clientResponse . client ) ;
548+ expectedClientState . updatedAt = '' ;
549+ expectedClientState . createdAt = '' ;
550+ expectedClientState . area = {
551+ _id : "Center" ,
552+ parent : "" ,
553+ __v : 0 ,
554+ ancestors : [ ]
555+ } ;
556+
557+ mockAdapter
558+ . onGet ( HOST + '/api/v1/area' ) . reply ( 200 , response )
559+ . onGet ( HOST + '/api/v1/client/1' ) . reply ( 200 , clientResponse )
560+ . onPost ( HOST + '/api/v1/client' ) . reply ( 403 )
561+ . onPut ( HOST + '/api/v1/client' ) . reply ( 204 )
562+
563+ Client = require ( 'components/Client/Save/Client' ) . default ;
564+
565+ component = mount (
566+ < Client params = { { id : 1 } } /> ,
567+ { context }
568+ ) ;
569+
570+ setTimeout ( ( ) => {
571+
572+ try {
573+
574+ selectArea = component . find ( 'select' )
575+ selectArea . node . value = 'Center'
576+ selectArea . simulate ( 'change' , selectArea ) ;
577+
578+ component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
579+
580+ component . update ( ) ;
581+ expect ( component . state ( ) . error ) . toEqual ( '' ) ;
582+ expect ( component . state ( ) . client ) . toEqual ( expectedClientState ) ;
583+ done ( ) ;
584+ } catch ( e ) {
585+ console . log ( e ) ;
586+ }
587+ } , 0 ) ;
588+ } ) ;
339589} ) ;
340590
0 commit comments