2023-04-23 21:23:45 +05:30
|
|
|
import { GlAvatarLabeled, GlCollapsibleListbox } from '@gitlab/ui';
|
2021-11-11 11:23:49 +05:30
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import * as projectsApi from '~/api/projects_api';
|
|
|
|
import ProjectSelect from '~/invite_members/components/project_select.vue';
|
|
|
|
import { allProjects, project1 } from '../mock_data/api_response_data';
|
|
|
|
|
|
|
|
describe('ProjectSelect', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
const createComponent = () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
wrapper = shallowMountExtended(ProjectSelect, {
|
|
|
|
stubs: {
|
|
|
|
GlCollapsibleListbox,
|
|
|
|
GlAvatarLabeled,
|
|
|
|
},
|
|
|
|
});
|
2021-11-11 11:23:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(projectsApi, 'getProjects').mockResolvedValue(allProjects);
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
const findGlCollapsibleListbox = () => wrapper.findComponent(GlCollapsibleListbox);
|
|
|
|
const findAvatarLabeled = (index) => wrapper.findAllComponents(GlAvatarLabeled).at(index);
|
|
|
|
|
|
|
|
it('renders GlCollapsibleListbox with default props', () => {
|
|
|
|
expect(findGlCollapsibleListbox().exists()).toBe(true);
|
|
|
|
expect(findGlCollapsibleListbox().props()).toMatchObject({
|
|
|
|
items: [],
|
|
|
|
loading: false,
|
|
|
|
multiple: false,
|
|
|
|
noResultsText: 'No matching results',
|
|
|
|
placement: 'left',
|
|
|
|
searchPlaceholder: 'Search projects',
|
|
|
|
searchable: true,
|
|
|
|
searching: false,
|
|
|
|
size: 'medium',
|
|
|
|
toggleText: 'Select a project',
|
|
|
|
totalItems: null,
|
|
|
|
variant: 'default',
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when user types in the search input', () => {
|
|
|
|
let resolveApiRequest;
|
|
|
|
let rejectApiRequest;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(projectsApi, 'getProjects').mockImplementation(
|
|
|
|
() =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
resolveApiRequest = resolve;
|
|
|
|
rejectApiRequest = reject;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
findGlCollapsibleListbox().vm.$emit('search', project1.name);
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the API', () => {
|
|
|
|
resolveApiRequest({ data: allProjects });
|
|
|
|
|
|
|
|
expect(projectsApi.getProjects).toHaveBeenCalledWith(project1.name, {
|
|
|
|
active: true,
|
|
|
|
exclude_internal: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays loading icon while waiting for API call to resolve and then sets loading false', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(findGlCollapsibleListbox().props('searching')).toBe(true);
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
resolveApiRequest({ data: allProjects });
|
|
|
|
await waitForPromises();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(findGlCollapsibleListbox().props('searching')).toBe(false);
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('displays a dropdown item and avatar for each project fetched', async () => {
|
|
|
|
resolveApiRequest({ data: allProjects });
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
allProjects.forEach((project, index) => {
|
|
|
|
expect(findAvatarLabeled(index).attributes()).toMatchObject({
|
|
|
|
src: project.avatar_url,
|
|
|
|
'entity-id': String(project.id),
|
|
|
|
'entity-name': project.name_with_namespace,
|
2023-04-23 21:23:45 +05:30
|
|
|
size: '32',
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
expect(findAvatarLabeled(index).props('label')).toBe(project.name_with_namespace);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the empty message when the API results are empty', async () => {
|
|
|
|
resolveApiRequest({ data: [] });
|
|
|
|
await waitForPromises();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(findGlCollapsibleListbox().text()).toBe('No matching results');
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the error message when the fetch fails', async () => {
|
|
|
|
rejectApiRequest();
|
|
|
|
await waitForPromises();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
// To be displayed in GlCollapsibleListbox once we implement
|
|
|
|
// https://gitlab.com/gitlab-org/gitlab-ui/-/issues/2132
|
|
|
|
// https://gitlab.com/gitlab-org/gitlab/-/issues/389974
|
|
|
|
expect(findGlCollapsibleListbox().text()).toBe('No matching results');
|
2021-11-11 11:23:49 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|