2020-05-24 23:13:21 +05:30
|
|
|
import { GlFilteredSearchToken, GlFilteredSearchSuggestion, GlLoadingIcon } from '@gitlab/ui';
|
2022-05-07 20:08:51 +05:30
|
|
|
import { nextTick } from 'vue';
|
2020-05-24 23:13:21 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2022-05-07 20:08:51 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-10-24 23:57:45 +05:30
|
|
|
import Api from '~/api';
|
2020-07-28 23:09:34 +05:30
|
|
|
import PipelineBranchNameToken from '~/pipelines/components/pipelines_list/tokens/pipeline_branch_name_token.vue';
|
2020-06-23 00:09:42 +05:30
|
|
|
import { branches, mockBranchesAfterMap } from '../mock_data';
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe('Pipeline Branch Name Token', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
const findFilteredSearchToken = () => wrapper.findComponent(GlFilteredSearchToken);
|
|
|
|
const findAllFilteredSearchSuggestions = () =>
|
|
|
|
wrapper.findAllComponents(GlFilteredSearchSuggestion);
|
|
|
|
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
|
2022-05-07 20:08:51 +05:30
|
|
|
const getBranchSuggestions = () =>
|
|
|
|
findAllFilteredSearchSuggestions().wrappers.map((w) => w.text());
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
const stubs = {
|
|
|
|
GlFilteredSearchToken: {
|
|
|
|
template: `<div><slot name="suggestions"></slot></div>`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
config: {
|
|
|
|
type: 'ref',
|
|
|
|
icon: 'branch',
|
|
|
|
title: 'Branch name',
|
|
|
|
unique: true,
|
|
|
|
projectId: '21',
|
2022-05-07 20:08:51 +05:30
|
|
|
defaultBranchName: null,
|
2020-06-23 00:09:42 +05:30
|
|
|
disabled: false,
|
2020-05-24 23:13:21 +05:30
|
|
|
},
|
|
|
|
value: {
|
|
|
|
data: '',
|
|
|
|
},
|
2022-07-16 23:28:13 +05:30
|
|
|
cursorPosition: 'start',
|
2020-05-24 23:13:21 +05:30
|
|
|
};
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
const optionsWithDefaultBranchName = (options) => {
|
|
|
|
return {
|
|
|
|
propsData: {
|
|
|
|
...defaultProps,
|
|
|
|
config: {
|
|
|
|
...defaultProps.config,
|
|
|
|
defaultBranchName: 'main',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
const createComponent = (options, data) => {
|
|
|
|
wrapper = shallowMount(PipelineBranchNameToken, {
|
|
|
|
propsData: {
|
|
|
|
...defaultProps,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-06-23 00:09:42 +05:30
|
|
|
jest.spyOn(Api, 'branches').mockResolvedValue({ data: branches });
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes config correctly', () => {
|
|
|
|
expect(findFilteredSearchToken().props('config')).toEqual(defaultProps.config);
|
|
|
|
});
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it('fetches and sets project branches', () => {
|
|
|
|
expect(Api.branches).toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(wrapper.vm.branches).toEqual(mockBranchesAfterMap);
|
|
|
|
expect(findLoadingIcon().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
describe('displays loading icon correctly', () => {
|
|
|
|
it('shows loading icon', () => {
|
|
|
|
createComponent({ stubs }, { loading: true });
|
|
|
|
|
|
|
|
expect(findLoadingIcon().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show loading icon', () => {
|
|
|
|
createComponent({ stubs }, { loading: false });
|
|
|
|
|
|
|
|
expect(findLoadingIcon().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('shows branches correctly', () => {
|
2020-06-23 00:09:42 +05:30
|
|
|
it('renders all branches', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
createComponent({ stubs }, { branches, loading: false });
|
|
|
|
|
|
|
|
expect(findAllFilteredSearchSuggestions()).toHaveLength(branches.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders only the branch searched for', () => {
|
2021-06-08 01:23:25 +05:30
|
|
|
const mockBranches = ['main'];
|
2020-05-24 23:13:21 +05:30
|
|
|
createComponent({ stubs }, { branches: mockBranches, loading: false });
|
|
|
|
|
|
|
|
expect(findAllFilteredSearchSuggestions()).toHaveLength(mockBranches.length);
|
|
|
|
});
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
it('shows the default branch first if no branch was searched for', async () => {
|
|
|
|
const mockBranches = [{ name: 'branch-1' }];
|
|
|
|
jest.spyOn(Api, 'branches').mockResolvedValue({ data: mockBranches });
|
|
|
|
|
|
|
|
createComponent(optionsWithDefaultBranchName({ stubs }), { loading: false });
|
|
|
|
await nextTick();
|
|
|
|
expect(getBranchSuggestions()).toEqual(['main', 'branch-1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show the default branch if a search term was provided', async () => {
|
|
|
|
const mockBranches = [{ name: 'branch-1' }];
|
|
|
|
jest.spyOn(Api, 'branches').mockResolvedValue({ data: mockBranches });
|
|
|
|
|
|
|
|
createComponent(optionsWithDefaultBranchName(), { loading: false });
|
|
|
|
|
|
|
|
findFilteredSearchToken().vm.$emit('input', { data: 'branch-1' });
|
|
|
|
await waitForPromises();
|
|
|
|
expect(getBranchSuggestions()).toEqual(['branch-1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows the default branch only once if it appears in the results', async () => {
|
|
|
|
const mockBranches = [{ name: 'main' }];
|
|
|
|
jest.spyOn(Api, 'branches').mockResolvedValue({ data: mockBranches });
|
|
|
|
|
|
|
|
createComponent(optionsWithDefaultBranchName({ stubs }), { loading: false });
|
|
|
|
await nextTick();
|
|
|
|
expect(getBranchSuggestions()).toEqual(['main']);
|
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
});
|