This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
paste.test.tsx
102 lines (81 loc) · 5.12 KB
/
paste.test.tsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {parseTable, getTable, formatMarkdownTableMessage, formatGithubCodePaste} from './paste';
const validClipboardData: any = {
items: [1],
types: ['text/html'],
getData: () => {
return '<table><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></table>';
},
};
const validTable: any = parseTable(validClipboardData.getData());
describe('Paste.getTable', () => {
test('returns false without html in the clipboard', () => {
const badClipboardData: any = {
items: [1],
types: ['text/plain'],
};
expect(getTable(badClipboardData)).toBe(false);
});
test('returns false without table in the clipboard', () => {
const badClipboardData: any = {
items: [1],
types: ['text/html'],
getData: () => '<p>There is no table here</p>',
};
expect(getTable(badClipboardData)).toBe(false);
});
test('returns table from valid clipboard data', () => {
expect(getTable(validClipboardData)).toEqual(validTable);
});
});
describe('Paste.formatMarkdownTableMessage', () => {
const markdownTable = '|test | test|\n|--- | ---|\n|test | test|\n';
test('returns a markdown table when valid html table provided', () => {
expect(formatMarkdownTableMessage(validTable)).toBe(markdownTable);
});
test('returns a markdown table under a message when one is provided', () => {
const testMessage = 'test message';
expect(formatMarkdownTableMessage(validTable, testMessage)).toBe(`${testMessage}\n\n${markdownTable}`);
});
});
describe('Paste.formatGithubCodePaste', () => {
const clipboardData: any = {
items: [],
types: ['text/plain', 'text/html'],
getData: (type: any) => {
if (type === 'text/plain') {
return '// a javascript codeblock example\nif (1 > 0) {\n return \'condition is true\';\n}';
}
return '<table class="highlight tab-size js-file-line-container" data-tab-size="8"><tbody><tr><td id="LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-c"><span class="pl-c">//</span> a javascript codeblock example</span></td></tr><tr><td id="L2" class="blob-num js-line-number" data-line-number="2"> </td><td id="LC2" class="blob-code blob-code-inner js-file-line"><span class="pl-k">if</span> (<span class="pl-c1">1</span> <span class="pl-k">></span> <span class="pl-c1">0</span>) {</td></tr><tr><td id="L3" class="blob-num js-line-number" data-line-number="3"> </td><td id="LC3" class="blob-code blob-code-inner js-file-line"><span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">\'</span>condition is true<span class="pl-pds">\'</span></span>);</td></tr><tr><td id="L4" class="blob-num js-line-number" data-line-number="4"> </td><td id="LC4" class="blob-code blob-code-inner js-file-line">}</td></tr></tbody></table>';
},
};
test('Formatted message for empty message', () => {
const message = "```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```";
const codeBlock = "```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```";
const {formattedMessage, formattedCodeBlock} = formatGithubCodePaste(0, '', clipboardData);
expect(message).toBe(formattedMessage);
expect(codeBlock).toBe(formattedCodeBlock);
});
test('Formatted message with a draft and cursor at end', () => {
const message = "test\n```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```";
const codeBlock = "\n```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```";
const {formattedMessage, formattedCodeBlock} = formatGithubCodePaste(4, 'test', clipboardData);
expect(message).toBe(formattedMessage);
expect(codeBlock).toBe(formattedCodeBlock);
});
test('Formatted message with a draft and cursor at start', () => {
const message = "```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```\ntest";
const codeBlock = "```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```\n";
const {formattedMessage, formattedCodeBlock} = formatGithubCodePaste(0, 'test', clipboardData);
expect(message).toBe(formattedMessage);
expect(codeBlock).toBe(formattedCodeBlock);
});
test('Formatted message with a draft and cursor at middle', () => {
const message = "te\n```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```\nst";
const codeBlock = "\n```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```\n";
const {formattedMessage, formattedCodeBlock} = formatGithubCodePaste(2, 'test', clipboardData);
expect(message).toBe(formattedMessage);
expect(codeBlock).toBe(formattedCodeBlock);
});
});