@@ -4,6 +4,8 @@ jest.dontMock('components/Error/Error');
44jest . dontMock ( 'react' ) ;
55jest . dontMock ( 'axios' ) ;
66jest . dontMock ( 'enzyme' ) ;
7+ jest . dontMock ( 'axios-mock-adapter' ) ;
8+ jest . dontMock ( 'services/User' ) ;
79
810describe ( 'Test Login' , ( ) => {
911 require ( '../tests/__mocks__/LocalStorageMock' ) ;
@@ -18,6 +20,9 @@ describe('Test Login', () => {
1820 push : ( arg ) => arg
1921 }
2022 } ;
23+ let axios = require ( 'axios' ) ;
24+ let MockAdapter = require ( 'axios-mock-adapter' ) ;
25+
2126 let User = require ( 'services/User' ) . default ;
2227
2328 it ( 'Login should show form' , ( done ) => {
@@ -27,59 +32,19 @@ describe('Test Login', () => {
2732 { context }
2833 ) ;
2934
30- expect ( component . find ( 'form' ) . length ) . toEqual ( 1 ) ;
31-
32- done ( ) ;
33- } ) ;
34-
35- it ( 'Login should get login values' , ( done ) => {
36-
37- let Login = require ( 'components/Login/Login' ) . default ;
38- let loginInformed ;
39- let passwordInformed ;
40-
41- User . login = jest . genMockFunction ( ) . mockImplementation ( ( login , password ) => {
42- return new Promise ( ( resolve , reject ) => {
43- loginInformed = login ;
44- passwordInformed = password ;
45- } ) ;
46- } )
47-
48- let component = mount (
49- < Login /> ,
50- { context }
51- ) ;
52-
53- expect ( loginInformed ) . toEqual ( undefined ) ;
54- expect ( passwordInformed ) . toEqual ( undefined ) ;
55-
56- let inputLogin = component . find ( 'form div p input[type="text"]' ) ;
57- let inputPassword = component . find ( 'form div p input[type="password"]' ) ;
58-
59- inputLogin . node . value = 'Astolfo' ;
60- inputLogin . simulate ( 'change' , inputLogin ) ;
61-
62- inputPassword . node . value = 'abcd' ;
63- inputPassword . simulate ( 'change' , inputPassword ) ;
35+ setTimeout ( ( ) => {
36+ expect ( component . find ( 'form' ) . length ) . toEqual ( 1 ) ;
6437
65- component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
66-
67- expect ( loginInformed ) . toEqual ( 'Astolfo' ) ;
68- expect ( passwordInformed ) . toEqual ( 'abcd' ) ;
69-
70- done ( ) ;
38+ done ( ) ;
39+ } , 0 ) ;
7140 } ) ;
7241
7342 it ( 'Login should not set localStorage if success is different from 200' , ( done ) => {
7443
7544 let Login = require ( 'components/Login/Login' ) . default ;
76- let promises = [ ] ;
45+ let mockAdapter = new MockAdapter ( axios ) ;
7746
78- User . login = jest . genMockFunction ( ) . mockImplementation ( ( login , password ) => {
79- return new Promise ( ( resolve , reject ) => {
80- resolve ( { data : { success : 201 , token : 'token_test' } } ) ;
81- } ) ;
82- } )
47+ mockAdapter . onPost ( 'http://localhost:3000/authenticate' ) . reply ( 201 , { success : 201 , token : 'token_test' } ) ;
8348
8449 let component = mount (
8550 < Login /> ,
@@ -97,31 +62,20 @@ describe('Test Login', () => {
9762
9863 expect ( window . localStorage . getItem ( 'token' ) ) . toEqual ( null ) ;
9964
100- promises . push (
101- ( ( ) => {
102- component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
103- } ) ( )
104- ) ;
65+ component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
10566
106- Promise . all ( promises ) . then ( ( ) => {
67+ setTimeout ( ( ) => {
10768 expect ( window . localStorage . getItem ( 'token' ) ) . toEqual ( null ) ;
10869 done ( ) ;
109- } ) . catch ( ( error ) => {
110- console . log ( error ) ;
111- } ) ;
70+ } , 0 ) ;
11271 } ) ;
11372
114-
11573 it ( 'Login should set localStorage' , ( done ) => {
11674
11775 let Login = require ( 'components/Login/Login' ) . default ;
118- let promises = [ ] ;
76+ let mockAdapter = new MockAdapter ( axios ) ;
11977
120- User . login = jest . genMockFunction ( ) . mockImplementation ( ( login , password ) => {
121- return new Promise ( ( resolve , reject ) => {
122- resolve ( { data : { success : 200 , token : 'token_test' } } ) ;
123- } ) ;
124- } )
78+ mockAdapter . onPost ( 'http://localhost:3000/authenticate' ) . reply ( 200 , { success : 200 , token : 'token_test' } ) ;
12579
12680 let component = mount (
12781 < Login /> ,
@@ -139,31 +93,21 @@ describe('Test Login', () => {
13993
14094 expect ( window . localStorage . getItem ( 'token' ) ) . toEqual ( null ) ;
14195
142- promises . push (
143- ( ( ) => {
144- component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
145- } ) ( )
146- ) ;
96+ component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
14797
148- Promise . all ( promises ) . then ( ( ) => {
98+ setTimeout ( ( ) => {
14999 expect ( window . localStorage . getItem ( 'token' ) ) . toEqual ( 'token_test' ) ;
150100 done ( ) ;
151- } ) . catch ( ( error ) => {
152- console . log ( error ) ;
153- } ) ;
101+ } , 0 ) ;
154102 } ) ;
155103
156104 it ( 'Login should show error default on state' , ( done ) => {
157105
158106 let Login = require ( 'components/Login/Login' ) . default ;
159- let error = { response : { error : "Another error" } } ;
160- let promises = [ ] ;
107+ let response = { } ;
108+ let mockAdapter = new MockAdapter ( axios ) ;
161109
162- User . login = jest . genMockFunction ( ) . mockImplementation ( ( login , password ) => {
163- return new Promise ( ( resolve , reject ) => {
164- throw error ;
165- } ) ;
166- } )
110+ mockAdapter . onPost ( 'http://localhost:3000/authenticate' ) . reply ( 503 , response ) ;
167111
168112 let component = mount (
169113 < Login /> ,
@@ -179,32 +123,21 @@ describe('Test Login', () => {
179123 inputPassword . node . value = 'abcd' ;
180124 inputPassword . simulate ( 'change' , inputPassword ) ;
181125
182- promises . push (
183- ( ( ) => {
184- component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
185- } ) ( )
186- ) ;
126+ component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
187127
188- Promise . all ( promises ) . then ( ( ) => {
128+ setTimeout ( ( ) => {
189129 expect ( component . state ( ) . error ) . toEqual ( 'Authentication failed' ) ;
190130 done ( ) ;
191- } ) . catch ( ( error ) => {
192- console . log ( error ) ;
193- } ) ;
131+ } , 0 ) ;
194132 } ) ;
195133
196-
197134 it ( 'Login should set error received on state' , ( done ) => {
198135
199136 let Login = require ( 'components/Login/Login' ) . default ;
200- let error = { response : { data : { error :"User Not Found" } } } ;
201- let promises = [ ] ;
137+ let response = { error :"User Not Found" } ;
138+ let mockAdapter = new MockAdapter ( axios ) ;
202139
203- User . login = jest . genMockFunction ( ) . mockImplementation ( ( login , password ) => {
204- return new Promise ( ( resolve , reject ) => {
205- throw error ;
206- } ) ;
207- } )
140+ mockAdapter . onPost ( 'http://localhost:3000/authenticate' ) . reply ( 401 , response ) ;
208141
209142 let component = mount (
210143 < Login /> ,
@@ -220,18 +153,12 @@ describe('Test Login', () => {
220153 inputPassword . node . value = 'abcd' ;
221154 inputPassword . simulate ( 'change' , inputPassword ) ;
222155
223- promises . push (
224- ( ( ) => {
225- component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
226- } ) ( )
227- ) ;
156+ component . find ( 'form' ) . simulate ( 'submit' , { target : component . find ( 'form' ) . get ( 0 ) } ) ;
228157
229- Promise . all ( promises ) . then ( ( ) => {
158+ setTimeout ( ( ) => {
230159 expect ( component . state ( ) . error ) . toEqual ( 'User Not Found' ) ;
231160 done ( ) ;
232- } ) . catch ( ( error ) => {
233- console . log ( error ) ;
234- } ) ;
161+ } , 0 ) ;
235162 } ) ;
236163
237164} ) ;
0 commit comments