debian-mirror-gitlab/spec/frontend/header_search/store/actions_spec.js

61 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-11-18 22:05:49 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-11-11 11:23:49 +05:30
import testAction from 'helpers/vuex_action_helper';
2021-11-18 22:05:49 +05:30
import createFlash from '~/flash';
2021-11-11 11:23:49 +05:30
import * as actions from '~/header_search/store/actions';
import * as types from '~/header_search/store/mutation_types';
import createState from '~/header_search/store/state';
2021-11-18 22:05:49 +05:30
import axios from '~/lib/utils/axios_utils';
import { MOCK_SEARCH, MOCK_AUTOCOMPLETE_OPTIONS } from '../mock_data';
jest.mock('~/flash');
2021-11-11 11:23:49 +05:30
describe('Header Search Store Actions', () => {
let state;
2021-11-18 22:05:49 +05:30
let mock;
const flashCallback = (callCount) => {
expect(createFlash).toHaveBeenCalledTimes(callCount);
createFlash.mockClear();
};
2021-11-11 11:23:49 +05:30
beforeEach(() => {
state = createState({});
2021-11-18 22:05:49 +05:30
mock = new MockAdapter(axios);
2021-11-11 11:23:49 +05:30
});
afterEach(() => {
state = null;
2021-11-18 22:05:49 +05:30
mock.restore();
});
describe.each`
axiosMock | type | expectedMutations | flashCallCount
${{ method: 'onGet', code: 200, res: MOCK_AUTOCOMPLETE_OPTIONS }} | ${'success'} | ${[{ type: types.REQUEST_AUTOCOMPLETE }, { type: types.RECEIVE_AUTOCOMPLETE_SUCCESS, payload: MOCK_AUTOCOMPLETE_OPTIONS }]} | ${0}
${{ method: 'onGet', code: 500, res: null }} | ${'error'} | ${[{ type: types.REQUEST_AUTOCOMPLETE }, { type: types.RECEIVE_AUTOCOMPLETE_ERROR }]} | ${1}
`('fetchAutocompleteOptions', ({ axiosMock, type, expectedMutations, flashCallCount }) => {
describe(`on ${type}`, () => {
beforeEach(() => {
mock[axiosMock.method]().replyOnce(axiosMock.code, axiosMock.res);
});
it(`should dispatch the correct mutations`, () => {
return testAction({
action: actions.fetchAutocompleteOptions,
state,
expectedMutations,
}).then(() => flashCallback(flashCallCount));
});
});
2021-11-11 11:23:49 +05:30
});
describe('setSearch', () => {
it('calls the SET_SEARCH mutation', () => {
return testAction({
action: actions.setSearch,
payload: MOCK_SEARCH,
state,
expectedMutations: [{ type: types.SET_SEARCH, payload: MOCK_SEARCH }],
});
});
});
});