debian-mirror-gitlab/spec/frontend/projects/compare/components/revision_dropdown_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

178 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2023-05-27 22:25:52 +05:30
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status';
2021-03-11 19:13:27 +05:30
import RevisionDropdown from '~/projects/compare/components/revision_dropdown.vue';
2021-06-08 01:23:25 +05:30
import { revisionDropdownDefaultProps as defaultProps } from './mock_data';
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2021-03-11 19:13:27 +05:30
describe('RevisionDropdown component', () => {
let wrapper;
let axiosMock;
const createComponent = (props = {}) => {
wrapper = shallowMount(RevisionDropdown, {
propsData: {
...defaultProps,
...props,
},
2021-04-29 21:17:54 +05:30
stubs: {
GlDropdown,
GlSearchBoxByType,
},
2021-03-11 19:13:27 +05:30
});
};
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
});
2022-10-11 01:57:18 +05:30
const findGlDropdown = () => wrapper.findComponent(GlDropdown);
const findSearchBox = () => wrapper.findComponent(GlSearchBoxByType);
2023-05-27 22:25:52 +05:30
const findBranchesDropdownItem = () =>
wrapper.findAllComponents('[data-testid="branches-dropdown-item"]');
const findTagsDropdownItem = () =>
wrapper.findAllComponents('[data-testid="tags-dropdown-item"]');
2021-03-11 19:13:27 +05:30
it('sets hidden input', () => {
createComponent();
expect(wrapper.find('input[type="hidden"]').attributes('value')).toBe(
defaultProps.paramsBranch,
);
});
it('update the branches on success', async () => {
const Branches = ['branch-1', 'branch-2'];
const Tags = ['tag-1', 'tag-2', 'tag-3'];
2023-04-23 21:23:45 +05:30
axiosMock.onGet(defaultProps.refsProjectPath).replyOnce(HTTP_STATUS_OK, {
2021-03-11 19:13:27 +05:30
Branches,
Tags,
});
createComponent();
2023-05-27 22:25:52 +05:30
expect(findBranchesDropdownItem()).toHaveLength(0);
expect(findTagsDropdownItem()).toHaveLength(0);
await waitForPromises();
expect(findBranchesDropdownItem()).toHaveLength(Branches.length);
expect(findTagsDropdownItem()).toHaveLength(Tags.length);
Branches.forEach((branch, index) => {
expect(findBranchesDropdownItem().at(index).text()).toBe(branch);
});
Tags.forEach((tag, index) => {
expect(findTagsDropdownItem().at(index).text()).toBe(tag);
});
2021-03-11 19:13:27 +05:30
});
2023-06-20 00:43:36 +05:30
it('shows an alert on error', async () => {
2023-04-23 21:23:45 +05:30
axiosMock.onGet('some/invalid/path').replyOnce(HTTP_STATUS_NOT_FOUND);
2021-03-11 19:13:27 +05:30
createComponent();
2023-05-27 22:25:52 +05:30
await waitForPromises();
2021-03-11 19:13:27 +05:30
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalled();
2021-03-11 19:13:27 +05:30
});
2021-04-17 20:07:23 +05:30
it('makes a new request when refsProjectPath is changed', async () => {
jest.spyOn(axios, 'get');
const newRefsProjectPath = 'new-selected-project-path';
createComponent();
wrapper.setProps({
...defaultProps,
refsProjectPath: newRefsProjectPath,
});
2023-05-27 22:25:52 +05:30
await waitForPromises();
2021-04-17 20:07:23 +05:30
expect(axios.get).toHaveBeenLastCalledWith(newRefsProjectPath);
});
2021-04-29 21:17:54 +05:30
describe('search', () => {
2023-06-20 00:43:36 +05:30
it('shows alert on error', async () => {
2023-04-23 21:23:45 +05:30
axiosMock.onGet('some/invalid/path').replyOnce(HTTP_STATUS_NOT_FOUND);
2021-04-29 21:17:54 +05:30
createComponent();
2023-05-27 22:25:52 +05:30
await waitForPromises();
2021-04-29 21:17:54 +05:30
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalled();
2021-04-29 21:17:54 +05:30
});
it('makes request with search param', async () => {
jest.spyOn(axios, 'get').mockResolvedValue({
data: {
Branches: [],
Tags: [],
},
});
const mockSearchTerm = 'foobar';
createComponent();
findSearchBox().vm.$emit('input', mockSearchTerm);
2023-05-27 22:25:52 +05:30
await waitForPromises();
2021-04-29 21:17:54 +05:30
expect(axios.get).toHaveBeenCalledWith(
defaultProps.refsProjectPath,
expect.objectContaining({
params: {
search: mockSearchTerm,
},
}),
);
});
});
2021-03-11 19:13:27 +05:30
describe('GlDropdown component', () => {
it('renders props', () => {
createComponent();
expect(wrapper.props()).toEqual(expect.objectContaining(defaultProps));
});
it('display default text', () => {
createComponent({
paramsBranch: null,
});
expect(findGlDropdown().props('text')).toBe('Select branch/tag');
});
it('display params branch text', () => {
createComponent();
expect(findGlDropdown().props('text')).toBe(defaultProps.paramsBranch);
});
});
2021-06-08 01:23:25 +05:30
it('emits `selectRevision` event when another revision is selected', async () => {
2023-05-27 22:25:52 +05:30
jest.spyOn(axios, 'get').mockResolvedValue({
data: {
Branches: ['some-branch'],
Tags: [],
},
});
2021-06-08 01:23:25 +05:30
createComponent();
2022-04-04 11:22:00 +05:30
await nextTick();
2021-06-08 01:23:25 +05:30
2022-10-11 01:57:18 +05:30
findGlDropdown().findAllComponents(GlDropdownItem).at(0).vm.$emit('click');
2021-06-08 01:23:25 +05:30
expect(wrapper.emitted('selectRevision')[0][0]).toEqual({
direction: 'to',
revision: 'some-branch',
});
});
2021-03-11 19:13:27 +05:30
});