Skip to content

Commit

Permalink
feat: add shared wishlist middleware for getting wishlist sets
Browse files Browse the repository at this point in the history
  • Loading branch information
danpadrinom committed Aug 24, 2023
1 parent c8d2a60 commit ea14909
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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 }]));
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 },
Expand All @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
* @subcategory Middlewares
*/
export { default as updateSharedWishlistUponItemsChanges } from './updateSharedWishlistUponItemsChanges';
export { default as getWishlistSetsUponSharedWishlistIdChanges } from './getWishlistSetsUponSharedWishlistIdChanges';
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down

0 comments on commit ea14909

Please sign in to comment.