2020-08-28 18:10:51 +00:00
|
|
|
import {
|
|
|
|
GlFilteredSearchToken,
|
|
|
|
GlFilteredSearchSuggestion,
|
|
|
|
GlFilteredSearchTokenSegment,
|
2020-09-14 18:09:48 +00:00
|
|
|
GlDropdownDivider,
|
2020-08-28 18:10:51 +00:00
|
|
|
} from '@gitlab/ui';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-08-12 15:10:02 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-02-14 18:09:20 +00:00
|
|
|
import createFlash from '~/flash';
|
2020-08-12 15:10:02 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
2020-08-28 18:10:51 +00:00
|
|
|
import { DEFAULT_MILESTONES } from '~/vue_shared/components/filtered_search_bar/constants';
|
2020-08-12 15:10:02 +00:00
|
|
|
import MilestoneToken from '~/vue_shared/components/filtered_search_bar/tokens/milestone_token.vue';
|
|
|
|
|
|
|
|
import {
|
|
|
|
mockMilestoneToken,
|
|
|
|
mockMilestones,
|
|
|
|
mockRegularMilestone,
|
|
|
|
mockEscapedMilestone,
|
|
|
|
} from '../mock_data';
|
|
|
|
|
|
|
|
jest.mock('~/flash');
|
|
|
|
|
2020-08-28 18:10:51 +00:00
|
|
|
const defaultStubs = {
|
|
|
|
Portal: true,
|
|
|
|
GlFilteredSearchSuggestionList: {
|
|
|
|
template: '<div></div>',
|
|
|
|
methods: {
|
|
|
|
getValue: () => '=',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-09-02 06:10:47 +00:00
|
|
|
function createComponent(options = {}) {
|
|
|
|
const {
|
|
|
|
config = mockMilestoneToken,
|
|
|
|
value = { data: '' },
|
|
|
|
active = false,
|
|
|
|
stubs = defaultStubs,
|
|
|
|
} = options;
|
|
|
|
return mount(MilestoneToken, {
|
2020-08-12 15:10:02 +00:00
|
|
|
propsData: {
|
|
|
|
config,
|
|
|
|
value,
|
|
|
|
active,
|
|
|
|
},
|
|
|
|
provide: {
|
|
|
|
portalName: 'fake target',
|
|
|
|
alignSuggestions: function fakeAlignSuggestions() {},
|
2020-11-13 12:09:03 +00:00
|
|
|
suggestionsListClass: 'custom-class',
|
2020-08-12 15:10:02 +00:00
|
|
|
},
|
2020-08-28 18:10:51 +00:00
|
|
|
stubs,
|
2020-08-12 15:10:02 +00:00
|
|
|
});
|
2020-09-02 06:10:47 +00:00
|
|
|
}
|
2020-08-12 15:10:02 +00:00
|
|
|
|
|
|
|
describe('MilestoneToken', () => {
|
|
|
|
let mock;
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('computed', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
// Milestone title with spaces is always enclosed in quotations by component.
|
|
|
|
wrapper = createComponent({ value: { data: `"${mockEscapedMilestone.title}"` } });
|
|
|
|
|
|
|
|
wrapper.setData({
|
|
|
|
milestones: mockMilestones,
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('currentValue', () => {
|
|
|
|
it('returns lowercase string for `value.data`', () => {
|
|
|
|
expect(wrapper.vm.currentValue).toBe('"5.0 rc1"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('activeMilestone', () => {
|
|
|
|
it('returns object for currently present `value.data`', () => {
|
|
|
|
expect(wrapper.vm.activeMilestone).toEqual(mockEscapedMilestone);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('methods', () => {
|
|
|
|
describe('fetchMilestoneBySearchTerm', () => {
|
|
|
|
it('calls `config.fetchMilestones` with provided searchTerm param', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchMilestones');
|
|
|
|
|
|
|
|
wrapper.vm.fetchMilestoneBySearchTerm('foo');
|
|
|
|
|
|
|
|
expect(wrapper.vm.config.fetchMilestones).toHaveBeenCalledWith('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets response to `milestones` when request is successful', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchMilestones').mockResolvedValue({
|
|
|
|
data: mockMilestones,
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.vm.fetchMilestoneBySearchTerm();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(wrapper.vm.milestones).toEqual(mockMilestones);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls `createFlash` with flash error message when request fails', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchMilestones').mockRejectedValue({});
|
|
|
|
|
|
|
|
wrapper.vm.fetchMilestoneBySearchTerm('foo');
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
2020-11-05 00:09:16 +00:00
|
|
|
expect(createFlash).toHaveBeenCalledWith({
|
|
|
|
message: 'There was a problem fetching milestones.',
|
|
|
|
});
|
2020-08-12 15:10:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets `loading` to false when request completes', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchMilestones').mockRejectedValue({});
|
|
|
|
|
|
|
|
wrapper.vm.fetchMilestoneBySearchTerm('foo');
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(wrapper.vm.loading).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('template', () => {
|
2020-12-23 12:10:26 +00:00
|
|
|
const defaultMilestones = [
|
|
|
|
{ text: 'foo', value: 'foo' },
|
|
|
|
{ text: 'bar', value: 'baz' },
|
|
|
|
];
|
2020-08-28 18:10:51 +00:00
|
|
|
|
2020-08-12 15:10:02 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
wrapper = createComponent({ value: { data: `"${mockRegularMilestone.title}"` } });
|
|
|
|
|
|
|
|
wrapper.setData({
|
|
|
|
milestones: mockMilestones,
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders gl-filtered-search-token component', () => {
|
|
|
|
expect(wrapper.find(GlFilteredSearchToken).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders token item when value is selected', () => {
|
|
|
|
const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);
|
|
|
|
|
|
|
|
expect(tokenSegments).toHaveLength(3); // Milestone, =, '%"4.0"'
|
2020-09-03 21:08:18 +00:00
|
|
|
expect(tokenSegments.at(2).text()).toBe(`%${mockRegularMilestone.title}`); // "4.0 RC1"
|
2020-08-12 15:10:02 +00:00
|
|
|
});
|
2020-08-28 18:10:51 +00:00
|
|
|
|
|
|
|
it('renders provided defaultMilestones as suggestions', async () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockMilestoneToken, defaultMilestones },
|
|
|
|
stubs: { Portal: true },
|
|
|
|
});
|
|
|
|
const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);
|
|
|
|
const suggestionsSegment = tokenSegments.at(2);
|
|
|
|
suggestionsSegment.vm.$emit('activate');
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
|
|
|
const suggestions = wrapper.findAll(GlFilteredSearchSuggestion);
|
|
|
|
|
|
|
|
expect(suggestions).toHaveLength(defaultMilestones.length);
|
|
|
|
defaultMilestones.forEach((milestone, index) => {
|
|
|
|
expect(suggestions.at(index).text()).toBe(milestone.text);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-02 06:10:47 +00:00
|
|
|
it('does not render divider when no defaultMilestones', async () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockMilestoneToken, defaultMilestones: [] },
|
|
|
|
stubs: { Portal: true },
|
|
|
|
});
|
|
|
|
const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);
|
|
|
|
const suggestionsSegment = tokenSegments.at(2);
|
|
|
|
suggestionsSegment.vm.$emit('activate');
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
2020-12-25 09:10:31 +00:00
|
|
|
expect(wrapper.find(GlFilteredSearchSuggestion).exists()).toBe(false);
|
|
|
|
expect(wrapper.find(GlDropdownDivider).exists()).toBe(false);
|
2020-09-02 06:10:47 +00:00
|
|
|
});
|
|
|
|
|
2020-08-28 18:10:51 +00:00
|
|
|
it('renders `DEFAULT_MILESTONES` as default suggestions', async () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockMilestoneToken },
|
|
|
|
stubs: { Portal: true },
|
|
|
|
});
|
|
|
|
const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);
|
|
|
|
const suggestionsSegment = tokenSegments.at(2);
|
|
|
|
suggestionsSegment.vm.$emit('activate');
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
|
|
|
|
const suggestions = wrapper.findAll(GlFilteredSearchSuggestion);
|
|
|
|
|
|
|
|
expect(suggestions).toHaveLength(DEFAULT_MILESTONES.length);
|
|
|
|
DEFAULT_MILESTONES.forEach((milestone, index) => {
|
|
|
|
expect(suggestions.at(index).text()).toBe(milestone.text);
|
|
|
|
});
|
|
|
|
});
|
2020-08-12 15:10:02 +00:00
|
|
|
});
|
|
|
|
});
|