2020-09-02 06:10:47 +00:00
|
|
|
import {
|
|
|
|
GlFilteredSearchToken,
|
|
|
|
GlFilteredSearchTokenSegment,
|
|
|
|
GlFilteredSearchSuggestion,
|
2020-09-14 18:09:48 +00:00
|
|
|
GlDropdownDivider,
|
2020-09-02 06:10:47 +00:00
|
|
|
} from '@gitlab/ui';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-06-02 15:08:24 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { deprecatedCreateFlash as createFlash } from '~/flash';
|
2020-06-02 15:08:24 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
2020-09-02 06:10:47 +00:00
|
|
|
import {
|
|
|
|
DEFAULT_LABEL_NONE,
|
|
|
|
DEFAULT_LABEL_ANY,
|
|
|
|
} from '~/vue_shared/components/filtered_search_bar/constants';
|
2020-06-02 15:08:24 +00:00
|
|
|
import AuthorToken from '~/vue_shared/components/filtered_search_bar/tokens/author_token.vue';
|
|
|
|
|
|
|
|
import { mockAuthorToken, mockAuthors } from '../mock_data';
|
|
|
|
|
|
|
|
jest.mock('~/flash');
|
2020-09-02 06:10:47 +00:00
|
|
|
const defaultStubs = {
|
|
|
|
Portal: true,
|
|
|
|
GlFilteredSearchSuggestionList: {
|
|
|
|
template: '<div></div>',
|
|
|
|
methods: {
|
|
|
|
getValue: () => '=',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function createComponent(options = {}) {
|
|
|
|
const {
|
|
|
|
config = mockAuthorToken,
|
|
|
|
value = { data: '' },
|
|
|
|
active = false,
|
|
|
|
stubs = defaultStubs,
|
|
|
|
} = options;
|
|
|
|
return mount(AuthorToken, {
|
2020-06-02 15:08:24 +00:00
|
|
|
propsData: {
|
|
|
|
config,
|
|
|
|
value,
|
2020-07-14 12:09:14 +00:00
|
|
|
active,
|
2020-06-02 15:08:24 +00:00
|
|
|
},
|
|
|
|
provide: {
|
|
|
|
portalName: 'fake target',
|
|
|
|
alignSuggestions: function fakeAlignSuggestions() {},
|
2020-11-13 12:09:03 +00:00
|
|
|
suggestionsListClass: 'custom-class',
|
2020-06-02 15:08:24 +00:00
|
|
|
},
|
2020-09-02 06:10:47 +00:00
|
|
|
stubs,
|
2020-06-02 15:08:24 +00:00
|
|
|
});
|
2020-09-02 06:10:47 +00:00
|
|
|
}
|
2020-06-02 15:08:24 +00:00
|
|
|
|
|
|
|
describe('AuthorToken', () => {
|
|
|
|
let mock;
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('computed', () => {
|
|
|
|
describe('currentValue', () => {
|
|
|
|
it('returns lowercase string for `value.data`', () => {
|
2020-07-14 12:09:14 +00:00
|
|
|
wrapper = createComponent({ value: { data: 'FOO' } });
|
2020-06-02 15:08:24 +00:00
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
expect(wrapper.vm.currentValue).toBe('foo');
|
2020-06-02 15:08:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('activeAuthor', () => {
|
2020-07-14 12:09:14 +00:00
|
|
|
it('returns object for currently present `value.data`', async () => {
|
|
|
|
wrapper = createComponent({ value: { data: mockAuthors[0].username } });
|
|
|
|
|
2020-06-02 15:08:24 +00:00
|
|
|
wrapper.setData({
|
|
|
|
authors: mockAuthors,
|
|
|
|
});
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
await wrapper.vm.$nextTick();
|
2020-06-02 15:08:24 +00:00
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
expect(wrapper.vm.activeAuthor).toEqual(mockAuthors[0]);
|
2020-06-02 15:08:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchAuthorBySearchTerm', () => {
|
|
|
|
it('calls `config.fetchAuthors` with provided searchTerm param', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchAuthors');
|
|
|
|
|
|
|
|
wrapper.vm.fetchAuthorBySearchTerm(mockAuthors[0].username);
|
|
|
|
|
|
|
|
expect(wrapper.vm.config.fetchAuthors).toHaveBeenCalledWith(
|
|
|
|
mockAuthorToken.fetchPath,
|
|
|
|
mockAuthors[0].username,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets response to `authors` when request is succesful', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchAuthors').mockResolvedValue(mockAuthors);
|
|
|
|
|
|
|
|
wrapper.vm.fetchAuthorBySearchTerm('root');
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(wrapper.vm.authors).toEqual(mockAuthors);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls `createFlash` with flash error message when request fails', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchAuthors').mockRejectedValue({});
|
|
|
|
|
|
|
|
wrapper.vm.fetchAuthorBySearchTerm('root');
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(createFlash).toHaveBeenCalledWith('There was a problem fetching users.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets `loading` to false when request completes', () => {
|
|
|
|
jest.spyOn(wrapper.vm.config, 'fetchAuthors').mockRejectedValue({});
|
|
|
|
|
|
|
|
wrapper.vm.fetchAuthorBySearchTerm('root');
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
expect(wrapper.vm.loading).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('template', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper.setData({
|
|
|
|
authors: mockAuthors,
|
|
|
|
});
|
|
|
|
|
|
|
|
return 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', () => {
|
|
|
|
wrapper.setProps({
|
|
|
|
value: { data: mockAuthors[0].username },
|
|
|
|
});
|
|
|
|
|
|
|
|
return wrapper.vm.$nextTick(() => {
|
|
|
|
const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);
|
|
|
|
|
|
|
|
expect(tokenSegments).toHaveLength(3); // Author, =, "Administrator"
|
|
|
|
expect(tokenSegments.at(2).text()).toBe(mockAuthors[0].name); // "Administrator"
|
|
|
|
});
|
|
|
|
});
|
2020-09-02 06:10:47 +00:00
|
|
|
|
|
|
|
it('renders provided defaultAuthors as suggestions', async () => {
|
|
|
|
const defaultAuthors = [DEFAULT_LABEL_NONE, DEFAULT_LABEL_ANY];
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockAuthorToken, defaultAuthors },
|
|
|
|
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(defaultAuthors.length);
|
|
|
|
defaultAuthors.forEach((label, index) => {
|
|
|
|
expect(suggestions.at(index).text()).toBe(label.text);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not render divider when no defaultAuthors', async () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockAuthorToken, defaultAuthors: [] },
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it('renders `DEFAULT_LABEL_ANY` as default suggestions', async () => {
|
|
|
|
wrapper = createComponent({
|
|
|
|
active: true,
|
|
|
|
config: { ...mockAuthorToken },
|
|
|
|
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(1);
|
|
|
|
expect(suggestions.at(0).text()).toBe(DEFAULT_LABEL_ANY.text);
|
|
|
|
});
|
2020-06-02 15:08:24 +00:00
|
|
|
});
|
|
|
|
});
|