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

feat(redux): add share wishlist middleware for refetching wishlist sets #851

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
@@ -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
Loading