11/* eslint-disable no-undef */
22import { mount , shallow } from 'enzyme' ;
33import React from 'react' ;
4+ import 'regenerator-runtime/runtime' ;
45import { Provider } from 'react-redux' ;
56import renderer from 'react-test-renderer' ;
67import configureStore from 'redux-mock-store' ;
8+ import TextField from '@material-ui/core/TextField' ;
79import customMessage from '../utils/customMessages' ;
810import App from '../entry/App' ;
911import HelloComponent from '../components/HelloComponent' ;
1012import FrontPage from '../views/frontPage' ;
1113import reducer from '../reducers/reducer' ;
1214import firstMessage from '../actions/actions' ;
15+ import Login , {
16+ UserLogin , mapDispatchToProps ,
17+ mapStateToProps ,
18+ } from '../components/forms/UserLogin' ;
19+ import InputField from '../components/forms/InputField' ;
20+ import Lines from '../components/forms/Lines' ;
21+ import Links from '../components/forms/Link' ;
22+ import SubmitButton from '../components/forms/SubmitButton' ;
23+ import SocialLogin from '../components/forms/SocialLogin' ;
24+ import userLogin from '../helpers/userLogin' ;
25+ import LOGIN_HANDLE from '../actions/loginTypes' ;
26+ import handleLogin from '../actions/loginActions' ;
1327
1428let component = '' ;
15-
29+ let loginComponent = '' ;
30+ const defaultProps = {
31+ errors : '' ,
32+ success : false ,
33+ token : '' ,
34+ } ;
1635const mockStore = configureStore ( [ ] ) ;
1736const store = mockStore ( {
1837 message : 'Welcome' ,
@@ -23,6 +42,22 @@ component = renderer.create(
2342 < HelloComponent />
2443 </ Provider > ,
2544) ;
45+ loginComponent = renderer . create (
46+ < Provider store = { store } >
47+ < Login />
48+ </ Provider > ,
49+ ) ;
50+ const initialObj = {
51+ errors : '' ,
52+ message : 'Welcome' ,
53+ success : false ,
54+ token : '' ,
55+ } ;
56+ const handleLoginChange = jest . fn ( ) ;
57+ const props = {
58+ name : 'password' ,
59+ handleLoginChange,
60+ } ;
2661describe ( 'App tests' , ( ) => {
2762 it ( 'Will prove that the app is rendered from App component' , ( ) => {
2863 const appRender = shallow ( < App /> ) ;
@@ -40,7 +75,7 @@ describe('App tests', () => {
4075 } ) ;
4176
4277 it ( 'should return welcome when no action provided' , ( ) => {
43- expect ( reducer ( undefined , { } ) ) . toEqual ( { message : 'Welcome' } ) ;
78+ expect ( reducer ( undefined , { } ) ) . toEqual ( initialObj ) ;
4479 } ) ;
4580 it ( 'should return Redux when action is provided with value' , ( ) => {
4681 expect ( reducer ( undefined , { ...firstMessage , value : 'Redux' } ) ) . toEqual ( { message : 'Redux' } ) ;
@@ -51,4 +86,93 @@ describe('App tests', () => {
5186 } ) ;
5287 expect ( store . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
5388 } ) ;
89+ it ( 'password should hold what a user entered' , ( ) => {
90+ const wrapper = shallow ( < InputField { ...props } /> ) ;
91+ wrapper
92+ . find ( TextField )
93+ . simulate ( 'change' , { target : { value : 'Example@2020' } } ) ;
94+ expect ( handleLoginChange ) . toBeCalledTimes ( 1 ) ;
95+ } ) ;
96+ it ( 'email should hold what a user entered' , ( ) => {
97+ const props = {
98+ name : 'email' ,
99+ handleLoginChange,
100+ } ;
101+ const wrapper = shallow ( < InputField { ...props } /> ) ;
102+ wrapper
103+ . find ( TextField )
104+ . simulate ( 'change' , { target :
{ value :
'[email protected] ' } } ) ; 105+ expect ( handleLoginChange ) . toBeCalledTimes ( 1 ) ;
106+ } ) ;
107+ it ( 'email should hold what a user entered' , ( ) => {
108+ const mockFn = jest . fn ( ) ;
109+ const highWrapper = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
110+ const wrapper = highWrapper . find ( TextField ) ;
111+ console . log ( 'hh' , wrapper ) ;
112+ wrapper
113+ . at ( 0 )
114+ . props ( )
115+ . handleLoginChange = mockFn ;
116+ wrapper
117+ . at ( 0 )
118+ . props ( )
119+ . handleLoginChange ( ) ;
120+ expect ( mockFn ) . toBeCalledTimes ( 1 ) ;
121+ } ) ;
122+ it ( 'rendering the user login component' , ( ) => {
123+ const login = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
124+ expect ( login . contains ( < InputField /> , < SubmitButton /> , < Links /> , < Lines /> , < SocialLogin /> ) ) ;
125+ } ) ;
126+ it ( 'login' , ( ) => {
127+ const dispatch = jest . fn ( ) ;
128+ mapDispatchToProps ( dispatch ) . login ( ) ;
129+ mapStateToProps ( { defaultProps } ) ;
130+ const wrapper = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
131+
132+ wrapper
133+ . find ( TextField )
134+ . at ( 0 )
135+ . simulate ( 'change' , { target :
{ value :
'[email protected] ' } } ) ; 136+ wrapper
137+ . find ( TextField )
138+ . at ( 1 )
139+ . simulate ( 'change' , { target : { value : 'Example@2020' } } ) ;
140+ wrapper
141+ . find ( SubmitButton )
142+ . simulate ( 'click' ) ;
143+ console . log ( wrapper . props ( ) ) ;
144+ expect ( dispatch ) . toBeCalledTimes ( 1 ) ;
145+ } ) ;
146+ it ( 'login functionality success' , async ( ) => {
147+ const credentials = {
148+ 149+ password : 'TravelAdmin2@' ,
150+ } ;
151+ global . fetch = jest . fn ( ( ) => Promise . resolve ( {
152+ json : ( ) => Promise . resolve ( { } ) ,
153+ } ) ) ;
154+ const result = await userLogin ( credentials ) ;
155+ console . log ( result ) ;
156+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
157+ } ) ;
158+ it ( 'login functionality fail' , async ( ) => {
159+ const credentials = {
160+ email : 'travel' ,
161+ password : 'Travel' ,
162+ } ;
163+ global . fetch = jest . fn ( ( ) => Promise . reject ( ) ) ;
164+ const result = await userLogin ( credentials ) ;
165+ console . log ( result ) ;
166+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
167+ } ) ;
168+ it ( ' when action is provided with value' , ( ) => {
169+ const credentials = {
170+ 171+ password : 'TravelAdmin2@' ,
172+ } ;
173+ const result = handleLogin ( credentials ) ;
174+ const dispatch = jest . fn ( ) ;
175+ mapDispatchToProps ( dispatch ) . login ( result ) ;
176+ console . log ( 'lo' , result ) ;
177+ } ) ;
54178} ) ;
0 commit comments