Skip to content
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

Merged
merged 19 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@
"validation.mustBeChecked": "Doit être vérifié.",
"validation.validatePassword": "Un mot de passe doit contenir au moins 3 des éléments suivants: minuscules, majuscules, chiffres, caractères spéciaux.",
"wishlist.emptyListText": "Il n'y a actuellement aucun élément dans cette liste",
"wishlist.privateText": "Privée",
"wishlist.publicText": "Publique",

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

"wishlistItem.addToCart": "Ajouter au panier",
"wishlistItem.addToCartError": "Un problème est survenu. Veuillez actualiser et réessayer.",
"wishlistPage.disabledMessage": "Désolé, cette fonctionnalité a été désactivée.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Object {
"category_id",
],
},
"BundleWishlistItem": Object {
"keyFields": [Function],
},
"Cart": Object {
"fields": Object {
"applied_gift_cards": Object {
Expand Down Expand Up @@ -50,6 +53,9 @@ Object {
},
},
},
"ConfigurableWishlistItem": Object {
"keyFields": [Function],
},
"Customer": Object {
"fields": Object {
"addresses": Object {
Expand Down Expand Up @@ -82,6 +88,15 @@ Object {
},
"keyFields": [Function],
},
"DownloadableWishlistItem": Object {
"keyFields": [Function],
},
"GiftCardWishlistItem": Object {
"keyFields": [Function],
},
"GroupedProductWishlistItem": Object {
"keyFields": [Function],
},
"ProductImage": Object {
"keyFields": Array [
"url",
Expand Down Expand Up @@ -130,5 +145,17 @@ Object {
"street",
],
},
"SimpleWishlistItem": Object {
"keyFields": [Function],
},
"VirtualWishlistItem": Object {
"keyFields": [Function],
},
"Wishlist": Object {
"keyFields": [Function],
},
"WishlistItem": Object {
"keyFields": [Function],
},
}
`;
37 changes: 37 additions & 0 deletions packages/peregrine/lib/Apollo/policies/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,40 @@ describe('CategoryTree type provides the correct values', () => {
expect(result).toStrictEqual(incoming);
});
});

test('wishlist entities have correct keys', () => {
const {
Wishlist,
WishlistItem,
SimpleWishlistItem,
VirtualWishlistItem,
DownloadableWishlistItem,
BundleWishlistItem,
GroupedProductWishlistItem,
ConfigurableWishlistItem,
GiftCardWishlistItem
} = typePolicies;
expect(Wishlist.keyFields({ id: 1 })).toBe('CustomerWishlist:1');
expect(WishlistItem.keyFields({ id: 1 })).toBe('CustomerWishlistItem:1');
expect(SimpleWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerSimpleWishlistItem:1'
);
expect(VirtualWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerVirtualWishlistItem:1'
);
expect(DownloadableWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerDownloadableWishlistItem:1'
);
expect(BundleWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerBundleWishlistItem:1'
);
expect(GroupedProductWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerGroupedProductWishlistItem:1'
);
expect(ConfigurableWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerConfigurableWishlistItem:1'
);
expect(GiftCardWishlistItem.keyFields({ id: 1 })).toBe(
'CustomerGiftCardWishlistItem:1'
);
});
27 changes: 27 additions & 0 deletions packages/peregrine/lib/Apollo/policies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ const typePolicies = {
}
}
}
},
Wishlist: {
keyFields: ({ id }) => `CustomerWishlist:${id}`
},
WishlistItem: {
keyFields: ({ id }) => `CustomerWishlistItem:${id}`
},
SimpleWishlistItem: {
keyFields: ({ id }) => `CustomerSimpleWishlistItem:${id}`
},
VirtualWishlistItem: {
keyFields: ({ id }) => `CustomerVirtualWishlistItem:${id}`
},
DownloadableWishlistItem: {
keyFields: ({ id }) => `CustomerDownloadableWishlistItem:${id}`
},
BundleWishlistItem: {
keyFields: ({ id }) => `CustomerBundleWishlistItem:${id}`
},
GroupedProductWishlistItem: {
keyFields: ({ id }) => `CustomerGroupedProductWishlistItem:${id}`
},
ConfigurableWishlistItem: {
keyFields: ({ id }) => `CustomerConfigurableWishlistItem:${id}`
},
GiftCardWishlistItem: {
keyFields: ({ id }) => `CustomerGiftCardWishlistItem:${id}`
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

exports[`should return properly 1`] = `
Object {
"formErrors": Map {},
"handleCreateList": [Function],
"handleHideModal": [Function],
"handleShowModal": [Function],
"isModalOpen": false,
"loading": false,
"shouldRender": true,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Object {
"errors": Map {
"getCustomerWishlistQuery" => undefined,
},
"loading": undefined,
"wishlists": Array [],
}
`;
Expand Down
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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, found it :D Just don't await the act'ed click.

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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { useQuery } from '@apollo/client';
jest.mock('react-router-dom', () => ({
useHistory: jest.fn()
}));
jest.mock('@apollo/client', () => ({
useQuery: jest.fn().mockReturnValue({})
}));
jest.mock('@apollo/client', () => {
const ApolloClient = jest.requireActual('@apollo/client');
return {
...ApolloClient,
useQuery: jest.fn().mockReturnValue({})
};
});
jest.mock('../../../context/user', () => ({
useUserContext: jest.fn().mockReturnValue([{ isSignedIn: true }])
}));
Expand All @@ -26,7 +30,9 @@ const Component = props => {
};

const props = {
queries: {}
queries: {
getCustomerWishlistQuery: {}
}
};

test('return correct shape', () => {
Expand Down
Loading