Skip to content

Commit

Permalink
test: add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 4, 2023
1 parent 3b58c49 commit e3fb47e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import copyTextToClipboard from '../src/main';

it('should handle document not available', () => {
const text = 'Hello, world!';
const mockCb = jest.fn();
// Create a spy to replace the document object
const documentSpy = jest.spyOn(global, 'document', 'get');
documentSpy.mockImplementation(() => undefined);
copyTextToClipboard(text, mockCb);
expect(mockCb).not.toHaveBeenCalled();
// Restore the original document object
documentSpy.mockRestore();
});

test('copyTextToClipboard should copy the text to the clipboard and return true', () => {
const text = 'Hello, world!';
const copied = jest.fn();
Expand Down Expand Up @@ -66,4 +78,4 @@ it('should copy text to clipboard', () => {
expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(document.body.removeChild).toHaveBeenCalled();
expect(mockCb).toHaveBeenCalledWith(true);
});
});

0 comments on commit e3fb47e

Please sign in to comment.