<commit_before>import * as types from '@vue-storefront/core/modules/cart/store/mutation-types'
import { configureProductAsync } from '@vue-storefront/core/modules/catalog/helpers'
import { prepareProductsToAdd, productsEquals, validateProduct } from '@vue-storefront/core/modules/cart/helpers'
import cartActions from '@vue-storefront/core/modules/cart/store/actions';
import { createContextMock } from '@vue-storefront/unit-tests/utils';
import config from 'config';

jest.mock('@vue-storefront/core/store', () => ({
  dispatch: jest.fn(),
  state: {}
}));
jest.mock('js-sha3', () => ({ sha3_224: jest.fn() }));
jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) }));
jest.mock('config', () => ({}));
jest.mock('@vue-storefront/core/lib/multistore', () => ({
  currentStoreView: jest.fn(),
  localizedRoute: jest.fn()
}));
jest.mock('@vue-storefront/core/lib/logger', () => ({
  Logger: {
    log: jest.fn(() => () => {}),
    debug: jest.fn(() => () => {}),
    warn: jest.fn(() => () => {}),
    error: jest.fn(() => () => {}),
    info: jest.fn(() => () => {})
  }
}));
jest.mock('@vue-storefront/core/data-resolver', () => ({ CartService: {
  applyCoupon: async () => ({ result: true }),
  removeCoupon: async () => ({ result: true })
} }));
jest.mock('@vue-storefront/core/lib/storage-manager', () => ({
  StorageManager: {
    get: jest.fn()
  }
}));
jest.mock('@vue-storefront/core/app', () => ({ router: jest.fn() }))
jest.mock('@vue-storefront/core/modules/catalog/helpers', () => ({
  configureProductAsync: jest.fn()
}));
jest.mock('@vue-storefront/core/modules/cart/helpers', () => ({
  prepareProductsToAdd: jest.fn(),
  productsEquals: jest.fn(),
  validateProduct: jest.fn(),
  notifications: {
    createNotifications: jest.fn()
  },
  createDiffLog: () => ({
    pushNotifications: jest.fn(),
    merge: jest.fn()
  })
}));
jest.mock('@vue-storefront/core/helpers', () => ({
  get isServer () {
    return true
  },
  onlineHelper: {
    get isOnline () {
      return true
    }
  },
  processLocalizedURLAddress: (url) => url
}));
jest.mock('config', () => ({}));

describe('Cart itemActions', () => {
  it('configures item and deletes when there is same sku', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const product2 = { sku: 2, name: 'product2', server_item_id: 2 }

    const configureProductAsyncMock = configureProductAsync as jest.Mock
    configureProductAsyncMock.mockImplementation(() => product2)

    const contextMock = createContextMock({
      getters: {
        isCartSyncEnabled: true,
        getCartItems: [product2]
      }
    })

    await (cartActions as any).configureItem(contextMock, { product: product1, configuration: {} })
    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_DEL_ITEM, { product: product2 })
    expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_UPD_ITEM_PROPS, { product: { ...product1, ...product2 } })
    expect(contextMock.dispatch).toBeCalledWith('sync', { forceClientState: true })
  })

  it('configures item', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const product2 = { sku: 2, name: 'product2', server_item_id: 2 }

    const configureProductAsyncMock = configureProductAsync as jest.Mock
    configureProductAsyncMock.mockImplementation(() => product2)

    const contextMock = createContextMock({
      getters: {
        isCartSyncEnabled: true,
        getCartItems: [product1]
      }
    })

    await (cartActions as any).configureItem(contextMock, { product: product1, configuration: {} })
    expect(contextMock.commit).not.toHaveBeenNthCalledWith(1, types.CART_DEL_ITEM, { product: product2 })
    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_UPD_ITEM_PROPS, { product: { ...product1, ...product2 } })
    expect(contextMock.dispatch).toBeCalledWith('sync', { forceClientState: true })
  })

  it('adds item to the cart', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const prepareProductsToAddMock = prepareProductsToAdd as jest.Mock
    prepareProductsToAddMock.mockImplementation(() => [product1])
    const contextMock = createContextMock()

    await (cartActions as any).addItem(contextMock, { productToAdd: product1 })

    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_ADDING_ITEM, { isAdding: true })
    expect(contextMock.dispatch).toBeCalledWith('addItems', { productsToAdd: [product1], forceServerSilence: false })
    expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_ADDING_ITEM, { isAdding: false })
  })<commit_msg>fix tests<commit_after>import * as types from '@vue-storefront/core/modules/cart/store/mutation-types'
