debian-mirror-gitlab/spec/frontend/notebook/cells/markdown_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

222 lines
6.9 KiB
JavaScript
Raw Normal View History

2021-11-11 11:23:49 +05:30
import { mount } from '@vue/test-utils';
2020-05-24 23:13:21 +05:30
import katex from 'katex';
2022-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2021-11-18 22:05:49 +05:30
import markdownTableJson from 'test_fixtures/blob/notebook/markdown-table.json';
import basicJson from 'test_fixtures/blob/notebook/basic.json';
import mathJson from 'test_fixtures/blob/notebook/math.json';
2020-05-24 23:13:21 +05:30
import MarkdownComponent from '~/notebook/cells/markdown.vue';
const Component = Vue.extend(MarkdownComponent);
window.katex = katex;
2021-11-11 11:23:49 +05:30
function buildCellComponent(cell, relativePath = '') {
return mount(Component, {
propsData: {
cell,
},
provide: {
relativeRawPath: relativePath,
},
}).vm;
}
function buildMarkdownComponent(markdownContent, relativePath = '') {
return buildCellComponent(
{
cell_type: 'markdown',
metadata: {},
source: markdownContent,
},
relativePath,
);
}
2020-05-24 23:13:21 +05:30
describe('Markdown component', () => {
let vm;
let cell;
let json;
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2021-11-18 22:05:49 +05:30
json = basicJson;
2020-05-24 23:13:21 +05:30
// eslint-disable-next-line prefer-destructuring
cell = json.cells[1];
2021-11-11 11:23:49 +05:30
vm = buildCellComponent(cell);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2020-05-24 23:13:21 +05:30
});
2021-06-08 01:23:25 +05:30
it('does not render prompt', () => {
2020-05-24 23:13:21 +05:30
expect(vm.$el.querySelector('.prompt span')).toBeNull();
});
it('does not render the markdown text', () => {
expect(vm.$el.querySelector('.markdown').innerHTML.trim()).not.toEqual(cell.source.join(''));
});
it('renders the markdown HTML', () => {
expect(vm.$el.querySelector('.markdown h1')).not.toBeNull();
});
2021-06-02 17:11:27 +05:30
it('sanitizes Markdown output', async () => {
2020-05-24 23:13:21 +05:30
Object.assign(cell, {
source: [
'[XSS](data:text/html;base64,PHNjcmlwdD5hbGVydChkb2N1bWVudC5kb21haW4pPC9zY3JpcHQ+Cg==)\n',
],
});
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
expect(vm.$el.querySelector('a').getAttribute('href')).toBeNull();
2020-05-24 23:13:21 +05:30
});
2021-06-02 17:11:27 +05:30
it('sanitizes HTML', async () => {
const findLink = () => vm.$el.querySelector('.xss-link');
Object.assign(cell, {
source: ['<a href="test.js" data-remote=true data-type="script" class="xss-link">XSS</a>\n'],
});
2022-04-04 11:22:00 +05:30
await nextTick();
2022-07-23 23:45:48 +05:30
expect(findLink().dataset.remote).toBeUndefined();
expect(findLink().dataset.type).toBeUndefined();
2021-06-02 17:11:27 +05:30
});
2021-11-11 11:23:49 +05:30
describe('When parsing images', () => {
it.each([
[
'for relative images in root folder, it does',
'![](local_image.png)\n',
'src="/raw/local_image',
],
[
'for relative images in child folders, it does',
'![](data/local_image.png)\n',
'src="/raw/data',
],
["for embedded images, it doesn't", '![](data:image/jpeg;base64)\n', 'src="data:'],
["for images urls, it doesn't", '![](http://image.png)\n', 'src="http:'],
])('%s', async ([testMd, mustContain]) => {
vm = buildMarkdownComponent([testMd], '/raw/');
2022-04-04 11:22:00 +05:30
await nextTick();
2021-11-11 11:23:49 +05:30
expect(vm.$el.innerHTML).toContain(mustContain);
});
});
2021-06-08 01:23:25 +05:30
describe('tables', () => {
beforeEach(() => {
2021-11-18 22:05:49 +05:30
json = markdownTableJson;
2021-06-08 01:23:25 +05:30
});
2022-04-04 11:22:00 +05:30
it('renders images and text', async () => {
2021-11-11 11:23:49 +05:30
vm = buildCellComponent(json.cells[0]);
2021-06-08 01:23:25 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
const images = vm.$el.querySelectorAll('img');
expect(images.length).toBe(5);
const columns = vm.$el.querySelectorAll('td');
expect(columns.length).toBe(6);
expect(columns[0].textContent).toEqual('Hello ');
expect(columns[1].textContent).toEqual('Test ');
expect(columns[2].textContent).toEqual('World ');
expect(columns[3].textContent).toEqual('Fake ');
expect(columns[4].textContent).toEqual('External image: ');
expect(columns[5].textContent).toEqual('Empty');
expect(columns[0].innerHTML).toContain('<img src="data:image/jpeg;base64');
expect(columns[1].innerHTML).toContain('<img src="data:image/png;base64');
expect(columns[2].innerHTML).toContain('<img src="data:image/jpeg;base64');
2022-08-13 15:12:31 +05:30
expect(columns[3].innerHTML).toContain('<img src="attachment:bogus">');
2022-04-04 11:22:00 +05:30
expect(columns[4].innerHTML).toContain('<img src="https://www.google.com/');
2021-06-08 01:23:25 +05:30
});
});
2020-05-24 23:13:21 +05:30
describe('katex', () => {
beforeEach(() => {
2021-11-18 22:05:49 +05:30
json = mathJson;
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('renders multi-line katex', async () => {
2021-11-11 11:23:49 +05:30
vm = buildCellComponent(json.cells[0]);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
expect(vm.$el.querySelector('.katex')).not.toBeNull();
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('renders inline katex', async () => {
2021-11-11 11:23:49 +05:30
vm = buildCellComponent(json.cells[1]);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
expect(vm.$el.querySelector('p:first-child .katex')).not.toBeNull();
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('renders multiple inline katex', async () => {
2021-11-11 11:23:49 +05:30
vm = buildCellComponent(json.cells[1]);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
expect(vm.$el.querySelectorAll('p:nth-child(2) .katex')).toHaveLength(4);
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('output cell in case of katex error', async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent(['Some invalid $a & b$ inline formula $b & c$\n', '\n']);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one paragraph with no katex formula in it
expect(vm.$el.querySelectorAll('p')).toHaveLength(1);
expect(vm.$el.querySelectorAll('p .katex')).toHaveLength(0);
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('output cell and render remaining formula in case of katex error', async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent([
'An invalid $a & b$ inline formula and a vaild one $b = c$\n',
'\n',
]);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one paragraph with no katex formula in it
expect(vm.$el.querySelectorAll('p')).toHaveLength(1);
expect(vm.$el.querySelectorAll('p .katex')).toHaveLength(1);
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it('renders math formula in list object', async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent(["- list with inline $a=2$ inline formula $a' + b = c$\n", '\n']);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one list with a katex formula in it
expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
2020-05-24 23:13:21 +05:30
});
2021-04-29 21:17:54 +05:30
it("renders math formula with tick ' in it", async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent(["- list with inline $a=2$ inline formula $a' + b = c$\n", '\n']);
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one list with a katex formula in it
expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
});
it('renders math formula with less-than-operator < in it', async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent(['- list with inline $a=2$ inline formula $a + b < c$\n', '\n']);
2021-04-29 21:17:54 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one list with a katex formula in it
expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
});
it('renders math formula with greater-than-operator > in it', async () => {
2021-11-11 11:23:49 +05:30
vm = buildMarkdownComponent(['- list with inline $a=2$ inline formula $a + b > c$\n', '\n']);
2021-04-29 21:17:54 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-29 21:17:54 +05:30
// expect one list with a katex formula in it
expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
2020-05-24 23:13:21 +05:30
});
});
});