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
|
import PasteMarkdownTable from '~/behaviors/markdown/paste_markdown_table';
describe('PasteMarkdownTable', () => {
let data;
beforeEach(() => {
const event = new window.Event('paste');
Object.defineProperty(event, 'dataTransfer', {
value: {
getData: jest.fn().mockImplementation(type => {
if (type === 'text/html') {
return '<table><tr><td>First</td><td>Second</td></tr></table>';
}
return 'First\tSecond';
}),
},
});
data = event.dataTransfer;
});
describe('isTable', () => {
it('return false when no HTML data is provided', () => {
data.types = ['text/plain'];
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
});
it('returns false when no text data is provided', () => {
data.types = ['text/html'];
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
});
it('returns true when a table is provided in both text and HTML', () => {
data.types = ['text/html', 'text/plain'];
expect(new PasteMarkdownTable(data).isTable()).toBe(true);
});
it('returns false when no HTML table is included', () => {
data.types = ['text/html', 'text/plain'];
data.getData = jest.fn().mockImplementation(() => 'nothing');
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
});
it('returns false when the number of rows are not consistent', () => {
data.types = ['text/html', 'text/plain'];
data.getData = jest.fn().mockImplementation(mimeType => {
if (mimeType === 'text/html') {
return '<table><tr><td>def test<td></tr></table>';
}
return "def test\n 'hello'\n";
});
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
});
it('returns false when the table copy comes from a diff', () => {
data.types = ['text/html', 'text/plain'];
data.getData = jest.fn().mockImplementation(mimeType => {
if (mimeType === 'text/html') {
return '<table class="diff-wrap-lines"><tr><td>First</td><td>Second</td></tr></table>';
}
return 'First\tSecond';
});
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
});
});
describe('convertToTableMarkdown', () => {
it('returns a Markdown table', () => {
data.types = ['text/html', 'text/plain'];
data.getData = jest.fn().mockImplementation(type => {
if (type === 'text/html') {
return '<table><tr><td>First</td><td>Last</td><tr><td>John</td><td>Doe</td><tr><td>Jane</td><td>Doe</td></table>';
} else if (type === 'text/plain') {
return 'First\tLast\nJohn\tDoe\nJane\tDoe';
}
return '';
});
const expected = [
'| First | Last |',
'|-------|------|',
'| John | Doe |',
'| Jane | Doe |',
].join('\n');
const converter = new PasteMarkdownTable(data);
expect(converter.isTable()).toBe(true);
expect(converter.convertToTableMarkdown()).toBe(expected);
});
it('returns a Markdown table with rows normalized', () => {
data.types = ['text/html', 'text/plain'];
data.getData = jest.fn().mockImplementation(type => {
if (type === 'text/html') {
return '<table><tr><td>First</td><td>Last</td><tr><td>John</td><td>Doe</td><tr><td>Jane</td><td>/td></table>';
} else if (type === 'text/plain') {
return 'First\tLast\nJohn\tDoe\nJane';
}
return '';
});
const expected = [
'| First | Last |',
'|-------|------|',
'| John | Doe |',
'| Jane | |',
].join('\n');
const converter = new PasteMarkdownTable(data);
expect(converter.isTable()).toBe(true);
expect(converter.convertToTableMarkdown()).toBe(expected);
});
});
});
|