diff --git a/packages/core/src/sharedWishlists/redux/middlewares/__tests__/getWishlistSetsUponSharedWishlistIdChanges.test.js b/packages/core/src/sharedWishlists/redux/middlewares/__tests__/getWishlistSetsUponSharedWishlistIdChanges.test.js new file mode 100644 index 000000000..a6ce396d3 --- /dev/null +++ b/packages/core/src/sharedWishlists/redux/middlewares/__tests__/getWishlistSetsUponSharedWishlistIdChanges.test.js @@ -0,0 +1,66 @@ +import { + CREATE_SHARED_WISHLIST_SUCCESS, + REMOVE_SHARED_WISHLIST_SUCCESS, +} from '../../actionTypes'; +import { mockSharedWishlistState } from 'tests/__fixtures__/sharedWishlists'; +import { mockStore } from '../../../../../tests'; +import getWishlistSetsUponSharedWishlistIdChanges from '../updateSharedWishlistUponItemsChanges'; +import INITIAL_STATE from '../../reducer'; + +jest.mock('../../../../wishlists/redux/actions', () => ({ + ...jest.requireActual('../../../../wishlists/redux/actions'), + doGetWishlistSets: jest.fn(() => () => ({ type: 'foo' })), +})); +const mockState = { + ...mockSharedWishlistState, + entities: { + ...mockSharedWishlistState.entities, + user: { isGuest: false }, + }, +}; + +describe('updateSharedWishlistUponItemsChanges', () => { + it('should do nothing if the action is not adding, deleting or updating a wishlist item', () => { + const store = mockStore({ sharedWishlist: INITIAL_STATE }, mockState, [ + getWishlistSetsUponSharedWishlistIdChanges, + ]); + + store.dispatch({ type: 'foo' }); + + const actions = store.getActions(); + + expect(actions).toEqual(expect.arrayContaining([{ type: 'foo' }])); + }); + + it.each([CREATE_SHARED_WISHLIST_SUCCESS, REMOVE_SHARED_WISHLIST_SUCCESS])( + 'should intercept %s, and do nothing for a guest user', + actionType => { + const store = mockStore( + { sharedWishlist: INITIAL_STATE }, + { entities: { user: { isGuest: true } } }, + [getWishlistSetsUponSharedWishlistIdChanges], + ); + + store.dispatch({ type: actionType }); + + const actions = store.getActions(); + + expect(actions).toEqual(expect.arrayContaining([{ type: actionType }])); + }, + ); + + it.each([CREATE_SHARED_WISHLIST_SUCCESS, REMOVE_SHARED_WISHLIST_SUCCESS])( + 'should intercept %s, and get the wishlist sets for a non-guest user', + actionType => { + const store = mockStore({ sharedWishlist: INITIAL_STATE }, mockState, [ + getWishlistSetsUponSharedWishlistIdChanges, + ]); + + store.dispatch({ type: actionType }); + + const actions = store.getActions(); + + expect(actions).toEqual(expect.arrayContaining([{ type: actionType }])); + }, + ); +}); diff --git a/packages/core/src/sharedWishlists/redux/middlewares/__tests__/updateSharedWishlistUponItemsChanges.test.js b/packages/core/src/sharedWishlists/redux/middlewares/__tests__/updateSharedWishlistUponItemsChanges.test.js index 9e0ed54de..0fc927d51 100644 --- a/packages/core/src/sharedWishlists/redux/middlewares/__tests__/updateSharedWishlistUponItemsChanges.test.js +++ b/packages/core/src/sharedWishlists/redux/middlewares/__tests__/updateSharedWishlistUponItemsChanges.test.js @@ -2,6 +2,7 @@ import { ADD_ITEM_TO_WISHLIST_SUCCESS, DELETE_WISHLIST_ITEM_SUCCESS, UPDATE_WISHLIST_ITEM_SUCCESS, + UPDATE_WISHLIST_SET_SUCCESS, } from '../../../../wishlists/redux/actionTypes'; import { mockSharedWishlistState } from 'tests/__fixtures__/sharedWishlists'; import { mockStore } from '../../../../../tests'; @@ -37,6 +38,7 @@ describe('updateSharedWishlistUponItemsChanges', () => { ADD_ITEM_TO_WISHLIST_SUCCESS, DELETE_WISHLIST_ITEM_SUCCESS, UPDATE_WISHLIST_ITEM_SUCCESS, + UPDATE_WISHLIST_SET_SUCCESS, ])('should intercept %s, and do nothing for a guest user', actionType => { const store = mockStore( { sharedWishlist: INITIAL_STATE }, @@ -55,6 +57,7 @@ describe('updateSharedWishlistUponItemsChanges', () => { UPDATE_WISHLIST_ITEM_SUCCESS, ADD_ITEM_TO_WISHLIST_SUCCESS, DELETE_WISHLIST_ITEM_SUCCESS, + UPDATE_WISHLIST_SET_SUCCESS, ])( 'should intercept %s, and update the shared wishlist for a non-guest user', actionType => { diff --git a/packages/core/src/sharedWishlists/redux/middlewares/getWishlistSetsUponSharedWishlistIdChanges.js b/packages/core/src/sharedWishlists/redux/middlewares/getWishlistSetsUponSharedWishlistIdChanges.js new file mode 100644 index 000000000..5c457e7bb --- /dev/null +++ b/packages/core/src/sharedWishlists/redux/middlewares/getWishlistSetsUponSharedWishlistIdChanges.js @@ -0,0 +1,35 @@ +import { + CREATE_SHARED_WISHLIST_SUCCESS, + REMOVE_SHARED_WISHLIST_SUCCESS, +} from '../actionTypes'; +import { doGetWishlistSets } from '../../../wishlists/redux/actions'; +import { getUser, getUserIsGuest } from '../../../entities/redux/selectors'; +import { getWishlistsSets as getWishlistsSetsClient } from '../../../wishlists/client'; + +const getWishlistSets = doGetWishlistSets(getWishlistsSetsClient); + +/** + * Middleware to get the updated wishlist sets if a shared wishlist has been created or deleted. + * + * @function getWishlistSetsUponSharedWishlistIdChanges + * @memberof module:sharedWishlists/middlewares + * + * @param {object} store - Redux store at the moment. + * + * @returns {Function} Redux middleware. + */ +export default store => next => action => { + if ( + action.type === REMOVE_SHARED_WISHLIST_SUCCESS || + action.type === CREATE_SHARED_WISHLIST_SUCCESS + ) { + const state = store.getState(); + const user = getUser(state); + const isGuestUser = getUserIsGuest(user); + + if (!isGuestUser) { + store.dispatch(getWishlistSets()); + } + } + return next(action); +}; diff --git a/packages/core/src/sharedWishlists/redux/middlewares/index.js b/packages/core/src/sharedWishlists/redux/middlewares/index.js index 42ecc4ef4..aea81dc6c 100644 --- a/packages/core/src/sharedWishlists/redux/middlewares/index.js +++ b/packages/core/src/sharedWishlists/redux/middlewares/index.js @@ -6,3 +6,4 @@ * @subcategory Middlewares */ export { default as updateSharedWishlistUponItemsChanges } from './updateSharedWishlistUponItemsChanges'; +export { default as getWishlistSetsUponSharedWishlistIdChanges } from './getWishlistSetsUponSharedWishlistIdChanges'; diff --git a/packages/core/src/sharedWishlists/redux/middlewares/updateSharedWishlistUponItemsChanges.js b/packages/core/src/sharedWishlists/redux/middlewares/updateSharedWishlistUponItemsChanges.js index 28b317d77..35a6ee630 100644 --- a/packages/core/src/sharedWishlists/redux/middlewares/updateSharedWishlistUponItemsChanges.js +++ b/packages/core/src/sharedWishlists/redux/middlewares/updateSharedWishlistUponItemsChanges.js @@ -2,6 +2,7 @@ import { ADD_ITEM_TO_WISHLIST_SUCCESS, DELETE_WISHLIST_ITEM_SUCCESS, UPDATE_WISHLIST_ITEM_SUCCESS, + UPDATE_WISHLIST_SET_SUCCESS, } from '../../../wishlists/redux/actionTypes'; import { doUpdateSharedWishlist } from '../actions'; import { getSharedWishlistId } from '../selectors'; @@ -24,7 +25,8 @@ export default store => next => action => { if ( action.type === UPDATE_WISHLIST_ITEM_SUCCESS || action.type === ADD_ITEM_TO_WISHLIST_SUCCESS || - action.type === DELETE_WISHLIST_ITEM_SUCCESS + action.type === DELETE_WISHLIST_ITEM_SUCCESS || + action.type === UPDATE_WISHLIST_SET_SUCCESS ) { const user = getUser(store.getState()); const isGuestUser = getUserIsGuest(user);