debian-mirror-gitlab/spec/frontend/behaviors/copy_as_gfm_spec.js

137 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-03-02 22:35:43 +05:30
import initCopyAsGFM, { CopyAsGFM } from '~/behaviors/markdown/copy_as_gfm';
2021-03-11 19:13:27 +05:30
import * as commonUtils from '~/lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
describe('CopyAsGFM', () => {
describe('CopyAsGFM.pasteGFM', () => {
function callPasteGFM() {
const e = {
originalEvent: {
clipboardData: {
getData(mimeType) {
// When GFM code is copied, we put the regular plain text
// on the clipboard as `text/plain`, and the GFM as `text/x-gfm`.
// This emulates the behavior of `getData` with that data.
if (mimeType === 'text/plain') {
return 'code';
}
if (mimeType === 'text/x-gfm') {
return '`code`';
}
return null;
},
},
},
preventDefault() {},
};
CopyAsGFM.pasteGFM(e);
}
it('wraps pasted code when not already in code tags', () => {
2020-07-28 23:09:34 +05:30
jest.spyOn(commonUtils, 'insertText').mockImplementation((el, textFunc) => {
2018-03-17 18:26:18 +05:30
const insertedText = textFunc('This is code: ', '');
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(insertedText).toEqual('`code`');
});
callPasteGFM();
});
it('does not wrap pasted code when already in code tags', () => {
2020-07-28 23:09:34 +05:30
jest.spyOn(commonUtils, 'insertText').mockImplementation((el, textFunc) => {
2018-03-17 18:26:18 +05:30
const insertedText = textFunc('This is code: `', '`');
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(insertedText).toEqual('code');
});
callPasteGFM();
});
});
2018-11-08 19:23:39 +05:30
describe('CopyAsGFM.copyGFM', () => {
// Stub getSelection to return a purpose-built object.
const stubSelection = (html, parentNode) => ({
getRangeAt: () => ({
commonAncestorContainer: { tagName: parentNode },
cloneContents: () => {
const fragment = document.createDocumentFragment();
const node = document.createElement('div');
node.innerHTML = html;
2021-03-08 18:12:59 +05:30
Array.from(node.childNodes).forEach((item) => fragment.appendChild(item));
2018-11-08 19:23:39 +05:30
return fragment;
},
}),
rangeCount: 1,
});
const clipboardData = {
setData() {},
};
const simulateCopy = () => {
const e = {
originalEvent: {
clipboardData,
},
preventDefault() {},
stopPropagation() {},
};
CopyAsGFM.copyAsGFM(e, CopyAsGFM.transformGFMSelection);
return clipboardData;
};
2021-03-08 18:12:59 +05:30
beforeAll((done) => {
2019-03-02 22:35:43 +05:30
initCopyAsGFM();
// Fake call to nodeToGfm so the import of lazy bundle happened
CopyAsGFM.nodeToGFM(document.createElement('div'))
.then(() => {
done();
})
.catch(done.fail);
});
2020-06-23 00:09:42 +05:30
beforeEach(() => jest.spyOn(clipboardData, 'setData'));
2018-11-08 19:23:39 +05:30
describe('list handling', () => {
2021-03-08 18:12:59 +05:30
it('uses correct gfm for unordered lists', (done) => {
2018-11-08 19:23:39 +05:30
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'UL');
2019-03-02 22:35:43 +05:30
2020-06-23 00:09:42 +05:30
window.getSelection = jest.fn(() => selection);
2018-11-08 19:23:39 +05:30
simulateCopy();
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-07-07 11:18:12 +05:30
const expectedGFM = '* List Item1\n* List Item2';
2018-12-13 13:39:08 +05:30
2019-03-02 22:35:43 +05:30
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
done();
});
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
it('uses correct gfm for ordered lists', (done) => {
2018-11-08 19:23:39 +05:30
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'OL');
2019-03-02 22:35:43 +05:30
2020-06-23 00:09:42 +05:30
window.getSelection = jest.fn(() => selection);
2018-11-08 19:23:39 +05:30
simulateCopy();
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-07-07 11:18:12 +05:30
const expectedGFM = '1. List Item1\n1. List Item2';
2018-12-13 13:39:08 +05:30
2019-03-02 22:35:43 +05:30
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
done();
});
2018-11-08 19:23:39 +05:30
});
});
});
2020-10-24 23:57:45 +05:30
describe('CopyAsGFM.quoted', () => {
const sampleGFM = '* List 1\n* List 2\n\n`Some code`';
2021-03-08 18:12:59 +05:30
it('adds quote char `> ` to each line', (done) => {
2020-10-24 23:57:45 +05:30
const expectedQuotedGFM = '> * List 1\n> * List 2\n> \n> `Some code`';
expect(CopyAsGFM.quoted(sampleGFM)).toEqual(expectedQuotedGFM);
done();
});
});
2018-03-17 18:26:18 +05:30
});