From 6ad1f6e0beac40a063e3e9800731d8e69b059c7b Mon Sep 17 00:00:00 2001 From: StefonSimmons Date: Mon, 15 Apr 2024 18:06:10 -0400 Subject: [PATCH] add tests --- assets/scripts/components/copy-code.js | 1 + assets/scripts/tests/copy-code.test.js | 94 ++++++++------------------ 2 files changed, 30 insertions(+), 65 deletions(-) diff --git a/assets/scripts/components/copy-code.js b/assets/scripts/components/copy-code.js index d43d792e901fd..8546487f05f88 100644 --- a/assets/scripts/components/copy-code.js +++ b/assets/scripts/components/copy-code.js @@ -46,6 +46,7 @@ function addCopyButton (fencedLangs) { // EVENT FUNCTION for copy functionality function copyCode ({currentTarget}, btn){ const code = getCode(currentTarget) + console.log('CODE', code) // Create a range object const range = document.createRange(); // Select the node diff --git a/assets/scripts/tests/copy-code.test.js b/assets/scripts/tests/copy-code.test.js index 426a133f1564f..a860fbcff0361 100644 --- a/assets/scripts/tests/copy-code.test.js +++ b/assets/scripts/tests/copy-code.test.js @@ -1,5 +1,6 @@ import * as cc from '../components/copy-code'; + describe('initCopyCode', () => { it('should add copy button to specified languages of fenced code blocks in markdown', () => { document.body.innerHTML = ` @@ -56,69 +57,32 @@ describe('initCopyCode', () => { }) - // it('should copy code to clipboard when copy button is clicked', () => { - - // document.body.innerHTML = ` - //
- //
some code
- //
- // - //
- //
- // `; - - // const highlight = document.querySelector('.highlight'); - // const copyBtn = document.querySelector('.js-copy-button'); - // const code = document.querySelector('code'); - - // // mock closest method - // const mockClosest = jest.fn(() => { - // querySelector: jest.fn(() => code) - // }); - - // // mock previousElementSibling property - // Object.defineProperty(Element.prototype, 'previousElementSibling', { - // get() { - // return highlight; - // } - // }); - - // // mock navigator.clipboard.writeText - // Object.assign(navigator, { - // clipboard: { - // writeText: jest.fn((text) => { - // Promise.resolve() - // }) - // } - // }); - - // Object.assign(document, { - // createRange: jest.fn(() => ({ - // selectNode: jest.fn() - // })) - // }); - - // // spyOn document methods: `closest` and `previousElementSibling` - // jest.spyOn(Element.prototype, 'closest').mockImplementation(mockClosest); - // jest.spyOn(Element.prototype, 'previousElementSibling', 'get') - - // // spyOn getCode function - // jest.spyOn(cc, 'getCode').mockImplementation(() => code); - - // // spyOn createRange method - // const createRangeSpy = jest.spyOn(document, "createRange") - - // // spyOn writeText method - // const writeTextSpy = jest.spyOn(navigator.clipboard, 'writeText'); - // cc.copyCode({currentTarget: copyBtn}, copyBtn); - - // expect(createRangeSpy).toHaveBeenCalled(); - // expect(createRangeSpy).toHaveBeenCalledWith(code); - - // expect(writeTextSpy).toHaveBeenCalled(); - // expect(writeTextSpy).toHaveBeenCalledWith(code.innerText); - - // // cleanup - // jest.restoreAllMocks(); - // }) + it('should get code snippet text for markdown fenced code blocks', () => { + document.body.innerHTML = ` +
+
some code
+ +
+ `; + const code = document.querySelector('code'); + const btn = document.querySelector('button'); + expect(cc.getCode(btn)).toBe(code); + }) + + it('should get code snippet text for try-rule modal', () => { + document.body.innerHTML = ` +
+ + + + + +
some code not returnedsome code returned
+
+ + `; + const code = document.querySelector('td:last-child code'); + const btn = document.querySelector('button'); + expect(cc.getCode(btn)).toBe(code); + }) }) \ No newline at end of file