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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import { stubPerformanceWebAPI } from 'helpers/performance';
import { __ } from '~/locale';
import CannotPushCodeAlert from '~/ide/components/cannot_push_code_alert.vue';
import ErrorMessage from '~/ide/components/error_message.vue';
import Ide from '~/ide/components/ide.vue';
import eventHub from '~/ide/eventhub';
import { MSG_CANNOT_PUSH_CODE_GO_TO_FORK, MSG_GO_TO_FORK } from '~/ide/messages';
import { createStore } from '~/ide/stores';
import { file } from '../helpers';
import { projectData } from '../mock_data';
Vue.use(Vuex);
const TEST_FORK_IDE_PATH = '/test/ide/path';
const MSG_ARE_YOU_SURE = __('Are you sure you want to lose unsaved changes?');
describe('WebIDE', () => {
const emptyProjData = { ...projectData, empty_repo: true, branches: {} };
let store;
let wrapper;
const createComponent = ({ projData = emptyProjData, state = {} } = {}) => {
store.state.currentProjectId = 'abcproject';
store.state.currentBranchId = 'main';
store.state.projects.abcproject = projData && { ...projData };
store.state.trees['abcproject/main'] = {
tree: [],
loading: false,
};
Object.keys(state).forEach((key) => {
store.state[key] = state[key];
});
wrapper = shallowMount(Ide, {
store,
});
};
const findAlert = () => wrapper.findComponent(CannotPushCodeAlert);
const callOnBeforeUnload = (e = {}) => window.onbeforeunload(e);
beforeEach(() => {
stubPerformanceWebAPI();
store = createStore();
});
afterEach(() => {
window.onbeforeunload = null;
});
describe('ide component, empty repo', () => {
beforeEach(() => {
createComponent({
projData: {
empty_repo: true,
},
});
});
it('renders "New file" button in empty repo', async () => {
expect(wrapper.find('[title="New file"]').exists()).toBe(true);
});
});
describe('ide component, non-empty repo', () => {
describe('error message', () => {
it.each`
errorMessage | exists
${null} | ${false}
${{ text: 'error' }} | ${true}
`(
'should error message exists=$exists when errorMessage=$errorMessage',
async ({ errorMessage, exists }) => {
createComponent({
state: {
errorMessage,
},
});
await waitForPromises();
expect(wrapper.findComponent(ErrorMessage).exists()).toBe(exists);
},
);
});
describe('onBeforeUnload', () => {
it('returns undefined when no staged files or changed files', () => {
createComponent();
expect(callOnBeforeUnload()).toBe(undefined);
});
it('returns warning text when their are changed files', () => {
createComponent({
state: {
changedFiles: [file()],
},
});
const e = {};
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
});
it('returns warning text when their are staged files', () => {
createComponent({
state: {
stagedFiles: [file()],
},
});
const e = {};
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
});
it('returns undefined once after "skip-beforeunload" was emitted', () => {
createComponent({
state: {
stagedFiles: [file()],
},
});
eventHub.$emit('skip-beforeunload');
const e = {};
expect(callOnBeforeUnload()).toBe(undefined);
expect(e.returnValue).toBe(undefined);
expect(callOnBeforeUnload(e)).toBe(MSG_ARE_YOU_SURE);
expect(e.returnValue).toBe(MSG_ARE_YOU_SURE);
});
});
describe('non-existent branch', () => {
it('does not render "New file" button for non-existent branch when repo is not empty', () => {
createComponent({
state: {
projects: {},
},
});
expect(wrapper.find('[title="New file"]').exists()).toBe(false);
});
});
describe('branch with files', () => {
beforeEach(() => {
createComponent({
projData: {
empty_repo: false,
},
});
});
it('does not render "New file" button', () => {
expect(wrapper.find('[title="New file"]').exists()).toBe(false);
});
});
});
it('when user cannot push code, shows alert', () => {
store.state.links = {
forkInfo: {
ide_path: TEST_FORK_IDE_PATH,
},
};
createComponent({
projData: {
userPermissions: {
pushCode: false,
},
},
});
expect(findAlert().props()).toMatchObject({
message: MSG_CANNOT_PUSH_CODE_GO_TO_FORK,
action: {
href: TEST_FORK_IDE_PATH,
text: MSG_GO_TO_FORK,
},
});
});
it.each`
desc | projData
${'when user can push code'} | ${{ userPermissions: { pushCode: true } }}
${'when project is not ready'} | ${null}
`('$desc, no alert is shown', ({ projData }) => {
createComponent({
projData,
});
expect(findAlert().exists()).toBe(false);
});
});
|