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
208
209
|
import Vue, { nextTick } from 'vue';
import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import createFlash from '~/flash';
import modal from '~/ide/components/new_dropdown/modal.vue';
import { createStore } from '~/ide/stores';
jest.mock('~/flash');
describe('new file modal component', () => {
const Component = Vue.extend(modal);
let vm;
afterEach(() => {
vm.$destroy();
});
describe.each`
entryType | modalTitle | btnTitle | showsFileTemplates
${'tree'} | ${'Create new directory'} | ${'Create directory'} | ${false}
${'blob'} | ${'Create new file'} | ${'Create file'} | ${true}
`('$entryType', ({ entryType, modalTitle, btnTitle, showsFileTemplates }) => {
beforeEach(async () => {
const store = createStore();
vm = createComponentWithStore(Component, store).$mount();
vm.open(entryType);
vm.name = 'testing';
await nextTick();
});
afterEach(() => {
vm.close();
});
it(`sets modal title as ${entryType}`, () => {
expect(document.querySelector('.modal-title').textContent.trim()).toBe(modalTitle);
});
it(`sets button label as ${entryType}`, () => {
expect(document.querySelector('.btn-confirm').textContent.trim()).toBe(btnTitle);
});
it(`sets form label as ${entryType}`, () => {
expect(document.querySelector('.label-bold').textContent.trim()).toBe('Name');
});
it(`shows file templates: ${showsFileTemplates}`, () => {
const templateFilesEl = document.querySelector('.file-templates');
expect(Boolean(templateFilesEl)).toBe(showsFileTemplates);
});
});
describe('rename entry', () => {
beforeEach(() => {
const store = createStore();
store.state.entries = {
'test-path': {
name: 'test',
type: 'blob',
path: 'test-path',
},
};
vm = createComponentWithStore(Component, store).$mount();
});
it.each`
entryType | modalTitle | btnTitle
${'tree'} | ${'Rename folder'} | ${'Rename folder'}
${'blob'} | ${'Rename file'} | ${'Rename file'}
`(
'renders title and button for renaming $entryType',
async ({ entryType, modalTitle, btnTitle }) => {
vm.$store.state.entries['test-path'].type = entryType;
vm.open('rename', 'test-path');
await nextTick();
expect(document.querySelector('.modal-title').textContent.trim()).toBe(modalTitle);
expect(document.querySelector('.btn-confirm').textContent.trim()).toBe(btnTitle);
},
);
describe('entryName', () => {
it('returns entries name', () => {
vm.open('rename', 'test-path');
expect(vm.entryName).toBe('test-path');
});
it('does not reset entryName to its old value if empty', () => {
vm.entryName = 'hello';
vm.entryName = '';
expect(vm.entryName).toBe('');
});
});
describe('open', () => {
it('sets entryName to path provided if modalType is rename', () => {
vm.open('rename', 'test-path');
expect(vm.entryName).toBe('test-path');
});
it("appends '/' to the path if modalType isn't rename", () => {
vm.open('blob', 'test-path');
expect(vm.entryName).toBe('test-path/');
});
it('leaves entryName blank if no path is provided', () => {
vm.open('blob');
expect(vm.entryName).toBe('');
});
});
});
describe('createFromTemplate', () => {
let store;
beforeEach(() => {
store = createStore();
store.state.entries = {
'test-path/test': {
name: 'test',
deleted: false,
},
};
vm = createComponentWithStore(Component, store).$mount();
vm.open('blob');
jest.spyOn(vm, 'createTempEntry').mockImplementation();
});
it.each`
entryName | newFilePath
${''} | ${'.gitignore'}
${'README.md'} | ${'.gitignore'}
${'test-path/test/'} | ${'test-path/test/.gitignore'}
${'test-path/test'} | ${'test-path/.gitignore'}
${'test-path/test/abc.md'} | ${'test-path/test/.gitignore'}
`(
'creates a new file with the given template name in appropriate directory for path: $path',
({ entryName, newFilePath }) => {
vm.entryName = entryName;
vm.createFromTemplate({ name: '.gitignore' });
expect(vm.createTempEntry).toHaveBeenCalledWith({
name: newFilePath,
type: 'blob',
});
},
);
});
describe('submitForm', () => {
let store;
beforeEach(() => {
store = createStore();
store.state.entries = {
'test-path/test': {
name: 'test',
deleted: false,
},
};
vm = createComponentWithStore(Component, store).$mount();
});
it('throws an error when target entry exists', () => {
vm.open('rename', 'test-path/test');
expect(createFlash).not.toHaveBeenCalled();
vm.submitForm();
expect(createFlash).toHaveBeenCalledWith({
message: 'The name "test-path/test" is already taken in this directory.',
fadeTransition: false,
addBodyClass: true,
});
});
it('does not throw error when target entry does not exist', () => {
jest.spyOn(vm, 'renameEntry').mockImplementation();
vm.open('rename', 'test-path/test');
vm.entryName = 'test-path/test2';
vm.submitForm();
expect(createFlash).not.toHaveBeenCalled();
});
it('removes leading/trailing found in the new name', () => {
vm.open('rename', 'test-path/test');
vm.entryName = 'test-path /test';
vm.submitForm();
expect(vm.entryName).toBe('test-path/test');
});
});
});
|