Skip to content

Commit

Permalink
add copycode test
Browse files Browse the repository at this point in the history
  • Loading branch information
StefonSimmons committed Apr 16, 2024
1 parent f6b8d07 commit 85f08ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion assets/scripts/components/copy-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ function updateCopyBtnText(btn){
initCopyCode()

// Export the functions for testing
module.exports = {initCopyCode, getCode, addCopyButton, updateCopyBtnText}
module.exports = {initCopyCode, getCode, copyCode, addCopyButton, updateCopyBtnText}
24 changes: 24 additions & 0 deletions assets/scripts/tests/copy-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,28 @@ describe('initCopyCode', () => {
cc.updateCopyBtnText(btn);
expect(btn.textContent).toBe('Copied!');
})


it('should copy code snippet text to clipboard with the writeText method', () => {
document.body.innerHTML = `
<div class="highlight code-snippet">
<pre><code>some code</code></pre>
<button class="js-copy-button">Copy</button>
</div>
`;
const btn = document.querySelector('.js-copy-button');

const clipboardMock = {
writeText: jest.fn(() => Promise.resolve())
};
Object.defineProperty(document, 'createRange', {
value: jest.fn(() => ({
selectNode: jest.fn(),
}))
})
navigator.clipboard = clipboardMock;

cc.copyCode(btn);
expect(clipboardMock.writeText).toHaveBeenCalledTimes(1);
})
})

0 comments on commit 85f08ab

Please sign in to comment.