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
|
import Vue from 'vue';
import { shallowMount, mount } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import BlobHeader from '~/blob/components/blob_header.vue';
import DefaultActions from '~/blob/components/blob_header_default_actions.vue';
import BlobFilepath from '~/blob/components/blob_header_filepath.vue';
import ViewerSwitcher from '~/blob/components/blob_header_viewer_switcher.vue';
import {
RICH_BLOB_VIEWER_TITLE,
SIMPLE_BLOB_VIEWER,
SIMPLE_BLOB_VIEWER_TITLE,
} from '~/blob/components/constants';
import TableContents from '~/blob/components/table_contents.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import WebIdeLink from 'ee_else_ce/vue_shared/components/web_ide_link.vue';
import userInfoQuery from '~/blob/queries/user_info.query.graphql';
import applicationInfoQuery from '~/blob/queries/application_info.query.graphql';
import { Blob, userInfoMock, applicationInfoMock } from './mock_data';
Vue.use(VueApollo);
describe('Blob Header Default Actions', () => {
let wrapper;
const defaultProvide = {
blobHash: 'foo-bar',
};
const findDefaultActions = () => wrapper.findComponent(DefaultActions);
const findTableContents = () => wrapper.findComponent(TableContents);
const findViewSwitcher = () => wrapper.findComponent(ViewerSwitcher);
const findBlobFilePath = () => wrapper.findComponent(BlobFilepath);
const findRichTextEditorBtn = () => wrapper.findByLabelText(RICH_BLOB_VIEWER_TITLE);
const findSimpleTextEditorBtn = () => wrapper.findByLabelText(SIMPLE_BLOB_VIEWER_TITLE);
const findWebIdeLink = () => wrapper.findComponent(WebIdeLink);
async function createComponent({
blobProps = {},
options = {},
propsData = {},
mountFn = shallowMount,
} = {}) {
const userInfoMockResolver = jest.fn().mockResolvedValue({
data: { ...userInfoMock },
});
const applicationInfoMockResolver = jest.fn().mockResolvedValue({
data: { ...applicationInfoMock },
});
const fakeApollo = createMockApollo([
[userInfoQuery, userInfoMockResolver],
[applicationInfoQuery, applicationInfoMockResolver],
]);
wrapper = mountFn(BlobHeader, {
apolloProvider: fakeApollo,
provide: {
...defaultProvide,
},
propsData: {
blob: { ...Blob, ...blobProps },
...propsData,
},
...options,
});
await waitForPromises();
}
describe('rendering', () => {
describe('WebIdeLink component', () => {
it('renders the WebIdeLink component with the correct props', async () => {
const { ideEditPath, editBlobPath, gitpodBlobUrl, pipelineEditorPath } = Blob;
const showForkSuggestion = false;
await createComponent({ propsData: { showForkSuggestion } });
expect(findWebIdeLink().props()).toMatchObject({
showEditButton: true,
editUrl: editBlobPath,
webIdeUrl: ideEditPath,
needsToFork: showForkSuggestion,
showPipelineEditorButton: Boolean(pipelineEditorPath),
pipelineEditorUrl: pipelineEditorPath,
gitpodUrl: gitpodBlobUrl,
showGitpodButton: applicationInfoMock.gitpodEnabled,
gitpodEnabled: userInfoMock.currentUser.gitpodEnabled,
userPreferencesGitpodPath: userInfoMock.currentUser.preferencesGitpodPath,
userProfileEnableGitpodPath: userInfoMock.currentUser.profileEnableGitpodPath,
});
});
it.each([[{ archived: true }], [{ editBlobPath: null }]])(
'does not render the WebIdeLink component when blob is archived or does not have an edit path',
(blobProps) => {
createComponent({ blobProps });
expect(findWebIdeLink().exists()).toBe(false);
},
);
});
describe('default render', () => {
it.each`
findComponent | componentName
${findTableContents} | ${'TableContents'}
${findViewSwitcher} | ${'ViewSwitcher'}
${findDefaultActions} | ${'DefaultActions'}
${findBlobFilePath} | ${'BlobFilePath'}
`('renders $componentName component by default', ({ findComponent }) => {
createComponent();
expect(findComponent().exists()).toBe(true);
});
});
it.each([[{ showBlameToggle: true }], [{ showBlameToggle: false }]])(
'passes the `showBlameToggle` prop to the viewer switcher',
(propsData) => {
createComponent({ propsData });
expect(findViewSwitcher().props('showBlameToggle')).toBe(propsData.showBlameToggle);
},
);
it('does not render viewer switcher if the blob has only the simple viewer', () => {
createComponent({
blobProps: {
richViewer: null,
},
});
expect(findViewSwitcher().props('showViewerToggles')).toBe(false);
});
it('does not render viewer switcher if a corresponding prop is passed', () => {
createComponent({
propsData: {
hideViewerSwitcher: true,
},
});
expect(findViewSwitcher().exists()).toBe(false);
});
it('does not render default actions is corresponding prop is passed', () => {
createComponent({
propsData: {
hideDefaultActions: true,
},
});
expect(findDefaultActions().exists()).toBe(false);
});
it.each`
slotContent | key
${'Foo Prepend'} | ${'prepend'}
${'Actions Bar'} | ${'actions'}
`('renders the slot $key', ({ key, slotContent }) => {
createComponent({
options: {
scopedSlots: {
[key]: `<span>${slotContent}</span>`,
},
},
mountFn: mount,
});
expect(wrapper.text()).toContain(slotContent);
});
it('passes information about render error down to default actions', () => {
createComponent({
propsData: {
hasRenderError: true,
},
});
expect(findDefaultActions().props('hasRenderError')).toBe(true);
});
it('passes the correct isBinary value to default actions when viewing a binary file', () => {
createComponent({ propsData: { isBinary: true } });
expect(findDefaultActions().props('isBinary')).toBe(true);
});
});
describe('functionality', () => {
const factory = (hideViewerSwitcher = false) => {
createComponent({
propsData: {
activeViewerType: SIMPLE_BLOB_VIEWER,
hideViewerSwitcher,
},
mountFn: mountExtended,
});
};
it('shows the correctly selected view by default', () => {
factory();
expect(findViewSwitcher().exists()).toBe(true);
expect(findRichTextEditorBtn().props().selected).toBe(false);
expect(findSimpleTextEditorBtn().props().selected).toBe(true);
});
it('Does not show the viewer switcher should be hidden', () => {
factory(true);
expect(findViewSwitcher().exists()).toBe(false);
});
it('watches the changes in viewer data and emits event when the change is registered', async () => {
factory();
await findRichTextEditorBtn().trigger('click');
expect(wrapper.emitted('viewer-changed')).toBeDefined();
});
it('sets different icons depending on the blob file type', async () => {
factory();
expect(findViewSwitcher().props('docIcon')).toBe('document');
await wrapper.setProps({
blob: {
...Blob,
richViewer: {
...Blob.richViewer,
fileType: 'csv',
},
},
});
expect(findViewSwitcher().props('docIcon')).toBe('table');
});
});
});
|