-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add share wishlist middleware for refetching wishlist sets
- Loading branch information
1 parent
16b251f
commit cfd8eb8
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
.../sharedWishlists/middlewares/__tests__/getWishlistSetsUponSharedWishlistIdChanges.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/redux/src/sharedWishlists/middlewares/getWishlistSetsUponSharedWishlistIdChanges.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters