debian-mirror-gitlab/spec/frontend/releases/components/tag_field_new_spec.js

208 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
import { GlDropdownItem } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mount, shallowMount } from '@vue/test-utils';
2021-04-17 20:07:23 +05:30
import Vue from 'vue';
2020-10-24 23:57:45 +05:30
import TagFieldNew from '~/releases/components/tag_field_new.vue';
import createStore from '~/releases/stores';
2021-04-29 21:17:54 +05:30
import createEditNewModule from '~/releases/stores/modules/edit_new';
2020-10-24 23:57:45 +05:30
const TEST_TAG_NAME = 'test-tag-name';
const TEST_PROJECT_ID = '1234';
const TEST_CREATE_FROM = 'test-create-from';
2021-04-17 20:07:23 +05:30
const NONEXISTENT_TAG_NAME = 'nonexistent-tag';
2020-10-24 23:57:45 +05:30
describe('releases/components/tag_field_new', () => {
let store;
let wrapper;
2021-06-08 01:23:25 +05:30
let RefSelectorStub;
const createComponent = (
mountFn = shallowMount,
{ searchQuery } = { searchQuery: NONEXISTENT_TAG_NAME },
) => {
// A mock version of the RefSelector component that just renders the
// #footer slot, so that the content inside this slot can be tested.
RefSelectorStub = Vue.component('RefSelectorStub', {
data() {
return {
footerSlotProps: {
isLoading: false,
matches: {
tags: {
totalCount: 1,
list: [{ name: TEST_TAG_NAME }],
},
},
query: searchQuery,
},
};
},
template: '<div><slot name="footer" v-bind="footerSlotProps"></slot></div>',
});
2020-10-24 23:57:45 +05:30
wrapper = mountFn(TagFieldNew, {
store,
stubs: {
2021-04-17 20:07:23 +05:30
RefSelector: RefSelectorStub,
2020-10-24 23:57:45 +05:30
},
});
};
beforeEach(() => {
store = createStore({
modules: {
2021-04-29 21:17:54 +05:30
editNew: createEditNewModule({
2020-10-24 23:57:45 +05:30
projectId: TEST_PROJECT_ID,
}),
},
});
2021-04-29 21:17:54 +05:30
store.state.editNew.createFrom = TEST_CREATE_FROM;
2020-10-24 23:57:45 +05:30
2021-04-29 21:17:54 +05:30
store.state.editNew.release = {
2020-10-24 23:57:45 +05:30
tagName: TEST_TAG_NAME,
assets: {
links: [],
},
};
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
const findTagNameFormGroup = () => wrapper.find('[data-testid="tag-name-field"]');
2021-04-17 20:07:23 +05:30
const findTagNameDropdown = () => findTagNameFormGroup().find(RefSelectorStub);
2020-10-24 23:57:45 +05:30
const findCreateFromFormGroup = () => wrapper.find('[data-testid="create-from-field"]');
2021-04-17 20:07:23 +05:30
const findCreateFromDropdown = () => findCreateFromFormGroup().find(RefSelectorStub);
const findCreateNewTagOption = () => wrapper.find(GlDropdownItem);
2020-10-24 23:57:45 +05:30
describe('"Tag name" field', () => {
describe('rendering and behavior', () => {
beforeEach(() => createComponent());
it('renders a label', () => {
expect(findTagNameFormGroup().attributes().label).toBe('Tag name');
});
2021-04-17 20:07:23 +05:30
describe('when the user selects a new tag name', () => {
beforeEach(async () => {
findCreateNewTagOption().vm.$emit('click');
});
it("updates the store's release.tagName property", () => {
2021-04-29 21:17:54 +05:30
expect(store.state.editNew.release.tagName).toBe(NONEXISTENT_TAG_NAME);
2021-04-17 20:07:23 +05:30
});
it('hides the "Create from" field', () => {
expect(findCreateFromFormGroup().exists()).toBe(true);
});
});
describe('when the user selects an existing tag name', () => {
const updatedTagName = 'updated-tag-name';
beforeEach(async () => {
findTagNameDropdown().vm.$emit('input', updatedTagName);
});
2020-10-24 23:57:45 +05:30
it("updates the store's release.tagName property", () => {
2021-04-29 21:17:54 +05:30
expect(store.state.editNew.release.tagName).toBe(updatedTagName);
2021-04-17 20:07:23 +05:30
});
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
it('shows the "Create from" field', () => {
expect(findCreateFromFormGroup().exists()).toBe(false);
2020-10-24 23:57:45 +05:30
});
});
});
2021-06-08 01:23:25 +05:30
describe('"Create tag" option', () => {
describe('when the search query exactly matches one of the search results', () => {
beforeEach(async () => {
createComponent(mount, { searchQuery: TEST_TAG_NAME });
});
it('does not show the "Create tag" option', () => {
expect(findCreateNewTagOption().exists()).toBe(false);
});
});
describe('when the search query does not exactly match one of the search results', () => {
beforeEach(async () => {
createComponent(mount, { searchQuery: NONEXISTENT_TAG_NAME });
});
it('shows the "Create tag" option', () => {
expect(findCreateNewTagOption().exists()).toBe(true);
});
});
});
2020-10-24 23:57:45 +05:30
describe('validation', () => {
beforeEach(() => {
createComponent(mount);
});
/**
* Utility function to test the visibility of the validation message
* @param {'shown' | 'hidden'} state The expected state of the validation message.
* Should be passed either 'shown' or 'hidden'
*/
2021-04-17 20:07:23 +05:30
const expectValidationMessageToBe = async (state) => {
await wrapper.vm.$nextTick();
expect(findTagNameFormGroup().element).toHaveClass(
state === 'shown' ? 'is-invalid' : 'is-valid',
);
expect(findTagNameFormGroup().element).not.toHaveClass(
state === 'shown' ? 'is-valid' : 'is-invalid',
);
2020-10-24 23:57:45 +05:30
};
describe('when the user has not yet interacted with the component', () => {
2021-04-17 20:07:23 +05:30
it('does not display a validation error', async () => {
findTagNameDropdown().vm.$emit('input', '');
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
await expectValidationMessageToBe('hidden');
2020-10-24 23:57:45 +05:30
});
});
describe('when the user has interacted with the component and the value is not empty', () => {
2021-04-17 20:07:23 +05:30
it('does not display validation error', async () => {
findTagNameDropdown().vm.$emit('hide');
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
await expectValidationMessageToBe('hidden');
2020-10-24 23:57:45 +05:30
});
});
describe('when the user has interacted with the component and the value is empty', () => {
2021-04-17 20:07:23 +05:30
it('displays a validation error', async () => {
findTagNameDropdown().vm.$emit('input', '');
findTagNameDropdown().vm.$emit('hide');
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
await expectValidationMessageToBe('shown');
2020-10-24 23:57:45 +05:30
});
});
});
});
describe('"Create from" field', () => {
beforeEach(() => createComponent());
it('renders a label', () => {
expect(findCreateFromFormGroup().attributes().label).toBe('Create from');
});
describe('when the user selects a git ref', () => {
2021-04-17 20:07:23 +05:30
it("updates the store's createFrom property", async () => {
2020-10-24 23:57:45 +05:30
const updatedCreateFrom = 'update-create-from';
findCreateFromDropdown().vm.$emit('input', updatedCreateFrom);
2021-04-29 21:17:54 +05:30
expect(store.state.editNew.createFrom).toBe(updatedCreateFrom);
2020-10-24 23:57:45 +05:30
});
});
});
});