import { configureProductAsync } from '@vue-storefront/core/modules/catalog/helpers'
import { prepareProductsToAdd, productsEquals, validateProduct } from '@vue-storefront/core/modules/cart/helpers'
import cartActions from '@vue-storefront/core/modules/cart/store/actions';
import { createContextMock } from '@vue-storefront/unit-tests/utils';
import config from 'config';

jest.mock('@vue-storefront/core/store', () => ({
  dispatch: jest.fn(),
  state: {}
}));
jest.mock('js-sha3', () => ({ sha3_224: jest.fn() }));
jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) }));
jest.mock('config', () => ({}));
jest.mock('@vue-storefront/core/lib/multistore', () => ({
  currentStoreView: jest.fn(),
  localizedRoute: jest.fn()
}));
jest.mock('@vue-storefront/core/lib/logger', () => ({
  Logger: {
    log: jest.fn(() => () => {}),
    debug: jest.fn(() => () => {}),
    warn: jest.fn(() => () => {}),
    error: jest.fn(() => () => {}),
    info: jest.fn(() => () => {})
  }
}));
jest.mock('@vue-storefront/core/data-resolver', () => ({ CartService: {
  applyCoupon: async () => ({ result: true }),
  removeCoupon: async () => ({ result: true })
} }));
jest.mock('@vue-storefront/core/lib/storage-manager', () => ({
  StorageManager: {
    get: jest.fn()
  }
}));
jest.mock('@vue-storefront/core/app', () => ({ router: jest.fn() }))
jest.mock('@vue-storefront/core/modules/catalog/helpers', () => ({
  configureProductAsync: jest.fn()
}));
jest.mock('@vue-storefront/core/modules/cart/helpers', () => ({
  prepareProductsToAdd: jest.fn(),
  productsEquals: jest.fn(),
  validateProduct: jest.fn(),
  notifications: {
    createNotifications: jest.fn()
  },
  createDiffLog: () => ({
    pushNotifications: jest.fn(),
    merge: jest.fn()
  })
}));
jest.mock('@vue-storefront/core/helpers', () => ({
  get isServer () {
    return true
  },
  onlineHelper: {
    get isOnline () {
      return true
    }
  },
  processLocalizedURLAddress: (url) => url
}));
jest.mock('config', () => ({}));

describe('Cart itemActions', () => {
  it('configures item and deletes when there is same sku', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const product2 = { sku: 2, name: 'product2', server_item_id: 2 }

    const contextMock = createContextMock({
      getters: {
        isCartSyncEnabled: true,
        getCartItems: [product2]
      },
      dispatch: jest.fn((actionName) => {
        switch (actionName) {
          case 'product/getProductVariant': {
            return product2
          }
        }
      })
    })

    await (cartActions as any).configureItem(contextMock, { product: product1, configuration: {} })
    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_DEL_ITEM, { product: product2 })
    expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_UPD_ITEM_PROPS, { product: { ...product1, ...product2 } })
    expect(contextMock.dispatch).toBeCalledWith('sync', { forceClientState: true })
  })

  it('configures item', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const product2 = { sku: 2, name: 'product2', server_item_id: 2 }

    const contextMock = createContextMock({
      getters: {
        isCartSyncEnabled: true,
        getCartItems: [product1]
      },
      dispatch: jest.fn((actionName) => {
        switch (actionName) {
          case 'product/getProductVariant': {
            return product2
          }
        }
      })
    })

    await (cartActions as any).configureItem(contextMock, { product: product1, configuration: {} })
    expect(contextMock.commit).not.toHaveBeenNthCalledWith(1, types.CART_DEL_ITEM, { product: product2 })
    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_UPD_ITEM_PROPS, { product: { ...product1, ...product2 } })
    expect(contextMock.dispatch).toBeCalledWith('sync', { forceClientState: true })
  })

  it('adds item to the cart', async () => {
    const product1 = { sku: 1, name: 'product1', server_item_id: 1 }
    const prepareProductsToAddMock = prepareProductsToAdd as jest.Mock
    prepareProductsToAddMock.mockImplementation(() => [product1])
    const contextMock = createContextMock()

    await (cartActions as any).addItem(contextMock, { productToAdd: product1 })

    expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_ADDING_ITEM, { isAdding: true })
    expect(contextMock.dispatch).toBeCalledWith('addItems', { productsToAdd: [product1], forceServerSilence: false })
    expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_ADDING_ITEM, { isAdding: false })
  })
{'fraction_non_alphanumeric': 0.1775501493811353, 'fraction_numerical': 0.010243277848911651, 'mean_word_length': 3.990947816826411, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3744913', 'n_tokens_mistral': 3086, 'n_tokens_neox': 3037, 'n_words': 529}