2021-09-30 23:02:18 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import Vue from 'vue';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Vuex from 'vuex';
|
2021-02-22 17:27:13 +05:30
|
|
|
import { MOCK_PROJECT, MOCK_QUERY } from 'jest/search/mock_data';
|
|
|
|
import { visitUrl, setUrlParams } from '~/lib/utils/url_utility';
|
2021-09-30 23:02:18 +05:30
|
|
|
import { PROJECTS_LOCAL_STORAGE_KEY } from '~/search/store/constants';
|
2021-02-22 17:27:13 +05:30
|
|
|
import ProjectFilter from '~/search/topbar/components/project_filter.vue';
|
|
|
|
import SearchableDropdown from '~/search/topbar/components/searchable_dropdown.vue';
|
|
|
|
import { ANY_OPTION, GROUP_DATA, PROJECT_DATA } from '~/search/topbar/constants';
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
Vue.use(Vuex);
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
|
|
|
visitUrl: jest.fn(),
|
|
|
|
setUrlParams: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('ProjectFilter', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
const actionSpies = {
|
|
|
|
fetchProjects: jest.fn(),
|
2021-09-30 23:02:18 +05:30
|
|
|
setFrequentProject: jest.fn(),
|
|
|
|
loadFrequentProjects: jest.fn(),
|
2021-02-22 17:27:13 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
initialData: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const createComponent = (initialState, props) => {
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
state: {
|
|
|
|
query: MOCK_QUERY,
|
|
|
|
...initialState,
|
|
|
|
},
|
|
|
|
actions: actionSpies,
|
2021-09-30 23:02:18 +05:30
|
|
|
getters: {
|
|
|
|
frequentProjects: () => [],
|
|
|
|
},
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
wrapper = shallowMount(ProjectFilter, {
|
|
|
|
store,
|
|
|
|
propsData: {
|
|
|
|
...defaultProps,
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const findSearchableDropdown = () => wrapper.find(SearchableDropdown);
|
|
|
|
|
|
|
|
describe('template', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders SearchableDropdown always', () => {
|
|
|
|
expect(findSearchableDropdown().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('events', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when @search is emitted', () => {
|
|
|
|
const search = 'test';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
findSearchableDropdown().vm.$emit('search', search);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls fetchProjects with the search paramter', () => {
|
|
|
|
expect(actionSpies.fetchProjects).toHaveBeenCalledWith(expect.any(Object), search);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when @change is emitted', () => {
|
|
|
|
describe('with Any', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
findSearchableDropdown().vm.$emit('change', ANY_OPTION);
|
|
|
|
});
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
it('calls setUrlParams with null, no group id, then calls visitUrl', () => {
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(setUrlParams).toHaveBeenCalledWith({
|
2021-09-30 23:02:18 +05:30
|
|
|
[PROJECT_DATA.queryParam]: null,
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
|
|
|
expect(visitUrl).toHaveBeenCalled();
|
|
|
|
});
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it('does not call setFrequentProject', () => {
|
|
|
|
expect(actionSpies.setFrequentProject).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('with a Project', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
findSearchableDropdown().vm.$emit('change', MOCK_PROJECT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls setUrlParams with project id, group id, then calls visitUrl', () => {
|
|
|
|
expect(setUrlParams).toHaveBeenCalledWith({
|
2021-03-11 19:13:27 +05:30
|
|
|
[GROUP_DATA.queryParam]: MOCK_PROJECT.namespace.id,
|
2021-02-22 17:27:13 +05:30
|
|
|
[PROJECT_DATA.queryParam]: MOCK_PROJECT.id,
|
|
|
|
});
|
|
|
|
expect(visitUrl).toHaveBeenCalled();
|
|
|
|
});
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
it(`calls setFrequentProject with the group and ${PROJECTS_LOCAL_STORAGE_KEY}`, () => {
|
|
|
|
expect(actionSpies.setFrequentProject).toHaveBeenCalledWith(
|
|
|
|
expect.any(Object),
|
|
|
|
MOCK_PROJECT,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when @first-open is emitted', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
findSearchableDropdown().vm.$emit('first-open');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls loadFrequentProjects', () => {
|
|
|
|
expect(actionSpies.loadFrequentProjects).toHaveBeenCalledTimes(1);
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('computed', () => {
|
|
|
|
describe('selectedProject', () => {
|
|
|
|
describe('when initialData is null', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets selectedProject to ANY_OPTION', () => {
|
|
|
|
expect(wrapper.vm.selectedProject).toBe(ANY_OPTION);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when initialData is set', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createComponent({}, { initialData: MOCK_PROJECT });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets selectedProject to the initialData', () => {
|
|
|
|
expect(wrapper.vm.selectedProject).toBe(MOCK_PROJECT);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|