2023-04-23 21:23:45 +05:30
|
|
|
import { GlAvatarLabeled, GlCollapsibleListbox, GlListboxItem } from '@gitlab/ui';
|
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Vue from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
2022-10-11 01:57:18 +05:30
|
|
|
import TopicSelect from '~/admin/topics/components/topic_select.vue';
|
2023-04-23 21:23:45 +05:30
|
|
|
import searchProjectTopics from '~/graphql_shared/queries/project_topics_search.query.graphql';
|
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
const mockTopics = [
|
2023-04-23 21:23:45 +05:30
|
|
|
{
|
|
|
|
id: 'gid://gitlab/Projects::Topic/6',
|
|
|
|
name: 'topic1',
|
|
|
|
title: 'Topic 1',
|
|
|
|
avatarUrl: 'avatar.com/topic1.png',
|
|
|
|
__typename: 'Topic',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'gid://gitlab/Projects::Topic/5',
|
|
|
|
name: 'gitlab',
|
|
|
|
title: 'GitLab',
|
|
|
|
avatarUrl: 'avatar.com/GitLab.png',
|
|
|
|
__typename: 'Topic',
|
|
|
|
},
|
2022-10-11 01:57:18 +05:30
|
|
|
];
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
const mockTopicsQueryResponse = {
|
|
|
|
data: {
|
|
|
|
topics: {
|
|
|
|
nodes: mockTopics,
|
|
|
|
__typename: 'TopicConnection',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe('TopicSelect', () => {
|
|
|
|
let wrapper;
|
2023-04-23 21:23:45 +05:30
|
|
|
const mockSearchTopicsSuccess = jest.fn().mockResolvedValue(mockTopicsQueryResponse);
|
|
|
|
|
|
|
|
const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
|
|
|
|
const findAllListboxItems = () => wrapper.findAllComponents(GlListboxItem);
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
function createMockApolloProvider({ mockSearchTopicsQuery = mockSearchTopicsSuccess } = {}) {
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
|
|
|
return createMockApollo([[searchProjectTopics, mockSearchTopicsQuery]]);
|
|
|
|
}
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
function createComponent({ props = {}, mockApollo } = {}) {
|
|
|
|
wrapper = mount(TopicSelect, {
|
|
|
|
apolloProvider: mockApollo || createMockApolloProvider(),
|
2022-10-11 01:57:18 +05:30
|
|
|
propsData: props,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
topics: mockTopics,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
2023-04-23 21:23:45 +05:30
|
|
|
jest.clearAllMocks();
|
2022-10-11 01:57:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('mounts', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(wrapper.exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('`selectedTopic` prop defaults to `{}`', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(wrapper.props('selectedTopic')).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('`labelText` prop defaults to `null`', () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(wrapper.props('labelText')).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders default text if no selected topic', () => {
|
|
|
|
createComponent();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(findListbox().props('toggleText')).toBe('Select a topic');
|
2022-10-11 01:57:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders selected topic', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
const mockTopic = mockTopics[0];
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
createComponent({
|
|
|
|
props: {
|
|
|
|
selectedTopic: mockTopic,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(findListbox().props('toggleText')).toBe(mockTopic.name);
|
2022-10-11 01:57:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders label', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
createComponent({
|
|
|
|
props: {
|
|
|
|
labelText: 'my label',
|
|
|
|
},
|
|
|
|
});
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
expect(wrapper.find('label').text()).toBe('my label');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders dropdown items', () => {
|
|
|
|
createComponent();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
const listboxItems = findAllListboxItems();
|
|
|
|
|
|
|
|
expect(listboxItems.at(0).findComponent(GlAvatarLabeled).props('label')).toBe('Topic 1');
|
|
|
|
expect(listboxItems.at(1).findComponent(GlAvatarLabeled).props('label')).toBe('GitLab');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dropdown `toggledAriaLabelledBy` prop is not set if `labelText` prop is null', () => {
|
|
|
|
createComponent();
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(findListbox().props('toggle-aria-labelled-by')).toBe(undefined);
|
2022-10-11 01:57:18 +05:30
|
|
|
});
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it('emits `click` event when topic selected', async () => {
|
2022-10-11 01:57:18 +05:30
|
|
|
createComponent();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
await findAllListboxItems().at(0).trigger('click');
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
expect(wrapper.emitted('click')).toEqual([[mockTopics[0]]]);
|
|
|
|
});
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
describe('when searching a topic', () => {
|
|
|
|
const searchTopic = (searchTerm) => findListbox().vm.$emit('search', searchTerm);
|
|
|
|
const mockSearchTerm = 'gitl';
|
|
|
|
|
|
|
|
it('toggles loading state', async () => {
|
|
|
|
createComponent();
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
await searchTopic(mockSearchTerm);
|
|
|
|
|
|
|
|
expect(findListbox().props('searching')).toBe(true);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findListbox().props('searching')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches topics matching search string', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await searchTopic(mockSearchTerm);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
expect(mockSearchTopicsSuccess).toHaveBeenCalledWith({
|
|
|
|
search: mockSearchTerm,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-11 01:57:18 +05:30
|
|
|
});
|