-
Notifications
You must be signed in to change notification settings - Fork 686
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-989: Real Data Wishlists and Wishlist Create #3041
Changes from all commits
56c450c
7366994
fb11c50
597f55d
f0a90cd
7908916
586d663
6bbfa3d
36712af
4a71ca4
97993d5
9c0e05f
b16dbbb
5eebf8b
bac3020
4b366a6
8a907e3
52227e3
77e12c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,164 @@ | ||
import React from 'react'; | ||
|
||
import createTestInstance from '../../../util/createTestInstance'; | ||
import { MockedProvider } from '@apollo/client/testing'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { useCreateWishlist } from '../useCreateWishlist'; | ||
import defaultOperations from '../createWishlist.gql'; | ||
import wishlistPageOperations from '../wishlistPage.gql'; | ||
|
||
const Component = props => { | ||
const talonProps = useCreateWishlist(props); | ||
jest.mock('@magento/peregrine/lib/context/app', () => { | ||
const state = {}; | ||
const api = { actions: { setPageLoading: jest.fn() } }; | ||
const useAppContext = jest.fn(() => [state, api]); | ||
|
||
return <i talonProps={talonProps} />; | ||
}; | ||
return { useAppContext }; | ||
}); | ||
|
||
const getTalonProps = props => { | ||
const tree = createTestInstance(<Component {...props} />); | ||
const { root } = tree; | ||
const { talonProps } = root.findByType('i').props; | ||
const createWishlistVariables = { | ||
name: 'Test WishList', | ||
visibility: 'PUBLIC' | ||
}; | ||
|
||
const update = newProps => { | ||
tree.update(<Component {...{ ...props, ...newProps }} />); | ||
let createWishlistMutationCalled = false; | ||
let getCustomerWishlistsQueryCalled = false; | ||
let getMultipleWishlistsEnabledQueryCalled = false; | ||
|
||
const getCustomerWishlistsMock = { | ||
request: { | ||
query: wishlistPageOperations.getCustomerWishlistQuery | ||
}, | ||
loading: false, | ||
result: () => { | ||
getCustomerWishlistsQueryCalled = true; | ||
return { | ||
data: { | ||
customer: { | ||
id: 'customerId', | ||
wishlists: [ | ||
{ | ||
id: 42, | ||
items_count: 0, | ||
name: 'Test WishList', | ||
visibility: 'PUBLIC', | ||
sharing_code: 'code' | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
return root.findByType('i').props.talonProps; | ||
}; | ||
const getMultipleWishlistsEnabledQueryMock = { | ||
request: { | ||
query: defaultOperations.getMultipleWishlistsEnabledQuery | ||
}, | ||
loading: false, | ||
result: () => { | ||
getMultipleWishlistsEnabledQueryCalled = true; | ||
return { | ||
data: { | ||
storeConfig: { | ||
id: '42', | ||
enable_multiple_wishlists: '1' | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
return { talonProps, tree, update }; | ||
const createWishlistMock = { | ||
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. How do you test the loading state of this mutation? I'm trying to do this in my PR but I'm having trouble... 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. Nvm, found it :D Just don't |
||
request: { | ||
query: defaultOperations.createWishlistMutation, | ||
variables: { input: createWishlistVariables } | ||
}, | ||
result: () => { | ||
createWishlistMutationCalled = true; | ||
return { | ||
data: { | ||
createWishlist: { | ||
wishlist: { | ||
id: '42' | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
test('should return properly', () => { | ||
const { talonProps } = getTalonProps(); | ||
const renderHookWithProviders = ({ | ||
renderHookOptions = {}, | ||
mocks = [ | ||
createWishlistMock, | ||
getCustomerWishlistsMock, | ||
getMultipleWishlistsEnabledQueryMock | ||
] | ||
} = {}) => { | ||
const wrapper = ({ children }) => ( | ||
<MockedProvider mocks={mocks} addTypename={false}> | ||
{children} | ||
</MockedProvider> | ||
); | ||
|
||
return renderHook(useCreateWishlist, { wrapper, ...renderHookOptions }); | ||
}; | ||
|
||
expect(talonProps).toMatchSnapshot(); | ||
test('should return properly', async () => { | ||
const { result } = renderHookWithProviders(); | ||
await new Promise(resolve => setTimeout(resolve, 0)); | ||
expect(getMultipleWishlistsEnabledQueryCalled).toBe(true); | ||
expect(result.current).toMatchSnapshot(); | ||
}); | ||
|
||
test('handleShowModal should set isModalOpen to true', () => { | ||
const { talonProps, update } = getTalonProps(); | ||
|
||
talonProps.handleShowModal(); | ||
const { isModalOpen } = update(); | ||
|
||
expect(isModalOpen).toBeTruthy(); | ||
test('shouldRender is false when multiple wishlists disabled', async () => { | ||
const multipleWishlistsDisabledMock = { | ||
request: getMultipleWishlistsEnabledQueryMock.request, | ||
result: { | ||
data: { storeConfig: { id: '42', enable_multiple_wishlists: '0' } } | ||
} | ||
}; | ||
const { result } = renderHookWithProviders({ | ||
mocks: [ | ||
createWishlistMock, | ||
getCustomerWishlistsMock, | ||
multipleWishlistsDisabledMock | ||
] | ||
}); | ||
await new Promise(resolve => setTimeout(resolve, 0)); | ||
expect(result.current.shouldRender).toBe(false); | ||
}); | ||
|
||
test('handleHideModal should set isModalOpen to false', () => { | ||
const { talonProps, update } = getTalonProps(); | ||
|
||
talonProps.handleHideModal(); | ||
const { isModalOpen } = update(); | ||
|
||
expect(isModalOpen).toBeFalsy(); | ||
test('should return error', async () => { | ||
const createWishlistErrorMock = { | ||
request: createWishlistMock.request, | ||
error: new Error('Only 5 wish list(s) can be created.') | ||
}; | ||
const { result } = renderHookWithProviders({ | ||
mocks: [createWishlistErrorMock, getMultipleWishlistsEnabledQueryMock] | ||
}); | ||
await act(() => result.current.handleCreateList(createWishlistVariables)); | ||
expect( | ||
result.current.formErrors.get('createWishlistMutation') | ||
).toMatchInlineSnapshot(`[Error: Only 5 wish list(s) can be created.]`); | ||
}); | ||
|
||
test('handleCreateList should set isModalOpen to false', () => { | ||
const { talonProps, update } = getTalonProps(); | ||
test('handleShowModal should set isModalOpen to true', async () => { | ||
const { result } = renderHookWithProviders(); | ||
await act(() => result.current.handleShowModal()); | ||
expect(result.current.isModalOpen).toBe(true); | ||
}); | ||
|
||
talonProps.handleCreateList(); | ||
const { isModalOpen } = update(); | ||
test('handleHideModal should set isModalOpen to false', async () => { | ||
const { result } = renderHookWithProviders(); | ||
await act(() => result.current.handleHideModal()); | ||
expect(result.current.isModalOpen).toBe(false); | ||
}); | ||
|
||
expect(isModalOpen).toBeFalsy(); | ||
test('handleCreateList should update cache and set isModalOpen to false', async () => { | ||
const { result } = renderHookWithProviders(); | ||
await act(() => result.current.handleCreateList(createWishlistVariables)); | ||
expect(createWishlistMutationCalled).toBe(true); | ||
expect(getCustomerWishlistsQueryCalled).toBe(true); | ||
expect(result.current.isModalOpen).toBe(false); | ||
}); |
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.
packages/extensions/venia-sample-language-packs/i18n/fr_FR.json