1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
import { GlModal, GlForm, GlFormCheckbox, GlSprintf } from '@gitlab/ui';
import { within } from '@testing-library/dom';
import { shallowMount, mount, createWrapper } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import api from '~/api';
import axios from '~/lib/utils/axios_utils';
import { BV_SHOW_MODAL } from '~/lib/utils/constants';
import BranchesDropdown from '~/projects/commit/components/branches_dropdown.vue';
import CommitFormModal from '~/projects/commit/components/form_modal.vue';
import ProjectsDropdown from '~/projects/commit/components/projects_dropdown.vue';
import eventHub from '~/projects/commit/event_hub';
import createStore from '~/projects/commit/store';
import mockData from '../mock_data';
jest.mock('~/api');
describe('CommitFormModal', () => {
let wrapper;
let store;
let axiosMock;
const createComponent = (method, state = {}, provide = {}, propsData = {}) => {
store = createStore({ ...mockData.mockModal, ...state });
wrapper = extendedWrapper(
method(CommitFormModal, {
provide: {
...provide,
},
propsData: { ...mockData.modalPropsData, ...propsData },
store,
attrs: {
static: true,
visible: true,
},
}),
);
};
const findModal = () => wrapper.findComponent(GlModal);
const findStartBranch = () => wrapper.find('#start_branch');
const findTargetProject = () => wrapper.find('#target_project_id');
const findBranchesDropdown = () => wrapper.findComponent(BranchesDropdown);
const findProjectsDropdown = () => wrapper.findComponent(ProjectsDropdown);
const findForm = () => findModal().findComponent(GlForm);
const findCheckBox = () => findForm().findComponent(GlFormCheckbox);
const findPrependedText = () => wrapper.findByTestId('prepended-text');
const findAppendedText = () => wrapper.findByTestId('appended-text');
const getByText = (text, options) =>
createWrapper(within(findModal().element).getByText(text, options));
beforeEach(() => {
axiosMock = new MockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
});
describe('Basic interactions', () => {
it('Listens for opening of modal on mount', () => {
jest.spyOn(eventHub, '$on');
createComponent(shallowMount);
expect(eventHub.$on).toHaveBeenCalledWith(mockData.modalPropsData.openModal, wrapper.vm.show);
});
it('Shows modal', () => {
createComponent(shallowMount);
const rootEmit = jest.spyOn(wrapper.vm.$root, '$emit');
wrapper.vm.show();
expect(rootEmit).toHaveBeenCalledWith(BV_SHOW_MODAL, mockData.modalPropsData.modalId);
});
it('Clears the modal state once modal is hidden', () => {
createComponent(shallowMount);
jest.spyOn(store, 'dispatch').mockImplementation();
wrapper.vm.checked = false;
findModal().vm.$emit('hidden');
expect(store.dispatch).toHaveBeenCalledWith('clearModal');
expect(store.dispatch).toHaveBeenCalledWith('setSelectedBranch', '');
expect(wrapper.vm.checked).toBe(true);
});
it('Shows the checkbox for new merge request', () => {
createComponent(shallowMount);
expect(findCheckBox().exists()).toBe(true);
});
it('Shows the prepended text', () => {
createComponent(shallowMount, {}, { prependedText: '_prepended_text_' });
expect(findPrependedText().exists()).toBe(true);
expect(findPrependedText().findComponent(GlSprintf).attributes('message')).toBe(
'_prepended_text_',
);
});
it('Does not show prepended text', () => {
createComponent(shallowMount);
expect(findPrependedText().exists()).toBe(false);
});
it('Does not show extra message text', () => {
createComponent(shallowMount);
expect(findModal().find('[data-testid="appended-text"]').exists()).toBe(false);
});
it('Does not show the checkbox for new merge request', () => {
createComponent(shallowMount, { pushCode: false });
expect(findCheckBox().exists()).toBe(false);
});
it('Shows the branch in fork message', () => {
createComponent(shallowMount, { pushCode: false });
expect(findAppendedText().exists()).toBe(true);
expect(findAppendedText().findComponent(GlSprintf).attributes('message')).toContain(
mockData.modalPropsData.i18n.branchInFork,
);
});
it('Shows the branch collaboration message', () => {
createComponent(shallowMount, { pushCode: false, branchCollaboration: true });
expect(findAppendedText().exists()).toBe(true);
expect(findAppendedText().findComponent(GlSprintf).attributes('message')).toContain(
mockData.modalPropsData.i18n.existingBranch,
);
});
});
describe('Taking action on the form', () => {
beforeEach(() => {
createComponent(mount);
});
it('Action primary button dispatches submit action', () => {
const submitSpy = jest.spyOn(findForm().element, 'submit');
getByText(mockData.modalPropsData.i18n.actionPrimaryText).trigger('click');
expect(submitSpy).toHaveBeenCalled();
submitSpy.mockRestore();
});
it('Changes the start_branch input value', async () => {
findBranchesDropdown().vm.$emit('input', '_changed_branch_value_');
await nextTick();
expect(findStartBranch().attributes('value')).toBe('_changed_branch_value_');
});
it('Changes the target_project_id input value', async () => {
createComponent(shallowMount, {}, {}, { isCherryPick: true });
findProjectsDropdown().vm.$emit('input', '_changed_project_value_');
await nextTick();
expect(findTargetProject().attributes('value')).toBe('_changed_project_value_');
});
});
it('action primary button triggers Redis HLL tracking api call', async () => {
createComponent(mount, {}, {}, { primaryActionEventName: 'test_event' });
await nextTick();
jest.spyOn(findForm().element, 'submit');
getByText(mockData.modalPropsData.i18n.actionPrimaryText).trigger('click');
expect(api.trackRedisHllUserEvent).toHaveBeenCalledWith('test_event');
});
});
|