Commit bea95666 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'xanf/fix-milestone-combobox-spec' into 'master'

Fix milestone_combobox spec

See merge request gitlab-org/gitlab!50107
parents dfe55136 3d764e72
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import { mount, createLocalVue } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import axios from 'axios'; import axios from 'axios';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import { GlLoadingIcon, GlSearchBoxByType, GlDropdownItem } from '@gitlab/ui'; import { GlLoadingIcon, GlSearchBoxByType, GlDropdownItem } from '@gitlab/ui';
import { s__ } from '~/locale';
import { ENTER_KEY } from '~/lib/utils/keys'; import { ENTER_KEY } from '~/lib/utils/keys';
import MilestoneCombobox from '~/milestones/components/milestone_combobox.vue'; import MilestoneCombobox from '~/milestones/components/milestone_combobox.vue';
import { projectMilestones, groupMilestones } from './mock_data'; import { projectMilestones, groupMilestones } from './mock_data';
...@@ -14,8 +14,7 @@ const extraLinks = [ ...@@ -14,8 +14,7 @@ const extraLinks = [
{ text: 'Manage milestones', url: '/h5bp/html5-boilerplate/-/milestones' }, { text: 'Manage milestones', url: '/h5bp/html5-boilerplate/-/milestones' },
]; ];
const localVue = createLocalVue(); Vue.use(Vuex);
localVue.use(Vuex);
describe('Milestone combobox component', () => { describe('Milestone combobox component', () => {
const projectId = '8'; const projectId = '8';
...@@ -29,26 +28,31 @@ describe('Milestone combobox component', () => { ...@@ -29,26 +28,31 @@ describe('Milestone combobox component', () => {
let searchApiCallSpy; let searchApiCallSpy;
const createComponent = (props = {}, attrs = {}) => { const createComponent = (props = {}, attrs = {}) => {
const propsData = {
projectId,
groupId,
groupMilestonesAvailable,
extraLinks,
value: [],
...props,
};
wrapper = mount(MilestoneCombobox, { wrapper = mount(MilestoneCombobox, {
propsData: { propsData,
projectId,
groupId,
groupMilestonesAvailable,
extraLinks,
value: [],
...props,
},
attrs, attrs,
listeners: { listeners: {
// simulate a parent component v-model binding // simulate a parent component v-model binding
input: (selectedMilestone) => { input: (selectedMilestone) => {
// ugly hack because setProps plays bad with immediate watchers
// see https://github.com/vuejs/vue-test-utils/issues/1140 and
// https://github.com/vuejs/vue-test-utils/pull/1752
propsData.value = selectedMilestone;
wrapper.setProps({ value: selectedMilestone }); wrapper.setProps({ value: selectedMilestone });
}, },
}, },
stubs: { stubs: {
GlSearchBoxByType: true, GlSearchBoxByType: true,
}, },
localVue,
store: createStore(), store: createStore(),
}); });
}; };
...@@ -115,7 +119,7 @@ describe('Milestone combobox component', () => { ...@@ -115,7 +119,7 @@ describe('Milestone combobox component', () => {
return projectMilestoneSection return projectMilestoneSection
.text() .text()
.includes(s__('MilestoneCombobox|An error occurred while searching for milestones')); .includes('An error occurred while searching for milestones');
}; };
const groupMilestoneSectionContainsErrorMessage = () => { const groupMilestoneSectionContainsErrorMessage = () => {
...@@ -123,7 +127,7 @@ describe('Milestone combobox component', () => { ...@@ -123,7 +127,7 @@ describe('Milestone combobox component', () => {
return groupMilestoneSection return groupMilestoneSection
.text() .text()
.includes(s__('MilestoneCombobox|An error occurred while searching for milestones')); .includes('An error occurred while searching for milestones');
}; };
// //
...@@ -141,13 +145,13 @@ describe('Milestone combobox component', () => { ...@@ -141,13 +145,13 @@ describe('Milestone combobox component', () => {
findFirstGroupMilestonesDropdownItem().vm.$emit('click'); findFirstGroupMilestonesDropdownItem().vm.$emit('click');
}; };
const waitForRequests = ({ andClearMocks } = { andClearMocks: false }) => const waitForRequests = async ({ andClearMocks } = { andClearMocks: false }) => {
axios.waitForAll().then(() => { await axios.waitForAll();
if (andClearMocks) { if (andClearMocks) {
projectMilestonesApiCallSpy.mockClear(); projectMilestonesApiCallSpy.mockClear();
groupMilestonesApiCallSpy.mockClear(); groupMilestonesApiCallSpy.mockClear();
} }
}); };
describe('initialization behavior', () => { describe('initialization behavior', () => {
beforeEach(createComponent); beforeEach(createComponent);
...@@ -250,7 +254,7 @@ describe('Milestone combobox component', () => { ...@@ -250,7 +254,7 @@ describe('Milestone combobox component', () => {
describe('when the search query is empty', () => { describe('when the search query is empty', () => {
it('renders a "no results" message', () => { it('renders a "no results" message', () => {
expect(findNoResults().text()).toBe(s__('MilestoneCombobox|No matching results')); expect(findNoResults().text()).toBe('No matching results');
}); });
}); });
}); });
...@@ -333,19 +337,19 @@ describe('Milestone combobox component', () => { ...@@ -333,19 +337,19 @@ describe('Milestone combobox component', () => {
it('renders a checkmark by the selected item', async () => { it('renders a checkmark by the selected item', async () => {
selectFirstProjectMilestone(); selectFirstProjectMilestone();
await localVue.nextTick(); await nextTick();
expect( expect(
findFirstProjectMilestonesDropdownItem().find('span').classes('selected-item'), findFirstProjectMilestonesDropdownItem().find('span').classes('selected-item'),
).toBe(false); ).toBe(true);
selectFirstProjectMilestone(); selectFirstProjectMilestone();
await localVue.nextTick(); await nextTick();
expect( expect(
findFirstProjectMilestonesDropdownItem().find('span').classes('selected-item'), findFirstProjectMilestonesDropdownItem().find('span').classes('selected-item'),
).toBe(true); ).toBe(false);
}); });
describe('when a project milestones is selected', () => { describe('when a project milestones is selected', () => {
...@@ -360,22 +364,21 @@ describe('Milestone combobox component', () => { ...@@ -360,22 +364,21 @@ describe('Milestone combobox component', () => {
it("displays the project milestones name in the dropdown's button", async () => { it("displays the project milestones name in the dropdown's button", async () => {
selectFirstProjectMilestone(); selectFirstProjectMilestone();
await localVue.nextTick(); await nextTick();
expect(findButtonContent().text()).toBe(s__('MilestoneCombobox|No milestone')); expect(findButtonContent().text()).toBe('v1.0');
selectFirstProjectMilestone(); selectFirstProjectMilestone();
await nextTick();
await localVue.nextTick(); expect(findButtonContent().text()).toBe('No milestone');
expect(findButtonContent().text()).toBe('v1.0');
}); });
it('updates the v-model binding with the project milestone title', () => { it('updates the v-model binding with the project milestone title', async () => {
expect(wrapper.vm.value).toEqual([]);
selectFirstProjectMilestone(); selectFirstProjectMilestone();
await nextTick();
expect(wrapper.vm.value).toEqual(['v1.0']); expect(wrapper.emitted().input[0][0]).toStrictEqual(['v1.0']);
}); });
}); });
}); });
...@@ -459,18 +462,18 @@ describe('Milestone combobox component', () => { ...@@ -459,18 +462,18 @@ describe('Milestone combobox component', () => {
it('renders a checkmark by the selected item', async () => { it('renders a checkmark by the selected item', async () => {
selectFirstGroupMilestone(); selectFirstGroupMilestone();
await localVue.nextTick(); await nextTick();
expect(findFirstGroupMilestonesDropdownItem().find('span').classes('selected-item')).toBe( expect(findFirstGroupMilestonesDropdownItem().find('span').classes('selected-item')).toBe(
false, true,
); );
selectFirstGroupMilestone(); selectFirstGroupMilestone();
await localVue.nextTick(); await nextTick();
expect(findFirstGroupMilestonesDropdownItem().find('span').classes('selected-item')).toBe( expect(findFirstGroupMilestonesDropdownItem().find('span').classes('selected-item')).toBe(
true, false,
); );
}); });
...@@ -486,22 +489,21 @@ describe('Milestone combobox component', () => { ...@@ -486,22 +489,21 @@ describe('Milestone combobox component', () => {
it("displays the group milestones name in the dropdown's button", async () => { it("displays the group milestones name in the dropdown's button", async () => {
selectFirstGroupMilestone(); selectFirstGroupMilestone();
await localVue.nextTick(); await nextTick();
expect(findButtonContent().text()).toBe(s__('MilestoneCombobox|No milestone')); expect(findButtonContent().text()).toBe('group-v1.0');
selectFirstGroupMilestone(); selectFirstGroupMilestone();
await nextTick();
await localVue.nextTick(); expect(findButtonContent().text()).toBe('No milestone');
expect(findButtonContent().text()).toBe('group-v1.0');
}); });
it('updates the v-model binding with the group milestone title', () => { it('updates the v-model binding with the group milestone title', async () => {
expect(wrapper.vm.value).toEqual([]);
selectFirstGroupMilestone(); selectFirstGroupMilestone();
await nextTick();
expect(wrapper.vm.value).toEqual(['group-v1.0']); expect(wrapper.emitted().input[0][0]).toStrictEqual(['group-v1.0']);
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment