From 1c9aed8e64cbf727c9387a7d01ced8507426f987 Mon Sep 17 00:00:00 2001 From: Revanth Kumar Date: Wed, 28 Apr 2021 16:12:08 -0500 Subject: [PATCH] Added useWishlist.ce tests. --- .../__snapshots__/useWishlist.ce.spec.js.snap | 54 ++++++++ .../Wishlist/__tests__/useWishlist.ce.spec.js | 129 ++++++++++++++++-- 2 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/__snapshots__/useWishlist.ce.spec.js.snap diff --git a/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/__snapshots__/useWishlist.ce.spec.js.snap b/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/__snapshots__/useWishlist.ce.spec.js.snap new file mode 100644 index 0000000000..5fde065133 --- /dev/null +++ b/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/__snapshots__/useWishlist.ce.spec.js.snap @@ -0,0 +1,54 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should return correct shape 1`] = ` +Object { + "called": true, + "error": null, + "handleAddToWishlist": [Function], + "isDisabled": true, + "isItemAdded": false, + "loading": true, +} +`; + +exports[`testing handleAddToWishlist should add the product to list 1`] = ` +Array [ + Object { + "variables": Object { + "itemOptions": Object { + "quantity": "1", + "selected_options": "selected options", + "sku": "sku", + }, + "wishlistId": "0", + }, + }, +] +`; + +exports[`testing handleAddToWishlist should call onWishlistUpdateError if there was an error calling the mutation 1`] = ` +Array [ + "Something went wrong", +] +`; + +exports[`testing handleAddToWishlist should call updateWishlistToastProps 1`] = ` +Array [ + Object { + "message": "Item successfully added to your favorites list.", + "timeout": 5000, + "type": "info", + }, +] +`; + +exports[`testing handleAddToWishlist should use necessary translations 1`] = ` +Array [ + Array [ + Object { + "defaultMessage": "Item successfully added to your favorites list.", + "id": "cartPage.wishlist.ce.successMessage", + }, + ], +] +`; diff --git a/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/useWishlist.ce.spec.js b/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/useWishlist.ce.spec.js index b4af8c84a0..da7e2b1aa0 100644 --- a/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/useWishlist.ce.spec.js +++ b/packages/peregrine/lib/talons/Wishlist/Wishlist/__tests__/useWishlist.ce.spec.js @@ -1,19 +1,34 @@ import React from 'react'; -// import { useMutation } from '@apollo/client'; +import { useMutation } from '@apollo/client'; import { useWishlist } from '../useWishlist.ce'; import createTestInstance from '../../../../util/createTestInstance'; +import { useIntl } from 'react-intl'; -jest.mock('@apollo/client', () => - jest.fn().mockReturnValue([ - jest.fn(), +jest.mock('@apollo/client', () => ({ + useMutation: jest.fn().mockReturnValue([ + jest.fn().mockResolvedValue({ data: 'data' }), { loading: false, called: false, error: null } ]) -); +})); + +jest.mock('react-intl', () => ({ + useIntl: jest.fn().mockReturnValue({ formatMessage: jest.fn() }) +})); + +jest.mock('../../../../util/shallowMerge', () => () => ({ + addProductToWishlistMutation: 'addProductToWishlistMutation' +})); + +jest.mock('../product.gql', () => () => ({})); + +const onWishlistUpdate = jest.fn(); +const onWishlistUpdateError = jest.fn(); +const updateWishlistToastProps = jest.fn(); const defaultProps = { item: { @@ -21,9 +36,9 @@ const defaultProps = { quantity: '1', selected_options: 'selected options' }, - onWishlistUpdate: jest.fn(), - onWishlistUpdateError: jest.fn(), - updateWishlistToastProps: jest.fn() + onWishlistUpdate, + onWishlistUpdateError, + updateWishlistToastProps }; const Component = props => { @@ -46,14 +61,106 @@ const getTalonProps = props => { return { talonProps, tree, update }; }; +const addProductToWishlist = jest.fn().mockResolvedValue({ data: 'data' }); +const formatMessage = jest + .fn() + .mockImplementation(({ defaultMessage }) => defaultMessage); + +beforeAll(() => { + useMutation.mockReturnValue([ + addProductToWishlist, + { called: true, loading: true, error: null } + ]); + + useIntl.mockReturnValue({ + formatMessage + }); +}); + +beforeEach(() => { + onWishlistUpdate.mockClear(); + onWishlistUpdateError.mockClear(); + updateWishlistToastProps.mockClear(); +}); + test('should return correct shape', () => { const { talonProps } = getTalonProps(defaultProps); expect(talonProps).toMatchSnapshot(); }); -// test('handleAddToWishlist') +describe('testing handleAddToWishlist', () => { + test('should add the product to list', async () => { + const { talonProps } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + expect(addProductToWishlist.mock.calls[0]).toMatchSnapshot(); + }); + + test('should set isItemAdded to true if mutation is successful', async () => { + const { talonProps, update } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + const { isItemAdded } = update(); + + expect(isItemAdded).toBeTruthy(); + }); + + test('should call updateWishlistToastProps', async () => { + const { talonProps } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + expect(updateWishlistToastProps.mock.calls[0]).toMatchSnapshot(); + }); + + test('should call onWishlistUpdate', async () => { + const { talonProps } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + expect(onWishlistUpdate).toHaveBeenCalled(); + }); + + test('should call onWishlistUpdateError if there was an error calling the mutation', async () => { + const error = 'Something went wrong'; + addProductToWishlist.mockRejectedValueOnce(error); + const { talonProps } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + expect(onWishlistUpdateError.mock.calls[0]).toMatchSnapshot(); + }); + + test('should use necessary translations', async () => { + const { talonProps } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + expect(formatMessage.mock.calls).toMatchSnapshot(); + }); +}); + +test('should reset isItemAdded if selected_options change', async () => { + const { talonProps, update } = getTalonProps(defaultProps); + + await talonProps.handleAddToWishlist(); + + const { isItemAdded } = update(); -// test('should use necessary translations', () => { + expect(isItemAdded).toBeTruthy(); -// }) + update({ + ...defaultProps, + item: { + ...defaultProps.item, + selected_options: 'new selected options' + } + }); + + const { isItemAdded: newIsItemAdded } = update(); + + expect(newIsItemAdded).toBeFalsy(); +});