Skip to content

Commit

Permalink
feat: add share wishlist middleware for refetching wishlist sets
Browse files Browse the repository at this point in the history
  • Loading branch information
danpadrinom authored and ruifcnunes committed Aug 30, 2023
1 parent 16b251f commit cfd8eb8
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as actionTypes from '../../actionTypes.js';
import { fetchWishlistSets } from '../../../wishlists/actions/index.js';
import { INITIAL_STATE } from '../../reducer.js';
import { mockStore } from '../../../../tests/index.js';
import {
mockWishlistId,
mockWishlistState,
} from 'tests/__fixtures__/wishlists/wishlists.fixtures.mjs';
import getWishlistSetsUponSharedWishlistIdChanges from '../getWishlistSetsUponSharedWishlistIdChanges.js';
import thunk from 'redux-thunk';
import type { UserEntity } from '../../../index.js';

jest.mock('../../../wishlists/actions', () => ({
...jest.requireActual('../../actions'),
fetchWishlistSets: jest.fn(() => () => ({ type: 'foo' })),
}));

describe('getWishlistSetsUponSharedWishlistIdChanges', () => {
it('should do nothing if the action is not creating or deleting a shared wishlist', () => {
const store = mockStore({ sharedWishlist: INITIAL_STATE }, {}, [
getWishlistSetsUponSharedWishlistIdChanges,
]);

store.dispatch({ type: 'foo' });

expect(fetchWishlistSets).not.toHaveBeenCalled();
});

it.each([
actionTypes.CREATE_SHARED_WISHLIST_SUCCESS,
actionTypes.REMOVE_SHARED_WISHLIST_SUCCESS,
])('should intercept %s, and do nothing for a guest user', actionType => {
const store = mockStore(
mockWishlistState,
{
entities: {
...mockWishlistState.entities,
user: { isGuest: true } as UserEntity,
},
},
[getWishlistSetsUponSharedWishlistIdChanges],
);

store.dispatch({ type: actionType });

expect(fetchWishlistSets).not.toHaveBeenCalled();
});

it.each([
actionTypes.CREATE_SHARED_WISHLIST_SUCCESS,
actionTypes.REMOVE_SHARED_WISHLIST_SUCCESS,
])(
'should intercept %s, and fetch the wishlist sets for a non-guest user',
actionType => {
const store = mockStore(
mockWishlistState,
{
entities: {
...mockWishlistState.entities,
user: { isGuest: false } as UserEntity,
},
},
[thunk, getWishlistSetsUponSharedWishlistIdChanges],
);

store.dispatch({
type: actionType,
});

expect(fetchWishlistSets).toHaveBeenCalledWith(mockWishlistId);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('updateSharedWishlistUponItemsChanges', () => {
actionTypes.ADD_WISHLIST_ITEM_SUCCESS,
actionTypes.REMOVE_WISHLIST_ITEM_SUCCESS,
actionTypes.UPDATE_WISHLIST_ITEM_SUCCESS,
actionTypes.UPDATE_WISHLIST_SET_SUCCESS,
])('should intercept %s, and do nothing for a guest user', actionType => {
const store = mockStore(
{ sharedWishlist: INITIAL_STATE },
Expand All @@ -46,6 +47,7 @@ describe('updateSharedWishlistUponItemsChanges', () => {
actionTypes.ADD_WISHLIST_ITEM_SUCCESS,
actionTypes.REMOVE_WISHLIST_ITEM_SUCCESS,
actionTypes.UPDATE_WISHLIST_ITEM_SUCCESS,
actionTypes.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,45 @@
import * as actionTypes from '../../sharedWishlists/actionTypes.js';
import { fetchWishlistSets } from '../../wishlists/actions/index.js';
import { type FetchWishlistSetsAction } from '../../wishlists/types/actions.types.js';
import {
type GetOptionsArgument,
getWishlistId,
type StoreState,
} from '../../index.js';
import { getUser, getUserIsGuest } from '../../users/selectors.js';
import type { AnyAction, Middleware } from 'redux';
import type { ThunkDispatch } from 'redux-thunk';

type GetWishlistSetsUponSharedWishlistIdChangesParams = {
// Redux action dispatch.
dispatch: ThunkDispatch<
StoreState,
GetOptionsArgument,
FetchWishlistSetsAction
>;
// Returns the current redux state.
getState: () => StoreState;
};

const getWishlistSetsUponSharedWishlistIdChanges: Middleware =
({ dispatch, getState }: GetWishlistSetsUponSharedWishlistIdChangesParams) =>
next =>
(action: AnyAction) => {
if (
action.type === actionTypes.REMOVE_SHARED_WISHLIST_SUCCESS ||
action.type === actionTypes.CREATE_SHARED_WISHLIST_SUCCESS
) {
const state = getState();
const user = getUser(state);
const isGuestUser = getUserIsGuest(user);
const wishlistId = getWishlistId(state);

if (!isGuestUser) {
dispatch(fetchWishlistSets(wishlistId as string));
}
}

return next(action);
};

export default getWishlistSetsUponSharedWishlistIdChanges;
1 change: 1 addition & 0 deletions packages/redux/src/sharedWishlists/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* Shared wishlists middlewares.
*/
export { default as getWishlistSetsUponSharedWishlistIdChanges } from './getWishlistSetsUponSharedWishlistIdChanges.js';
export { default as updateSharedWishlistUponItemsChanges } from './updateSharedWishlistUponItemsChanges.js';
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const updateSharedWishlistUponItemsChanges: Middleware =
if (
action.type === actionTypes.UPDATE_WISHLIST_ITEM_SUCCESS ||
action.type === actionTypes.ADD_WISHLIST_ITEM_SUCCESS ||
action.type === actionTypes.REMOVE_WISHLIST_ITEM_SUCCESS
action.type === actionTypes.REMOVE_WISHLIST_ITEM_SUCCESS ||
action.type === actionTypes.UPDATE_WISHLIST_SET_SUCCESS
) {
const state = getState();
const user = getUser(state);
Expand Down

0 comments on commit cfd8eb8

Please sign in to comment.