From 02baf7ec05b937bdea6009e73431a60eb9d25cd6 Mon Sep 17 00:00:00 2001 From: Luke Romanowicz Date: Tue, 23 Apr 2019 08:50:25 +0200 Subject: [PATCH] Fix Cart mutations tests --- .../cart/test/unit/store/mutations.spec.ts | 425 +++++++++--------- 1 file changed, 212 insertions(+), 213 deletions(-) diff --git a/core/modules/cart/test/unit/store/mutations.spec.ts b/core/modules/cart/test/unit/store/mutations.spec.ts index ffb95c04d0..7ec3b8d096 100644 --- a/core/modules/cart/test/unit/store/mutations.spec.ts +++ b/core/modules/cart/test/unit/store/mutations.spec.ts @@ -1,29 +1,32 @@ import Vue from 'vue' -import * as types from "../../../store/mutation-types"; -import cartMutations from '../../../store/mutations'; - +import * as types from '../../../store/mutation-types' +import cartMutations from '../../../store/mutations' Vue.prototype.$bus = { $emit: jest.fn() -}; +} -describe('Cart mutations', () => { +jest.mock('@vue-storefront/core/store', () => ({ + state: { + config: {} + } +})) +describe('Cart mutations', () => { beforeEach(() => { - jest.clearAllMocks(); - }); + jest.clearAllMocks() + }) describe('CART_ADD_ITEM', () => { - it('adds a product to cart if none of its sku is there yet', () => { const stateMock = { cartItems: [] - }; + } const product = { qty: 123, sku: 'foo' - }; + } const expectedState = { cartItems: [ { @@ -31,22 +34,22 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; - const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }); + } + const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-add', { product }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-add', { product }) expect(stateMock).toEqual(expectedState) - }); + }) it('adds a product to cart with quantity of 1 if none of its sku is there yet and product qty is not provided', () => { const stateMock = { cartItems: [] - }; + } const product = { sku: 'foo' - }; + } const expectedState = { cartItems: [ { @@ -54,14 +57,14 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; - const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }); + } + const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-add', { product: { ...product, qty: 1 } }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-add', { product: { ...product, qty: 1 } }) expect(stateMock).toEqual(expectedState) - }); + }) it('increases quantity of a a product in cart if one of its sku is already there', () => { const stateMock = { @@ -71,11 +74,11 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; + } const product = { qty: 10, sku: 'foo' - }; + } const expectedState = { cartItems: [ { @@ -83,13 +86,13 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; - const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }); + } + const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, { product }) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); + expect(stateMock).toEqual(expectedState) + }) it('increases quantity of a a product in cart by 1 if quantity was not provided', () => { const stateMock = { @@ -99,10 +102,10 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; + } const product = { sku: 'foo' - }; + } const expectedState = { cartItems: [ { @@ -110,14 +113,14 @@ describe('Cart mutations', () => { sku: 'foo' } ] - }; - const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, {product}); + } + const wrapper = (mutations: any) => mutations[types.CART_ADD_ITEM](stateMock, {product}) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); - }); + expect(stateMock).toEqual(expectedState) + }) + }) it('CART_SAVE emits cart-before-save and updates save date', () => { const stateMock = { @@ -128,7 +131,7 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [ { @@ -137,19 +140,18 @@ describe('Cart mutations', () => { } ], cartSavedAt: 1445412480000 - }; - const wrapper = (mutations: any) => mutations[types.CART_SAVE](stateMock); + } + const wrapper = (mutations: any) => mutations[types.CART_SAVE](stateMock) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-save', {items: stateMock.cartItems}); - expect(stateMock).toEqual(expectedState); - }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-save', {items: stateMock.cartItems}) + expect(stateMock).toEqual(expectedState) + }) describe('CART_DEL_ITEM', () => { - it('removes product from cart by sku', () => { const stateMock = { cartItems: [ @@ -159,32 +161,32 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_DEL_ITEM]( stateMock, { product: {sku: 'foo'}, removeByParentSku: false } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', { items: [{ qty: 10, sku: 'foo' }] - }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}); - expect(stateMock).toEqual(expectedState); - }); + }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}) + expect(stateMock).toEqual(expectedState) + }) it('removes product from cart by parent sku', () => { const stateMock = { @@ -195,33 +197,32 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_DEL_ITEM]( stateMock, { - product: {parentSku: 'foo'}, + product: {parentSku: 'foo'} } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', { items: [{ qty: 10, sku: 'foo' }] - }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}); - expect(stateMock).toEqual(expectedState); - }); - }); - + }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}) + expect(stateMock).toEqual(expectedState) + }) + }) describe('CART_DEL_NON_CONFIRMED_ITEM', () => { it('removes product from cart by sku', () => { @@ -233,32 +234,32 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_DEL_NON_CONFIRMED_ITEM]( stateMock, { product: {sku: 'foo'}, removeByParentSku: false } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', { items: [{ qty: 10, sku: 'foo' }] - }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}); - expect(stateMock).toEqual(expectedState); - }); + }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}) + expect(stateMock).toEqual(expectedState) + }) it('removes product from cart by parent sku', () => { const stateMock = { @@ -269,31 +270,31 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_DEL_NON_CONFIRMED_ITEM]( stateMock, { product: {parentSku: 'foo'} } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', { items: [{ qty: 10, sku: 'foo' }] - }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}); - expect(stateMock).toEqual(expectedState); - }); + }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}) + expect(stateMock).toEqual(expectedState) + }) it('does not remove a product that has server_item_id set, so it surely exists in the backend', () => { const stateMock = { @@ -305,7 +306,7 @@ describe('Cart mutations', () => { } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [ { @@ -315,106 +316,104 @@ describe('Cart mutations', () => { } ], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_DEL_NON_CONFIRMED_ITEM]( stateMock, { product: {sku: 'foo'}, removeByParentSku: false } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', {items: stateMock.cartItems}); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}); - expect(stateMock).toEqual(expectedState); - }); - }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-delete', {items: stateMock.cartItems}) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-delete', {items: expectedState.cartItems}) + expect(stateMock).toEqual(expectedState) + }) + }) describe('CART_UPD_ITEM', () => { - it('updates product quantity by sku', () => { const stateMock = { cartItems: [ { sku: 'foo', - qty: 10, + qty: 10 } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [ { qty: 20, - sku: 'foo', + sku: 'foo' } ], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_ITEM]( stateMock, { product: { sku: 'foo' }, qty: 20 } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) // unfortunately before and after events return a reference to the same object, therefore // after performing this mutation after event return same object with same, updated value as before event - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-update', { product: expectedState.cartItems[0] }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-update', { product: expectedState.cartItems[0] }); - expect(stateMock).toEqual(expectedState); - }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-before-update', { product: expectedState.cartItems[0] }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-update', { product: expectedState.cartItems[0] }) + expect(stateMock).toEqual(expectedState) + }) it('doesn\'t update anything if product is not found in cart', () => { const stateMock = { cartItems: [ { sku: 'foo', - qty: 10, + qty: 10 } ], cartSavedAt: 0 - }; - const expectedState = { ...stateMock }; + } + const expectedState = { ...stateMock } const wrapper = (mutations: any) => mutations[types.CART_UPD_ITEM]( stateMock, { product: {sku: 'qux'}, qty: 20 } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).not.toBeCalled(); - expect(stateMock).toEqual(expectedState); - }); - }); + expect(Vue.prototype.$bus.$emit).not.toBeCalled() + expect(stateMock).toEqual(expectedState) + }) + }) describe('CART_UPD_ITEM_PROPS', () => { - it('updates product properties by sku', () => { const stateMock = { cartItems: [ { sku: 'foo', someProp: 'bar', - qty: 10, + qty: 10 } ], cartSavedAt: new Date(0) - }; + } const expectedState = { cartItems: [ { @@ -424,27 +423,27 @@ describe('Cart mutations', () => { } ], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_ITEM_PROPS]( stateMock, { product: {sku: 'foo', someProp: 'baz', qty: 20} } - ); - let firstEmitCall = []; + ) + let firstEmitCall = [] Vue.prototype.$bus.$emit.mockImplementationOnce((eventName, args) => { - firstEmitCall.push(eventName); - firstEmitCall.push(args); - }); - Date.now = jest.fn(() => expectedState.cartSavedAt); + firstEmitCall.push(eventName) + firstEmitCall.push(args) + }) + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(firstEmitCall).toEqual(['cart-before-itemchanged', { item: expectedState.cartItems[0] }]); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-itemchanged', { item: expectedState.cartItems[0] }); - expect(stateMock).toEqual(expectedState); - }); + expect(firstEmitCall).toEqual(['cart-before-itemchanged', { item: expectedState.cartItems[0] }]) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-itemchanged', { item: expectedState.cartItems[0] }) + expect(stateMock).toEqual(expectedState) + }) it('updates product properties by server_item_id', () => { const stateMock = { @@ -453,11 +452,11 @@ describe('Cart mutations', () => { sku: 'foo', server_item_id: 123, someProp: 'bar', - qty: 10, + qty: 10 } ], cartSavedAt: 0 - }; + } const expectedState = { cartItems: [ { @@ -468,27 +467,27 @@ describe('Cart mutations', () => { } ], cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_ITEM_PROPS]( stateMock, { product: {server_item_id: 123, sku: 'bar', someProp: 'baz', qty: 20} } - ); - let firstEmitCall = []; + ) + let firstEmitCall = [] Vue.prototype.$bus.$emit.mockImplementationOnce((eventName, args) => { - firstEmitCall.push(eventName); - firstEmitCall.push(args); - }); - Date.now = jest.fn(() => expectedState.cartSavedAt); + firstEmitCall.push(eventName) + firstEmitCall.push(args) + }) + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(firstEmitCall).toEqual(['cart-before-itemchanged', { item: expectedState.cartItems[0] }]); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-itemchanged', { item: expectedState.cartItems[0] }); - expect(stateMock).toEqual(expectedState); - }); + expect(firstEmitCall).toEqual(['cart-before-itemchanged', { item: expectedState.cartItems[0] }]) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-itemchanged', { item: expectedState.cartItems[0] }) + expect(stateMock).toEqual(expectedState) + }) it('doesn\'t update anything if product is not found in cart', () => { const stateMock = { @@ -496,114 +495,114 @@ describe('Cart mutations', () => { { sku: 'foo', someProp: 'bar', - qty: 10, + qty: 10 } ], cartSavedAt: 0 - }; - const expectedState = { ...stateMock }; + } + const expectedState = { ...stateMock } const wrapper = (mutations: any) => mutations[types.CART_UPD_ITEM_PROPS]( stateMock, { product: {sku: 'qux', someProp: 'baz', qty: 20} } - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).not.toBeCalled(); - expect(stateMock).toEqual(expectedState); - }); - }); + expect(Vue.prototype.$bus.$emit).not.toBeCalled() + expect(stateMock).toEqual(expectedState) + }) + }) it('CART_UPD_SHIPPING sets given shipping method', () => { const stateMock = { shipping: 'foo', cartSavedAt: 0 - }; + } const expectedState = { shipping: 'bar', cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_SHIPPING]( stateMock, expectedState.shipping - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); + expect(stateMock).toEqual(expectedState) + }) it('CART_LOAD_CART initializes cart with given products', () => { - const stateMock = {}; + const stateMock = {} const expectedState = { cartItems: [ { sku: 'foo', - qty: 10, + qty: 10 } ], cartIsLoaded: true, cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_LOAD_CART]( stateMock, expectedState.cartItems - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('sync/PROCESS_QUEUE', { config: {} }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('application-after-loaded'); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-loaded'); - expect(stateMock).toEqual(expectedState); - }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('sync/PROCESS_QUEUE', { config: {} }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('application-after-loaded') + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-loaded') + expect(stateMock).toEqual(expectedState) + }) it('CART_LOAD_CART initializes an empty cart when no products are given', () => { - const stateMock = {}; + const stateMock = {} const expectedState = { cartItems: [], cartIsLoaded: true, cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_LOAD_CART]( stateMock - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(Vue.prototype.$bus.$emit).toBeCalledWith('sync/PROCESS_QUEUE', { config: {} }); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('application-after-loaded'); - expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-loaded'); - expect(stateMock).toEqual(expectedState); - }); + expect(Vue.prototype.$bus.$emit).toBeCalledWith('sync/PROCESS_QUEUE', { config: {} }) + expect(Vue.prototype.$bus.$emit).toBeCalledWith('application-after-loaded') + expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-loaded') + expect(stateMock).toEqual(expectedState) + }) it('CART_LOAD_CART_SERVER_TOKEN saves given cart token in cart data', () => { - const stateMock = {}; + const stateMock = {} const expectedState = { cartServerToken: 'foo' - }; + } const wrapper = (mutations: any) => mutations[types.CART_LOAD_CART_SERVER_TOKEN]( stateMock, expectedState.cartServerToken - ); + ) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); + expect(stateMock).toEqual(expectedState) + }) it('CART_UPD_TOTALS updates totals related data', () => { - const stateMock = {}; + const stateMock = {} const expectedState = { cartServerTotalsAt: 1445412480000, itemsAfterPlatformTotals: ['foo'], @@ -614,7 +613,7 @@ describe('Cart mutations', () => { {'code': 'subtotal', 'title': 'Subtotal', 'value': 39.36}, {'code': 'grand_total', 'title': 'Grand Total', 'value': 39.36, 'area': 'footer'} ] - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_TOTALS]( stateMock, { @@ -622,53 +621,53 @@ describe('Cart mutations', () => { totals: expectedState.platformTotals, platformTotalSegments: expectedState.platformTotalSegments } - ); + ) - Date.now = jest.fn(() => expectedState.cartServerTotalsAt); + Date.now = jest.fn(() => expectedState.cartServerTotalsAt) - wrapper(cartMutations); + wrapper(cartMutations) expect(Vue.prototype.$bus.$emit).toBeCalledWith('cart-after-updatetotals', { platformTotals: expectedState.platformTotals, platformTotalSegments: expectedState.platformTotalSegments - }); - expect(stateMock).toEqual(expectedState); - }); + }) + expect(stateMock).toEqual(expectedState) + }) it('CART_UPD_PAYMENT sets given payment method', () => { const stateMock = { payment: 'foo', cartSavedAt: 0 - }; + } const expectedState = { payment: 'bar', cartSavedAt: 1445412480000 - }; + } const wrapper = (mutations: any) => mutations[types.CART_UPD_PAYMENT]( stateMock, expectedState.payment - ); + ) - Date.now = jest.fn(() => expectedState.cartSavedAt); + Date.now = jest.fn(() => expectedState.cartSavedAt) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); + expect(stateMock).toEqual(expectedState) + }) it('CART_TOGGLE_MICROCART changes microcart open status to the opposite one', () => { const stateMock = { isMicrocartOpen: true - }; + } const expectedState = { isMicrocartOpen: false - }; + } const wrapper = (mutations: any) => mutations[types.CART_TOGGLE_MICROCART]( - stateMock, - ); + stateMock + ) - wrapper(cartMutations); + wrapper(cartMutations) - expect(stateMock).toEqual(expectedState); - }); -}); + expect(stateMock).toEqual(expectedState) + }) +})