[Vuejs]-Vue – unit test watching a vuex getter property

0👍

Getters are cached so I needed to implement the mock getter using reactive state instead of mockReturnValue:

test('Dispatches fetchAccount when defaultInternalAccountId is updated', () => {
  const accountId = '1234';
  const accountId2 = '5678';
  const vm = new Vue({ data: { accountId } });

  mockDefaultInternalAccountId.mockImplementation(() => vm.accountId);

  shallowMount(AssetAllocationsSummaryContainer, {
    localVue,
    store,
  });

  expect(mockFetchAccount.mock.calls[0][1]).toEqual({ accountId });

  vm.accountId = accountId2;

  expect(mockFetchAccount).toHaveBeenCalledTimes(2);
  expect(mockFetchAccount.mock.calls[1][1]).toEqual({ accountId: accountId2 });
});

Leave a comment