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
|
import { nextTick } from 'vue';
import { shallowMount } from '@vue/test-utils';
import { handleBlobRichViewer } from '~/blob/viewer';
import RichViewer from '~/vue_shared/components/blob_viewers/rich_viewer.vue';
import MarkdownFieldView from '~/vue_shared/components/markdown/field_view.vue';
import {
MARKUP_FILE_TYPE,
CONTENT_LOADED_EVENT,
} from '~/vue_shared/components/blob_viewers/constants';
import { handleLocationHash } from '~/lib/utils/common_utils';
jest.mock('~/blob/viewer');
jest.mock('~/lib/utils/common_utils');
describe('Blob Rich Viewer component', () => {
let wrapper;
const dummyContent = '<h1 id="markdown">Foo Bar</h1>';
const defaultType = 'markdown';
function createComponent(type = defaultType, richViewer, content = dummyContent) {
wrapper = shallowMount(RichViewer, {
propsData: {
richViewer,
content,
type,
},
});
}
beforeEach(() => createComponent());
const findMarkdownFieldView = () => wrapper.findComponent(MarkdownFieldView);
describe('Markdown content', () => {
const generateDummyContent = (contentLength) => {
let generatedContent = '';
for (let i = 0; i < contentLength; i += 1) {
generatedContent += `<span>Line: ${i + 1}</span>\n`;
}
generatedContent += '<img src="x" onerror="alert(`XSS`)">'; // for testing against XSS
return `<div class="js-markup-content">${generatedContent}</div>`;
};
describe('Large file', () => {
const content = generateDummyContent(50);
beforeEach(() => createComponent(MARKUP_FILE_TYPE, null, content));
it('renders the top of the file immediately and does not emit a content loaded event', () => {
expect(wrapper.text()).toContain('Line: 10');
expect(wrapper.text()).not.toContain('Line: 50');
expect(wrapper.emitted(CONTENT_LOADED_EVENT)).toBeUndefined();
expect(findMarkdownFieldView().props('isLoading')).toBe(true);
});
it('renders the rest of the file later and emits a content loaded event', async () => {
jest.runAllTimers();
await nextTick();
expect(wrapper.text()).toContain('Line: 10');
expect(wrapper.text()).toContain('Line: 50');
expect(wrapper.emitted(CONTENT_LOADED_EVENT)).toHaveLength(1);
expect(findMarkdownFieldView().props('isLoading')).toBe(false);
});
it('sanitizes the content', () => {
jest.runAllTimers();
expect(wrapper.html()).toContain('<img src="x">');
});
});
describe('Small file', () => {
const content = generateDummyContent(5);
beforeEach(() => createComponent(MARKUP_FILE_TYPE, null, content));
it('renders the entire file immediately and emits a content loaded event', () => {
expect(wrapper.text()).toContain('Line: 5');
expect(wrapper.emitted(CONTENT_LOADED_EVENT)).toHaveLength(1);
expect(findMarkdownFieldView().props('isLoading')).toBe(false);
});
it('sanitizes the content', () => {
expect(wrapper.html()).toContain('<img src="x">');
});
});
});
it('renders the passed content without transformations', () => {
expect(wrapper.html()).toContain(dummyContent);
});
it('renders the richViewer if one is present and emits a content loaded event', async () => {
const richViewer = '<div class="js-pdf-viewer"></div>';
createComponent('pdf', richViewer);
await nextTick();
expect(wrapper.html()).toContain(richViewer);
expect(wrapper.emitted(CONTENT_LOADED_EVENT)).toHaveLength(1);
});
it('queries for advanced viewer', () => {
expect(handleBlobRichViewer).toHaveBeenCalledWith(expect.anything(), defaultType);
});
it('is using Markdown View Field', () => {
expect(findMarkdownFieldView().exists()).toBe(true);
});
it('scrolls to the hash location', () => {
expect(handleLocationHash).toHaveBeenCalled();
});
});
|