-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PWA-998] Increase Test Coverage in peregrine/lib/talons/SignIn #2998
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
packages/peregrine/lib/talons/SignIn/__tests__/useSignIn.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import React from 'react'; | ||
import { MockedProvider } from '@apollo/client/testing'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { useCartContext } from '../../../context/cart'; | ||
import { useUserContext } from '../../../context/user'; | ||
import defaultOperations from '../signIn.gql'; | ||
import { useSignIn } from '../useSignIn'; | ||
|
||
jest.mock('../../../Apollo/clearCartDataFromCache'); | ||
jest.mock('../../../Apollo/clearCustomerDataFromCache'); | ||
jest.mock('../../../hooks/useAwaitQuery'); | ||
jest.mock('../../../store/actions/cart', () => ({ | ||
retrieveCartId: jest.fn().mockReturnValue('new-cart-id') | ||
})); | ||
|
||
jest.mock('../../../context/cart', () => ({ | ||
useCartContext: jest.fn().mockReturnValue([ | ||
{ cartId: 'old-cart-id' }, | ||
{ | ||
createCart: jest.fn(), | ||
removeCart: jest.fn(), | ||
getCartDetails: jest.fn() | ||
} | ||
]) | ||
})); | ||
|
||
jest.mock('../../../context/user', () => ({ | ||
useUserContext: jest.fn().mockReturnValue([ | ||
{ | ||
isGettingDetails: false, | ||
getDetailsError: 'getDetails error from redux' | ||
}, | ||
{ getUserDetails: jest.fn(), setToken: jest.fn() } | ||
]) | ||
})); | ||
|
||
const signInVariables = { | ||
email: 'fry@planetexpress.com', | ||
password: 'slurm is the best' | ||
}; | ||
const authToken = 'auth-token-123'; | ||
|
||
const signInMock = { | ||
request: { | ||
query: defaultOperations.signInMutation, | ||
variables: signInVariables | ||
}, | ||
result: { | ||
data: { generateCustomerToken: { token: authToken } } | ||
} | ||
}; | ||
|
||
const mergeCartsMock = { | ||
request: { | ||
query: defaultOperations.mergeCartsMutation, | ||
variables: { | ||
destinationCartId: 'new-cart-id', | ||
sourceCartId: 'old-cart-id' | ||
} | ||
}, | ||
result: { | ||
data: null | ||
} | ||
}; | ||
|
||
const initialProps = { | ||
getCartDetailsQuery: 'getCartDetailsQuery', | ||
setDefaultUsername: jest.fn(), | ||
showCreateAccount: jest.fn(), | ||
showForgotPassword: jest.fn() | ||
}; | ||
|
||
const renderHookWithProviders = ({ | ||
renderHookOptions = { initialProps }, | ||
mocks = [signInMock, mergeCartsMock] | ||
} = {}) => { | ||
const wrapper = ({ children }) => ( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
{children} | ||
</MockedProvider> | ||
); | ||
|
||
return renderHook(useSignIn, { wrapper, ...renderHookOptions }); | ||
}; | ||
|
||
test('returns correct shape', () => { | ||
const { result } = renderHookWithProviders(); | ||
|
||
expect(result.current).toMatchInlineSnapshot(` | ||
Object { | ||
"errors": Map { | ||
"getUserDetailsQuery" => "getDetails error from redux", | ||
"signInMutation" => undefined, | ||
}, | ||
"handleCreateAccount": [Function], | ||
"handleForgotPassword": [Function], | ||
"handleSubmit": [Function], | ||
"isBusy": false, | ||
"setFormApi": [Function], | ||
} | ||
`); | ||
}); | ||
|
||
test('handleSubmit triggers waterfall of operations and actions', async () => { | ||
const [, { getCartDetails }] = useCartContext(); | ||
const [, { getUserDetails, setToken }] = useUserContext(); | ||
|
||
const { result } = renderHookWithProviders(); | ||
|
||
await act(() => result.current.handleSubmit(signInVariables)); | ||
|
||
expect(result.current.isBusy).toBe(true); | ||
expect(setToken).toHaveBeenCalledWith(authToken); | ||
expect(getCartDetails).toHaveBeenCalled(); | ||
expect(getUserDetails).toHaveBeenCalled(); | ||
}); | ||
|
||
test('handleSubmit exception is logged and resets state', async () => { | ||
const errorMessage = 'Oh no! Something went wrong :('; | ||
const [, { getUserDetails, setToken }] = useUserContext(); | ||
setToken.mockRejectedValue(errorMessage); | ||
jest.spyOn(console, 'error'); | ||
|
||
const { result } = renderHookWithProviders(); | ||
|
||
await act(() => result.current.handleSubmit(signInVariables)); | ||
|
||
expect(result.current.isBusy).toBe(false); | ||
expect(getUserDetails).not.toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalledWith(errorMessage); | ||
}); | ||
|
||
test('handleForgotPassword triggers callbacks', () => { | ||
const mockUsername = 'fry@planetexpress.com'; | ||
const mockApi = { | ||
getValue: jest.fn().mockReturnValue(mockUsername) | ||
}; | ||
|
||
const { result } = renderHookWithProviders(); | ||
act(() => result.current.setFormApi(mockApi)); | ||
act(() => result.current.handleForgotPassword()); | ||
|
||
expect(initialProps.setDefaultUsername).toHaveBeenCalledWith(mockUsername); | ||
expect(initialProps.showForgotPassword).toHaveBeenCalled(); | ||
}); | ||
|
||
test('handleCreateAccount triggers callbacks', () => { | ||
const mockUsername = 'fry@planetexpress.com'; | ||
const mockApi = { | ||
getValue: jest.fn().mockReturnValue(mockUsername) | ||
}; | ||
|
||
const { result } = renderHookWithProviders(); | ||
act(() => result.current.setFormApi(mockApi)); | ||
act(() => result.current.handleCreateAccount()); | ||
|
||
expect(initialProps.setDefaultUsername).toHaveBeenCalledWith(mockUsername); | ||
expect(initialProps.showCreateAccount).toHaveBeenCalled(); | ||
}); | ||
|
||
test('mutation error is returned by talon', async () => { | ||
const signInErrorMock = { | ||
request: signInMock.request, | ||
error: new Error('Uh oh! There was an error signing in :(') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we seriously have a sad smiley in the error message 🤣 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. He why not is funny 😸 |
||
}; | ||
|
||
const { result } = renderHookWithProviders({ mocks: [signInErrorMock] }); | ||
await act(() => result.current.handleSubmit(signInVariables)); | ||
|
||
expect(result.current.errors.get('signInMutation')).toMatchInlineSnapshot( | ||
`[Error: Uh oh! There was an error signing in :(]` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to avoid implementation details from bleeding into tests, but included these two checks since we're mocking both of these contexts. Ideally we wouldn't mock these contexts and include them in the helper
renderHookWithProviders
, but that proved to be a bit difficult with redux in place. I hope once redux is deprecated from these contexts, this will be easier going forward, and we can assert on context values, instead of checking that methods were just called.