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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
4 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';
import * as actions from '~/header_search/store/actions';
import * as types from '~/header_search/store/mutation_types';
2022-10-11 01:57:18 +05:30
import initState from '~/header_search/store/state';
2021-11-18 22:05:49 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
2022-10-11 01:57:18 +05:30
import {
MOCK_SEARCH,
MOCK_AUTOCOMPLETE_OPTIONS_RES,
MOCK_AUTOCOMPLETE_PATH,
MOCK_PROJECT,
MOCK_SEARCH_CONTEXT,
MOCK_SEARCH_PATH,
MOCK_MR_PATH,
MOCK_ISSUE_PATH,
} from '../mock_data';
2021-11-18 22:05:49 +05:30
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;
2022-10-11 01:57:18 +05:30
const createState = (initialState) =>
initState({
searchPath: MOCK_SEARCH_PATH,
issuesPath: MOCK_ISSUE_PATH,
mrPath: MOCK_MR_PATH,
autocompletePath: MOCK_AUTOCOMPLETE_PATH,
searchContext: MOCK_SEARCH_CONTEXT,
...initialState,
});
2021-11-11 11:23:49 +05:30
afterEach(() => {
state = null;
2021-11-18 22:05:49 +05:30
mock.restore();
});
describe.each`
2023-04-23 21:23:45 +05:30
axiosMock | type | expectedMutations
${{ method: 'onGet', code: HTTP_STATUS_OK, res: MOCK_AUTOCOMPLETE_OPTIONS_RES }} | ${'success'} | ${[{ type: types.REQUEST_AUTOCOMPLETE }, { type: types.RECEIVE_AUTOCOMPLETE_SUCCESS, payload: MOCK_AUTOCOMPLETE_OPTIONS_RES }, { type: types.RECEIVE_AUTOCOMPLETE_SUCCESS, payload: MOCK_AUTOCOMPLETE_OPTIONS_RES }]}
${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR, res: null }} | ${'error'} | ${[{ type: types.REQUEST_AUTOCOMPLETE }, { type: types.RECEIVE_AUTOCOMPLETE_ERROR }, { type: types.RECEIVE_AUTOCOMPLETE_ERROR }]}
2022-05-07 20:08:51 +05:30
`('fetchAutocompleteOptions', ({ axiosMock, type, expectedMutations }) => {
2021-11-18 22:05:49 +05:30
describe(`on ${type}`, () => {
beforeEach(() => {
2022-10-11 01:57:18 +05:30
state = createState({});
mock = new MockAdapter(axios);
mock[axiosMock.method]().reply(axiosMock.code, axiosMock.res);
2021-11-18 22:05:49 +05:30
});
it(`should dispatch the correct mutations`, () => {
return testAction({
action: actions.fetchAutocompleteOptions,
state,
expectedMutations,
2022-05-07 20:08:51 +05:30
});
2021-11-18 22:05:49 +05:30
});
});
2021-11-11 11:23:49 +05:30
});
2022-10-11 01:57:18 +05:30
describe.each`
project | ref | fetchType | expectedPath
${null} | ${null} | ${null} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}`}
${MOCK_PROJECT} | ${null} | ${'generic'} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=${MOCK_PROJECT.id}&filter=generic`}
${null} | ${MOCK_PROJECT.id} | ${'generic'} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_ref=${MOCK_PROJECT.id}&filter=generic`}
${MOCK_PROJECT} | ${MOCK_PROJECT.id} | ${'search'} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=${MOCK_PROJECT.id}&project_ref=${MOCK_PROJECT.id}&filter=search`}
`('autocompleteQuery', ({ project, ref, fetchType, expectedPath }) => {
describe(`when project is ${project?.name} and project ref is ${ref}`, () => {
beforeEach(() => {
state = createState({
search: MOCK_SEARCH,
searchContext: {
project,
ref,
},
});
});
it(`should return ${expectedPath}`, () => {
expect(actions.autocompleteQuery({ state, fetchType })).toBe(expectedPath);
});
});
});
2022-01-26 12:08:38 +05:30
describe('clearAutocomplete', () => {
2022-10-11 01:57:18 +05:30
beforeEach(() => {
state = createState({});
});
2022-01-26 12:08:38 +05:30
it('calls the CLEAR_AUTOCOMPLETE mutation', () => {
return testAction({
action: actions.clearAutocomplete,
state,
expectedMutations: [{ type: types.CLEAR_AUTOCOMPLETE }],
});
});
});
2021-11-11 11:23:49 +05:30
describe('setSearch', () => {
2022-10-11 01:57:18 +05:30
beforeEach(() => {
state = createState({});
});
2021-11-11 11:23:49 +05:30
it('calls the SET_SEARCH mutation', () => {
return testAction({
action: actions.setSearch,
payload: MOCK_SEARCH,
state,
expectedMutations: [{ type: types.SET_SEARCH, payload: MOCK_SEARCH }],
});
});
});
});