Skip to content

Commit 12c30bc

Browse files
author
Sanjeev Yadav
committed
test: Write test for useLogin hook
1 parent 00c373e commit 12c30bc

File tree

6 files changed

+148
-7
lines changed

6 files changed

+148
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"ios": "react-native run-ios",
88
"start": "react-native start",
99
"test": "jest",
10+
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
1011
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
1112
"check-types": "tsc",
1213
"prettier": "prettier --ignore-path .gitignore \"src/**/*.+(js|json|ts|tsx)\"",

src/logic/app/__tests__/useForm.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('useForm', () => {
100100
useForm<{ email: string; password: string }>(inititalProps),
101101
);
102102
await act(async () => {
103-
await result.current.handleSubmit({});
103+
await result.current.handleSubmit();
104104
});
105105

106106
// Verify
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import React from 'react';
2+
import { renderHook, act } from '@testing-library/react-hooks';
3+
import { InMemoryCache } from '@apollo/client';
4+
import { MockedProvider } from '@apollo/client/testing';
5+
import { LoginForm, useLogin } from '../useLogin';
6+
import { CREATE_CUSTOMER_TOKEN } from '../../../apollo/mutations/createCustomerToken';
7+
import { IS_LOGGED_IN } from '../../../apollo/queries/isLoggedIn';
8+
9+
const initialLoginForm: LoginForm = {
10+
email: '',
11+
password: '',
12+
secureTextEntry: true,
13+
};
14+
const email = 'alexwarner@gmail.com';
15+
const password = 'password123';
16+
const successResponse = {
17+
generateCustomerToken: {
18+
token: '#$Gfh12DF%22kauw',
19+
},
20+
};
21+
const loginMutationMock = {
22+
request: {
23+
query: CREATE_CUSTOMER_TOKEN,
24+
variables: {
25+
email,
26+
password,
27+
},
28+
},
29+
result: {
30+
data: successResponse,
31+
},
32+
};
33+
const loginMutationErrorMock = {
34+
request: {
35+
query: CREATE_CUSTOMER_TOKEN,
36+
variables: {
37+
email,
38+
password,
39+
},
40+
},
41+
error: new Error('Something went wrong'),
42+
};
43+
44+
describe('useLogin', () => {
45+
function getHookWrapper(mocks: any = []) {
46+
const cache = new InMemoryCache({
47+
addTypename: false,
48+
});
49+
const wrapper = ({
50+
children,
51+
}: {
52+
children: React.ReactElement;
53+
}): React.ReactElement => (
54+
<MockedProvider mocks={mocks} addTypename={false} cache={cache}>
55+
{children}
56+
</MockedProvider>
57+
);
58+
const { result, waitForNextUpdate } = renderHook(() => useLogin(), {
59+
wrapper,
60+
});
61+
const getLoggedInStatusFromCache = () =>
62+
cache.readQuery({ query: IS_LOGGED_IN });
63+
// Test the initial state of the request
64+
expect(result.current.loading).toBeFalsy();
65+
expect(result.current.error).toBeUndefined();
66+
expect(result.current.data).toBeUndefined();
67+
expect(result.current.values).toEqual(initialLoginForm);
68+
expect(getLoggedInStatusFromCache()).toBeNull();
69+
expect(typeof result.current.handleChange).toBe('function');
70+
expect(typeof result.current.handleSubmit).toBe('function');
71+
72+
return { result, waitForNextUpdate, getLoggedInStatusFromCache };
73+
}
74+
75+
test('should handle change in values', () => {
76+
// Setup
77+
const expectedValues: LoginForm = {
78+
email,
79+
password,
80+
secureTextEntry: false,
81+
};
82+
const { result } = getHookWrapper([loginMutationMock]);
83+
84+
// Exercise
85+
act(() => {
86+
result.current.handleChange('email')(email);
87+
result.current.handleChange('password')(password);
88+
result.current.handleChange('secureTextEntry')(true); // the value true will be ignored, it will get toggled of previous value
89+
});
90+
91+
// Verify
92+
expect(result.current.values).toEqual(expectedValues);
93+
});
94+
95+
test('should handle success', async () => {
96+
// Setup
97+
const {
98+
result,
99+
getLoggedInStatusFromCache,
100+
waitForNextUpdate,
101+
} = getHookWrapper([loginMutationMock]);
102+
103+
// Enter correct credentials for login
104+
await act(async () => {
105+
result.current.handleChange('email')(email);
106+
result.current.handleChange('password')(password);
107+
await waitForNextUpdate();
108+
await result.current.handleSubmit();
109+
});
110+
111+
// Verify
112+
expect(result.current.loading).toBeFalsy();
113+
expect(result.current.error).toBeUndefined();
114+
expect(result.current.data).toEqual(successResponse);
115+
expect(getLoggedInStatusFromCache()).toEqual({ isLoggedIn: true });
116+
});
117+
118+
test('should handle error', async () => {
119+
// Setup
120+
const {
121+
result,
122+
getLoggedInStatusFromCache,
123+
waitForNextUpdate,
124+
} = getHookWrapper([loginMutationErrorMock]);
125+
126+
// Enter credentials for login
127+
await act(async () => {
128+
result.current.handleChange('email')(email);
129+
result.current.handleChange('password')(password);
130+
await waitForNextUpdate();
131+
await result.current.handleSubmit();
132+
});
133+
134+
// Verify
135+
expect(result.current.loading).toBeFalsy();
136+
expect(result.current.data).toBeUndefined();
137+
expect(getLoggedInStatusFromCache()).toBeNull();
138+
expect(result.current.error).toEqual(new Error('Something went wrong'));
139+
});
140+
});

src/logic/categories/__tests__/useCategories.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ describe('useCategories', () => {
7575

7676
// Verify
7777
expect(result.current.loading).toBeFalsy();
78-
expect(result.current.categories).toEqual(categoryList);
7978
expect(result.current.error).toBeUndefined();
79+
expect(result.current.categories).toEqual(categoryList);
8080
});
8181

8282
test('should return error when request fails', async () => {
@@ -90,8 +90,8 @@ describe('useCategories', () => {
9090
await waitForNextUpdate();
9191

9292
// Verify
93-
expect(result.current.categories).toBeUndefined();
9493
expect(result.current.loading).toBeFalsy();
9594
expect(result.current.error).toBeTruthy();
95+
expect(result.current.categories).toBeUndefined();
9696
});
9797
});

src/logic/products/__tests__/useProductDetails.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ describe('useProductDetails', () => {
100100

101101
// Verify
102102
expect(result.current.loading).toBeFalsy();
103-
expect(result.current.productDetails).toEqual(product);
104103
expect(result.current.error).toBeUndefined();
104+
expect(result.current.productDetails).toEqual(product);
105105
});
106106

107107
test('should return error when request fails', async () => {
@@ -116,7 +116,7 @@ describe('useProductDetails', () => {
116116

117117
// Verify
118118
expect(result.current.loading).toBeFalsy();
119-
expect(result.current.productDetails).toBeUndefined();
120119
expect(result.current.error).toBeTruthy();
120+
expect(result.current.productDetails).toBeUndefined();
121121
});
122122
});

src/logic/profile/__tests__/useCustomer.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ describe('useCustomer', () => {
5858

5959
// Verify
6060
expect(result.current.loading).toBeFalsy();
61-
expect(result.current.data).toEqual({ customer });
6261
expect(result.current.error).toBeUndefined();
62+
expect(result.current.data).toEqual({ customer });
6363
});
6464

6565
test('should return error when request fails', async () => {
@@ -76,7 +76,7 @@ describe('useCustomer', () => {
7676

7777
// Verify
7878
expect(result.current.loading).toBeFalsy();
79-
expect(result.current.data).toBeUndefined();
8079
expect(result.current.error).toBeTruthy();
80+
expect(result.current.data).toBeUndefined();
8181
});
8282
});

0 commit comments

Comments
 (0)