-
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 shared wishlist middleware for getting wishlist sets
- Loading branch information
1 parent
c8d2a60
commit ea14909
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...dWishlists/redux/middlewares/__tests__/getWishlistSetsUponSharedWishlistIdChanges.test.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
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 }])); | ||
}, | ||
); | ||
}); |
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
35 changes: 35 additions & 0 deletions
35
.../core/src/sharedWishlists/redux/middlewares/getWishlistSetsUponSharedWishlistIdChanges.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
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); | ||
}; |
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
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