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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
import { GlBadge, GlLink, GlIcon, GlIntersectionObserver } from '@gitlab/ui';
import { shallowMount, RouterLinkStub } from '@vue/test-utils';
import { nextTick } from 'vue';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import TableRow from '~/repository/components/table/row.vue';
import FileIcon from '~/vue_shared/components/file_icon.vue';
import { FILE_SYMLINK_MODE } from '~/vue_shared/constants';
import { ROW_APPEAR_DELAY } from '~/repository/constants';
const COMMIT_MOCK = { lockLabel: 'Locked by Root', committedDate: '2019-01-01' };
let vm;
let $router;
function factory(propsData = {}) {
$router = {
push: jest.fn(),
};
vm = shallowMount(TableRow, {
propsData: {
commitInfo: COMMIT_MOCK,
...propsData,
name: propsData.path,
projectPath: 'gitlab-org/gitlab-ce',
url: `https://test.com`,
totalEntries: 10,
rowNumber: 123,
},
directives: {
GlHoverLoad: createMockDirective('gl-hover-load'),
},
mocks: {
$router,
},
stubs: {
RouterLink: RouterLinkStub,
},
});
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
vm.setData({ escapedRef: 'main' });
}
describe('Repository table row component', () => {
const findRouterLink = () => vm.findComponent(RouterLinkStub);
const findIntersectionObserver = () => vm.findComponent(GlIntersectionObserver);
it('renders table row', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'file',
currentPath: '/',
});
await nextTick();
expect(vm.element).toMatchSnapshot();
});
it('renders a symlink table row', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'blob',
currentPath: '/',
mode: FILE_SYMLINK_MODE,
});
await nextTick();
expect(vm.element).toMatchSnapshot();
});
it('renders table row for path with special character', async () => {
factory({
id: '1',
sha: '123',
path: 'test$/test',
type: 'file',
currentPath: 'test$',
});
await nextTick();
expect(vm.element).toMatchSnapshot();
});
it('renders a gl-hover-load directive', () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'blob',
currentPath: '/',
});
const hoverLoadDirective = getBinding(findRouterLink().element, 'gl-hover-load');
expect(hoverLoadDirective).not.toBeUndefined();
expect(hoverLoadDirective.value).toBeInstanceOf(Function);
});
it.each`
type | component | componentName
${'tree'} | ${RouterLinkStub} | ${'RouterLink'}
${'blob'} | ${RouterLinkStub} | ${'RouterLink'}
${'commit'} | ${'a'} | ${'hyperlink'}
`('renders a $componentName for type $type', async ({ type, component }) => {
factory({
id: '1',
sha: '123',
path: 'test',
type,
currentPath: '/',
});
await nextTick();
expect(vm.findComponent(component).exists()).toBe(true);
});
it.each`
path
${'test#'}
${'Änderungen'}
`('renders link for $path', async ({ path }) => {
factory({
id: '1',
sha: '123',
path,
type: 'tree',
currentPath: '/',
});
await nextTick();
expect(vm.findComponent({ ref: 'link' }).props('to')).toEqual({
path: `/-/tree/main/${encodeURIComponent(path)}`,
});
});
it('renders link for directory with hash', async () => {
factory({
id: '1',
sha: '123',
path: 'test#',
type: 'tree',
currentPath: '/',
});
await nextTick();
expect(vm.find('.tree-item-link').props('to')).toEqual({ path: '/-/tree/main/test%23' });
});
it('renders commit ID for submodule', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'commit',
currentPath: '/',
});
await nextTick();
expect(vm.find('.commit-sha').text()).toContain('1');
});
it('renders link with href', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'blob',
url: 'https://test.com',
currentPath: '/',
});
await nextTick();
expect(vm.find('a').attributes('href')).toEqual('https://test.com');
});
it('renders LFS badge', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'commit',
currentPath: '/',
lfsOid: '1',
});
await nextTick();
expect(vm.findComponent(GlBadge).exists()).toBe(true);
});
it('renders commit and web links with href for submodule', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'commit',
url: 'https://test.com',
submoduleTreeUrl: 'https://test.com/commit',
currentPath: '/',
});
await nextTick();
expect(vm.find('a').attributes('href')).toEqual('https://test.com');
expect(vm.findComponent(GlLink).attributes('href')).toEqual('https://test.com/commit');
});
it('renders lock icon', async () => {
factory({
id: '1',
sha: '123',
path: 'test',
type: 'tree',
currentPath: '/',
});
await nextTick();
expect(vm.findComponent(GlIcon).exists()).toBe(true);
expect(vm.findComponent(GlIcon).props('name')).toBe('lock');
});
it('renders loading icon when path is loading', () => {
factory({
id: '1',
sha: '1',
path: 'test',
type: 'tree',
currentPath: '/',
loadingPath: 'test',
});
expect(vm.findComponent(FileIcon).props('loading')).toBe(true);
});
describe('row visibility', () => {
beforeEach(() => {
factory({
id: '1',
sha: '1',
path: 'test',
type: 'tree',
currentPath: '/',
commitInfo: null,
});
});
afterAll(() => jest.useRealTimers());
it('emits a `row-appear` event', async () => {
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
findIntersectionObserver().vm.$emit('appear');
jest.runAllTimers();
expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
expect(setTimeoutSpy).toHaveBeenLastCalledWith(expect.any(Function), ROW_APPEAR_DELAY);
expect(vm.emitted('row-appear')).toEqual([[123]]);
});
});
});
|