diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d8f4770e2..5931483468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixes when having multiple custom options with overlapping option_type_id values, selecting 1 changes the others - @carlokok (#4196) - Update eslint and fix code style. - @gibkigonzo (#4179 #4181) +- Fixes bug that caused addToCart action not to display messages to user - @juho-jaakkola (#4185) - add missing cache tags for category and product - @gibkigonzo (#4173) - add ssrAppId to avoid second meta render on csr - @gibkigonzo (#4203) diff --git a/core/modules/cart/store/actions/connectActions.ts b/core/modules/cart/store/actions/connectActions.ts index 03d91d252e..9b0ce22df7 100644 --- a/core/modules/cart/store/actions/connectActions.ts +++ b/core/modules/cart/store/actions/connectActions.ts @@ -69,7 +69,7 @@ const connectActions = { const cartToken = getters['getCartToken'] if (storedItems.length && !cartToken) { Logger.info('Creating server cart token', 'cart')() - await dispatch('connect', { guestCart: false }) + return dispatch('connect', { guestCart: false }) } } } diff --git a/core/modules/cart/store/actions/itemActions.ts b/core/modules/cart/store/actions/itemActions.ts index 378db9b438..fcaf2d6ddb 100644 --- a/core/modules/cart/store/actions/itemActions.ts +++ b/core/modules/cart/store/actions/itemActions.ts @@ -80,9 +80,18 @@ const itemActions = { productIndex++ } } - await dispatch('create') + + let newDiffLog = await dispatch('create') + if (newDiffLog !== undefined) { + diffLog.merge(newDiffLog) + } + if (getters.isCartSyncEnabled && getters.isCartConnected && !forceServerSilence) { - return dispatch('sync', { forceClientState: true }) + const syncDiffLog = await dispatch('sync', { forceClientState: true }) + + if (!syncDiffLog.isEmpty()) { + diffLog.merge(syncDiffLog) + } } return diffLog diff --git a/core/modules/cart/test/unit/store/itemActions.spec.ts b/core/modules/cart/test/unit/store/itemActions.spec.ts index b24ac5bb07..a68bd04443 100644 --- a/core/modules/cart/test/unit/store/itemActions.spec.ts +++ b/core/modules/cart/test/unit/store/itemActions.spec.ts @@ -46,7 +46,8 @@ jest.mock('@vue-storefront/core/modules/cart/helpers', () => ({ createNotifications: jest.fn() }, createDiffLog: () => ({ - pushNotifications: jest.fn() + pushNotifications: jest.fn(), + merge: jest.fn() }) })); jest.mock('@vue-storefront/core/helpers', () => ({ @@ -140,7 +141,12 @@ describe('Cart itemActions', () => { } }) - contextMock.dispatch.mockImplementationOnce(() => Promise.resolve({ status: 'ok', onlineCheckTaskId: 1 })) + // The third 'dispatch' call gets an instance of DiffLog class, which has the isEmpty() method. + // The return value of the second 'dispatch' call is not used at all, so it can be left empty. + contextMock.dispatch + .mockImplementationOnce(() => Promise.resolve({ status: 'ok', onlineCheckTaskId: 1 })) + .mockImplementationOnce(() => Promise.resolve({})) + .mockImplementationOnce(() => Promise.resolve({ isEmpty: () => { return true } })) await (cartActions as any).addItems(contextMock, { productsToAdd: [product] }) expect(contextMock.commit).toBeCalledWith(types.CART_ADD_ITEM, { product: { ...product, onlineStockCheckid: 1 } })