|
| 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 | +}); |
0 commit comments