2019-12-31 18:09:10 +00:00
|
|
|
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') {
|
2020-01-09 09:07:51 +00:00
|
|
|
return '<table><tr><td>First</td><td>Second</td></tr></table>';
|
2019-12-31 18:09:10 +00:00
|
|
|
}
|
2020-01-09 09:07:51 +00:00
|
|
|
return 'First\tSecond';
|
2019-12-31 18:09:10 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
data = event.dataTransfer;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isTable', () => {
|
|
|
|
it('return false when no HTML data is provided', () => {
|
|
|
|
data.types = ['text/plain'];
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
|
2019-12-31 18:09:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false when no text data is provided', () => {
|
|
|
|
data.types = ['text/html'];
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
|
2019-12-31 18:09:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true when a table is provided in both text and HTML', () => {
|
|
|
|
data.types = ['text/html', 'text/plain'];
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
expect(new PasteMarkdownTable(data).isTable()).toBe(true);
|
2019-12-31 18:09:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false when no HTML table is included', () => {
|
|
|
|
data.types = ['text/html', 'text/plain'];
|
|
|
|
data.getData = jest.fn().mockImplementation(() => 'nothing');
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
|
2019-12-31 18:09:10 +00:00
|
|
|
});
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
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";
|
|
|
|
});
|
2019-12-31 18:09:10 +00:00
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
expect(new PasteMarkdownTable(data).isTable()).toBe(false);
|
2019-12-31 18:09:10 +00:00
|
|
|
});
|
2020-04-28 09:09:34 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2020-01-09 09:07:51 +00:00
|
|
|
});
|
2019-12-31 18:09:10 +00:00
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
describe('convertToTableMarkdown', () => {
|
2019-12-31 18:09:10 +00:00
|
|
|
it('returns a Markdown table', () => {
|
2020-01-09 09:07:51 +00:00
|
|
|
data.types = ['text/html', 'text/plain'];
|
2019-12-31 18:09:10 +00:00
|
|
|
data.getData = jest.fn().mockImplementation(type => {
|
2020-01-09 09:07:51 +00:00
|
|
|
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') {
|
2019-12-31 18:09:10 +00:00
|
|
|
return 'First\tLast\nJohn\tDoe\nJane\tDoe';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = [
|
|
|
|
'| First | Last |',
|
|
|
|
'|-------|------|',
|
|
|
|
'| John | Doe |',
|
|
|
|
'| Jane | Doe |',
|
|
|
|
].join('\n');
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
const converter = new PasteMarkdownTable(data);
|
|
|
|
|
|
|
|
expect(converter.isTable()).toBe(true);
|
2019-12-31 18:09:10 +00:00
|
|
|
expect(converter.convertToTableMarkdown()).toBe(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a Markdown table with rows normalized', () => {
|
2020-01-09 09:07:51 +00:00
|
|
|
data.types = ['text/html', 'text/plain'];
|
2019-12-31 18:09:10 +00:00
|
|
|
data.getData = jest.fn().mockImplementation(type => {
|
2020-01-09 09:07:51 +00:00
|
|
|
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') {
|
2019-12-31 18:09:10 +00:00
|
|
|
return 'First\tLast\nJohn\tDoe\nJane';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = [
|
|
|
|
'| First | Last |',
|
|
|
|
'|-------|------|',
|
|
|
|
'| John | Doe |',
|
|
|
|
'| Jane | |',
|
|
|
|
].join('\n');
|
|
|
|
|
2020-01-09 09:07:51 +00:00
|
|
|
const converter = new PasteMarkdownTable(data);
|
|
|
|
|
|
|
|
expect(converter.isTable()).toBe(true);
|
2019-12-31 18:09:10 +00:00
|
|
|
expect(converter.convertToTableMarkdown()).toBe(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|