diff options
Diffstat (limited to 'spec/frontend/lib/print_markdown_dom_spec.js')
-rw-r--r-- | spec/frontend/lib/print_markdown_dom_spec.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/spec/frontend/lib/print_markdown_dom_spec.js b/spec/frontend/lib/print_markdown_dom_spec.js new file mode 100644 index 00000000000..7f28417228e --- /dev/null +++ b/spec/frontend/lib/print_markdown_dom_spec.js @@ -0,0 +1,102 @@ +import printJS from 'print-js'; +import printMarkdownDom from '~/lib/print_markdown_dom'; + +jest.mock('print-js', () => jest.fn()); + +describe('print util', () => { + describe('print markdown dom', () => { + beforeEach(() => { + document.body.innerHTML = `<div id='target'></div>`; + }); + + const getTarget = () => document.getElementById('target'); + + const contentValues = [ + { + title: 'test title', + expectedTitle: '<h2 class="gl-mt-0 gl-mb-5">test title</h2>', + content: '', + expectedContent: '<div class="md"></div>', + }, + { + title: 'test title', + expectedTitle: '<h2 class="gl-mt-0 gl-mb-5">test title</h2>', + content: '<p>test content</p>', + expectedContent: '<div class="md"><p>test content</p></div>', + }, + { + title: 'test title', + expectedTitle: '<h2 class="gl-mt-0 gl-mb-5">test title</h2>', + content: '<details><summary>test detail</summary><p>test detail content</p></details>', + expectedContent: + '<div class="md"><details open=""><summary>test detail</summary><p>test detail content</p></details></div>', + }, + { + title: undefined, + expectedTitle: '', + content: '', + expectedContent: '<div class="md"></div>', + }, + { + title: undefined, + expectedTitle: '', + content: '<p>test content</p>', + expectedContent: '<div class="md"><p>test content</p></div>', + }, + { + title: undefined, + expectedTitle: '', + content: '<details><summary>test detail</summary><p>test detail content</p></details>', + expectedContent: + '<div class="md"><details open=""><summary>test detail</summary><p>test detail content</p></details></div>', + }, + ]; + + it.each(contentValues)( + 'should print with title ($title) and content ($content)', + async ({ title, expectedTitle, content, expectedContent }) => { + const target = getTarget(); + target.innerHTML = content; + const stylesheet = 'test stylesheet'; + + await printMarkdownDom({ + target, + title, + stylesheet, + }); + + expect(printJS).toHaveBeenCalledWith({ + printable: expectedTitle + expectedContent, + type: 'raw-html', + documentTitle: title, + scanStyles: false, + css: stylesheet, + }); + }, + ); + }); + + describe('ignore selectors', () => { + beforeEach(() => { + document.body.innerHTML = `<div id='target'><div><div class='ignore-me'></div></div></div>`; + }); + + it('should ignore dom if ignoreSelectors', async () => { + const target = document.getElementById('target'); + const ignoreSelectors = ['.ignore-me']; + + await printMarkdownDom({ + target, + ignoreSelectors, + }); + + expect(printJS).toHaveBeenCalledWith({ + printable: '<div class="md"><div></div></div>', + type: 'raw-html', + documentTitle: undefined, + scanStyles: false, + css: [], + }); + }); + }); +}); |