Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.11.1] - UNRELEASED

### Fixed
- add disconnect and sync options for cart/clear - @gibkigonzo (#4062)

## [1.11.1] - 2020.02.05

### Added
Expand Down
17 changes: 14 additions & 3 deletions core/modules/cart/store/actions/connectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ const connectActions = {
toggleMicrocart ({ commit }) {
commit(types.CART_TOGGLE_MICROCART)
},
async clear ({ commit, dispatch, getters }) {
/**
* It will always clear cart items on frontend.
* Options:
* sync - if you want to sync it with backend.
* disconnect - if you want to clear cart token.
*/
async clear ({ commit, dispatch }, { disconnect = true, sync = true } = {}) {
await commit(types.CART_LOAD_CART, [])
await commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
await commit(types.CART_SET_ITEMS_HASH, null)
if (sync) {
await dispatch('sync', { forceClientState: true })
}
if (disconnect) {
await commit(types.CART_SET_ITEMS_HASH, null)
await dispatch('disconnect')
}
},
async disconnect ({ commit }) {
commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
Expand Down
59 changes: 46 additions & 13 deletions core/modules/cart/test/unit/store/connectActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,55 @@ jest.mock('@vue-storefront/core/helpers', () => ({
}));

describe('Cart connectActions', () => {
it('clears cart token and server hash', async () => {
const contextMock = createContextMock({
getters: {
isCartSyncEnabled: true
}
})
config.orders = {
directBackendSync: false
}
it('clear deletes all cart products and token', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock);
config.cart = { synchronize: false };

await (cartActions as any).clear(contextMock)
await wrapper(cartActions);

expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_LOAD_CART_SERVER_TOKEN, null);
expect(contextMock.commit).toHaveBeenNthCalledWith(3, types.CART_SET_ITEMS_HASH, null);
})
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(2, 'disconnect');
});

it('clear deletes all cart products but keep token', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock, { disconnect: false });

config.cart = { synchronize: false };

await wrapper(cartActions);

expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
});

it('clear deletes all cart products and token, but not sync with backend', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock, { sync: false });

config.cart = { synchronize: false };

await wrapper(cartActions);

expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'disconnect');
});

it('disconnects cart', async () => {
const contextMock = createContextMock()
Expand Down
3 changes: 2 additions & 1 deletion core/modules/checkout/store/checkout/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const actions: ActionTree<CheckoutState, RootState> = {
const result = await dispatch('order/placeOrder', order, { root: true })
if (!result.resultCode || result.resultCode === 200) {
await dispatch('updateOrderTimestamp')
await dispatch('cart/clear', null, { root: true })
// clear cart without sync, because after order cart will be already cleared on backend
await dispatch('cart/clear', { sync: false }, {root: true})
await dispatch('dropPassword')
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Checkout actions', () => {
expect(mockContext.dispatch).toHaveBeenCalledTimes(4);
expect(mockContext.dispatch).toHaveBeenNthCalledWith(1, 'order/placeOrder', order, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(2, 'updateOrderTimestamp');
expect(mockContext.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', null, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', { sync: false }, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(4, 'dropPassword');
});

Expand All @@ -54,7 +54,7 @@ describe('Checkout actions', () => {
expect(mockContext.dispatch).toHaveBeenCalledTimes(4);
expect(mockContext.dispatch).toHaveBeenNthCalledWith(1, 'order/placeOrder', order, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(2, 'updateOrderTimestamp');
expect(mockContext.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', null, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', { sync: false }, { root: true });
expect(mockContext.dispatch).toHaveBeenNthCalledWith(4, 'dropPassword');
});

Expand All @@ -65,7 +65,7 @@ describe('Checkout actions', () => {
expect(mockContext.dispatch).toHaveBeenCalledTimes(1);
expect(mockContext.dispatch).toHaveBeenNthCalledWith(1, 'order/placeOrder', order, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(2, 'updateOrderTimestamp');
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(3, 'cart/clear', null, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(3, 'cart/clear', { sync: false }, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(4, 'dropPassword');
});

Expand All @@ -76,7 +76,7 @@ describe('Checkout actions', () => {
expect(mockContext.dispatch).toHaveBeenCalledTimes(1);
expect(mockContext.dispatch).toHaveBeenNthCalledWith(1, 'order/placeOrder', order, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(2, 'updateOrderTimestamp');
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(3, 'cart/clear', null, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(3, 'cart/clear', { sync: false }, { root: true });
expect(mockContext.dispatch).not.toHaveBeenNthCalledWith(4, 'dropPassword');
expect(Logger.error).toHaveBeenCalled();
});
Expand Down
4 changes: 3 additions & 1 deletion core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ const actions: ActionTree<UserState, RootState> = {
await dispatch('cart/disconnect', {}, { root: true })
await dispatch('clearCurrentUser')
EventBus.$emit('user-after-logout')
await dispatch('cart/clear', null, { root: true })
// clear cart without sync, because after logout we don't want to clear cart on backend
// user should have items when he comes back
await dispatch('cart/clear', { sync: false }, { root: true })

if (!silent) {
await dispatch('notification/spawnNotification', {
Expand Down
2 changes: 1 addition & 1 deletion core/modules/user/test/unit/store/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ describe('User actions', () => {
expect(contextMock.commit).toBeCalledWith(types.USER_END_SESSION)
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'cart/disconnect', {}, {root: true})
expect(contextMock.dispatch).toHaveBeenNthCalledWith(2, 'clearCurrentUser')
expect(contextMock.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', null, {root: true})
expect(contextMock.dispatch).toHaveBeenNthCalledWith(3, 'cart/clear', { sync: false }, {root: true})
expect(contextMock.dispatch).toHaveBeenNthCalledWith(4, 'notification/spawnNotification', {
type: 'success',
message: "You're logged out",
Expand Down
3 changes: 2 additions & 1 deletion src/modules/instant-checkout/components/InstantCheckout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export default {
this.$store.dispatch('checkout/setThankYouPage', true)
this.$store.commit('ui/setMicrocart', false)
this.$router.push(this.localizedRoute('/checkout'))
this.$store.dispatch('cart/clear', null, {root: true})
// clear cart without sync, because after order cart will be already cleared on backend
this.$store.dispatch('cart/clear', { sync: false }, {root: true})
}
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ export default {
action1: { label: i18n.t('Cancel'), action: 'close' },
action2: { label: i18n.t('OK'),
action: async () => {
await this.$store.dispatch('cart/clear') // just clear the items without sync
await this.$store.dispatch('cart/sync', { forceClientState: true })
// We just need to clear cart on frontend and backend.
// but cart token can be reused
await this.$store.dispatch('cart/clear', { disconnect: false })
}
},
hasNoTimeout: true
Expand Down