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' ;
1325
1426let component = '' ;
15-
27+ let loginComponent = '' ;
28+ const defaultProps = {
29+ email : '' ,
30+ errors : '' ,
31+ password : '' ,
32+ success : false ,
33+ token : '' ,
34+ classes : '' ,
35+ } ;
1636const mockStore = configureStore ( [ ] ) ;
1737const store = mockStore ( {
1838 message : 'Welcome' ,
@@ -23,6 +43,22 @@ component = renderer.create(
2343 < HelloComponent />
2444 </ Provider > ,
2545) ;
46+ loginComponent = renderer . create (
47+ < Provider store = { store } >
48+ < Login />
49+ </ Provider > ,
50+ ) ;
51+ const initialObj = {
52+ errors : '' ,
53+ message : 'Welcome' ,
54+ success : false ,
55+ token : '' ,
56+ } ;
57+ const handleLoginChange = jest . fn ( ) ;
58+ const props = {
59+ name : 'password' ,
60+ handleLoginChange,
61+ } ;
2662describe ( 'App tests' , ( ) => {
2763 it ( 'Will prove that the app is rendered from App component' , ( ) => {
2864 const appRender = shallow ( < App /> ) ;
@@ -40,7 +76,7 @@ describe('App tests', () => {
4076 } ) ;
4177
4278 it ( 'should return welcome when no action provided' , ( ) => {
43- expect ( reducer ( undefined , { } ) ) . toEqual ( { message : 'Welcome' } ) ;
79+ expect ( reducer ( undefined , { } ) ) . toEqual ( initialObj ) ;
4480 } ) ;
4581 it ( 'should return Redux when action is provided with value' , ( ) => {
4682 expect ( reducer ( undefined , { ...firstMessage , value : 'Redux' } ) ) . toEqual ( { message : 'Redux' } ) ;
@@ -51,4 +87,88 @@ describe('App tests', () => {
5187 } ) ;
5288 expect ( store . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
5389 } ) ;
90+ it ( 'rendering the user login component' , ( ) => {
91+ const login = shallow ( < loginComponent /> ) ;
92+ expect ( login . contains ( < InputField /> , < SubmitButton /> , < Links /> , < Lines /> , < SocialLogin /> ) ) ;
93+ } ) ;
94+ it ( 'password should hold what a user entered' , ( ) => {
95+ const wrapper = shallow ( < InputField { ...props } /> ) ;
96+ wrapper
97+ . find ( TextField )
98+ . simulate ( 'change' , { target : { value : 'Example@2020' } } ) ;
99+ expect ( handleLoginChange ) . toBeCalledTimes ( 1 ) ;
100+ } ) ;
101+ it ( 'email should hold what a user entered' , ( ) => {
102+ const props = {
103+ name : 'email' ,
104+ handleLoginChange,
105+ } ;
106+ const wrapper = shallow ( < InputField { ...props } /> ) ;
107+ wrapper
108+ . find ( TextField )
109+ . simulate ( 'change' , { target :
{ value :
'[email protected] ' } } ) ; 110+ expect ( handleLoginChange ) . toBeCalledTimes ( 1 ) ;
111+ } ) ;
112+ it ( 'email should hold what a user entered' , ( ) => {
113+ const mockFn = jest . fn ( ) ;
114+ const highWrapper = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
115+ const wrapper = highWrapper . find ( TextField ) ;
116+ console . log ( 'hh' , wrapper ) ;
117+ wrapper
118+ . at ( 0 )
119+ . props ( )
120+ . handleLoginChange = mockFn ;
121+ wrapper
122+ . at ( 0 )
123+ . props ( )
124+ . handleLoginChange ( ) ;
125+ expect ( mockFn ) . toBeCalledTimes ( 1 ) ;
126+ } ) ;
127+ it ( 'rendering the user login component' , ( ) => {
128+ const login = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
129+ expect ( login . contains ( < InputField /> , < SubmitButton /> , < Links /> , < Lines /> , < SocialLogin /> ) ) ;
130+ } ) ;
131+ it ( 'login' , ( ) => {
132+ const handleLoginChange = jest . fn ( ) ;
133+ const dispatch = jest . fn ( ) ;
134+ mapDispatchToProps ( dispatch ) . login ( ) ;
135+ mapStateToProps ( { defaultProps } ) ;
136+ const wrapper = mount ( < Provider store = { store } > < Login /> </ Provider > ) ;
137+
138+ wrapper
139+ . find ( TextField )
140+ . at ( 0 )
141+ . simulate ( 'change' , { target :
{ value :
'[email protected] ' } } ) ; 142+ wrapper
143+ . find ( TextField )
144+ . at ( 1 )
145+ . simulate ( 'change' , { target : { value : 'Example@2020' } } ) ;
146+ wrapper
147+ . find ( SubmitButton )
148+ . simulate ( 'click' ) ;
149+
150+ expect ( dispatch ) . toBeCalledTimes ( 1 ) ;
151+ } ) ;
152+ it ( 'login functionality success' , async ( ) => {
153+ const credentials = {
154+ 155+ password : 'TravelAdmin2@' ,
156+ } ;
157+ global . fetch = jest . fn ( ( ) => Promise . resolve ( {
158+ json : ( ) => Promise . resolve ( { } ) ,
159+ } ) ) ;
160+ const result = await userLogin ( credentials ) ;
161+ console . log ( result ) ;
162+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
163+ } ) ;
164+ it ( 'login functionality fail' , async ( ) => {
165+ const credentials = {
166+ 167+ password : 'TravelAdmin2@' ,
168+ } ;
169+ global . fetch = jest . fn ( ( ) => Promise . reject ( ) ) ;
170+ const result = await userLogin ( credentials ) ;
171+ console . log ( result ) ;
172+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
173+ } ) ;
54174} ) ;
0 commit comments