@@ -3,141 +3,90 @@ jest.dontMock('components/Client/List/Client');
33jest . dontMock ( 'components/Error/Error' ) ;
44jest . dontMock ( 'react' ) ;
55jest . dontMock ( 'axios' ) ;
6+ jest . dontMock ( 'axios-mock-adapter' ) ;
67jest . dontMock ( 'enzyme' ) ;
7-
8+ jest . dontMock ( 'services/Client' ) ;
89
910describe ( 'Test Client' , ( ) => {
11+ require ( '../tests/__mocks__/LocalStorageMock' ) ;
12+
1013 const React = require ( 'react' ) ;
1114 const Enzyme = require ( 'enzyme' ) ;
1215 const shallow = Enzyme . shallow ;
13- let ClientService = require ( 'services/Client' ) . default ;
16+
17+ let axios = require ( 'axios' ) ;
18+ let MockAdapter = require ( 'axios-mock-adapter' ) ;
1419
1520 it ( 'Client should show error message' , ( done ) => {
1621
17- let error = { response :{ data :{ error :"Client Not Found" } } } ;
18- let promises = [ ] ;
22+ let response = { error :"Client Not Found" } ;
1923 let Client ;
2024 let component ;
25+ let mockAdapter = new MockAdapter ( axios ) ;
2126
22- promises . push (
23- ( ( ) => {
24- ClientService . getClients = jest . genMockFunction ( ) . mockImplementation ( ( ) => {
25- return new Promise ( ( resolve , reject ) => {
26- throw error ;
27- } ) ;
28- } )
29- } ) ( )
30- ) ;
27+ mockAdapter . onGet ( 'http://localhost:3000/api/v1/client' ) . reply ( 404 , response ) ;
3128
32- promises . push (
33- ( ( ) => {
34- Client = require ( 'components/Client/List/Client' ) . default ;
35- } ) ( )
36- ) ;
29+ Client = require ( 'components/Client/List/Client' ) . default ;
3730
38- promises . push (
39- ( ( ) => {
40- component = shallow (
41- < Client />
42- ) ;
43- } ) ( )
31+ component = shallow (
32+ < Client />
4433 ) ;
4534
46- Promise . all ( promises ) . then ( ( ) => {
35+ setTimeout ( ( ) => {
4736 component . update ( ) ;
4837 expect ( component . render ( ) . text ( ) ) . toEqual ( 'Client Not Found' ) ;
4938 done ( ) ;
50- } ) . catch ( ( error ) => {
51- console . log ( error ) ;
52- } ) ;
39+ } , 0 ) ;
5340 } ) ;
5441
5542 it ( 'Client should show default error message' , ( done ) => {
5643
57- let error = { error :"Client Not Found" } ;
58- let promises = [ ] ;
44+ let response = { } ;
5945 let Client ;
6046 let component ;
47+ let mockAdapter = new MockAdapter ( axios ) ;
6148
62- promises . push (
63- ( ( ) => {
64- ClientService . getClients = jest . genMockFunction ( ) . mockImplementation ( ( ) => {
65- return new Promise ( ( resolve , reject ) => {
66- throw error ;
67- } ) ;
68- } )
69- } ) ( )
70- ) ;
49+ mockAdapter . onGet ( 'http://localhost:3000/api/v1/client' ) . reply ( 503 , response ) ;
7150
72- promises . push (
73- ( ( ) => {
74- Client = require ( 'components/Client/List/Client' ) . default ;
75- } ) ( )
76- ) ;
51+ Client = require ( 'components/Client/List/Client' ) . default ;
7752
78- promises . push (
79- ( ( ) => {
80- component = shallow (
81- < Client />
82- ) ;
83- } ) ( )
53+ component = shallow (
54+ < Client />
8455 ) ;
8556
86- Promise . all ( promises ) . then ( ( ) => {
57+ setTimeout ( ( ) => {
8758 component . update ( ) ;
8859 expect ( component . render ( ) . text ( ) ) . toEqual ( 'Error Found: Trying get client' ) ;
8960 done ( ) ;
90- } ) . catch ( ( error ) => {
91- console . log ( error ) ;
92- } ) ;
61+ } , 0 ) ;
9362 } ) ;
9463
9564 it ( 'Client should show mocked data' , ( done ) => {
9665
9766 let response = {
98- data : {
99- clients : [
100- { name : 'Jon Snow' , address : '7 Street' , city : 'Winterfell' } ,
101- { name : 'Cotter Pyke' , address : '0 Street' , city : 'Castle Black' } ,
102- ]
103- }
67+ clients : [
68+ { name : 'Jon Snow' , address : '7 Street' , city : 'Winterfell' } ,
69+ { name : 'Cotter Pyke' , address : '0 Street' , city : 'Castle Black' } ,
70+ ]
10471 } ;
105- let promises = [ ] ;
10672 let Client ;
10773 let component ;
74+ let mockAdapter = new MockAdapter ( axios ) ;
10875
109- promises . push (
110- ( ( ) => {
111- ClientService . getClients = jest . genMockFunction ( ) . mockImplementation ( ( ) => {
112- return new Promise ( ( resolve , reject ) => {
113- resolve ( response ) ;
114- } ) ;
115- } )
116- } ) ( )
117- ) ;
76+ mockAdapter . onGet ( 'http://localhost:3000/api/v1/client' ) . reply ( 200 , response ) ;
11877
119- promises . push (
120- ( ( ) => {
121- Client = require ( 'components/Client/List/Client' ) . default ;
122- } ) ( )
123- ) ;
78+ Client = require ( 'components/Client/List/Client' ) . default ;
12479
125- promises . push (
126- ( ( ) => {
127- component = shallow (
128- < Client />
129- ) ;
130- } ) ( )
80+ component = shallow (
81+ < Client />
13182 ) ;
13283
133- Promise . all ( promises ) . then ( ( ) => {
84+ setTimeout ( ( ) => {
13485 component . update ( ) ;
13586 expect ( component . find ( 'tbody td' ) . at ( 0 ) . text ( ) ) . toEqual ( 'Jon Snow' ) ;
13687 expect ( component . find ( 'tbody td' ) . at ( 1 ) . text ( ) ) . toEqual ( '7 Street - Winterfell' ) ;
13788 done ( ) ;
138- } ) . catch ( ( error ) => {
139- console . log ( error ) ;
140- } ) ;
89+ } , 0 ) ;
14190 } ) ;
14291
14392} ) ;
0 commit comments