Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit 16b12dd

Browse files
bpedersenpriscilawebdev
authored andcommitted
refactor(#204): copyToClipBoard.test, moved test of utility to it's own test
1 parent dd53295 commit 16b12dd

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
import React from 'react';
22
import { mount, ReactWrapper } from 'enzyme';
33

4+
import { copyToClipBoardUtility } from '../../utils/cli-utils';
5+
46
import CopyToClipBoard from './CopyToClipBoard';
57
import { CopyIcon } from './styles';
68

9+
jest.mock('../../utils/cli-utils');
10+
711
describe('<CopyToClipBoard /> component', () => {
812
let wrapper: ReactWrapper;
13+
const copyText = 'copy text';
914

1015
beforeEach(() => {
11-
wrapper = mount(<CopyToClipBoard text={'copy text'} />);
16+
wrapper = mount(<CopyToClipBoard text={copyText} />);
1217
});
1318

1419
test('render the component', () => {
1520
expect(wrapper.html()).toMatchSnapshot();
1621
});
1722

18-
test('should call the DOM APIs for copy to clipboard utility', () => {
19-
const event = {
20-
preventDefault: jest.fn(),
21-
};
22-
23-
// @ts-ignore: Property 'getSelection' does not exist on type 'Global'.
24-
global.getSelection = jest.fn(() => ({
25-
removeAllRanges: () => {},
26-
addRange: () => {},
27-
}));
28-
29-
// @ts-ignore: Property 'document/getSelection' does not exist on type 'Global'.
30-
const { document, getSelection } = global;
31-
32-
wrapper.find(CopyIcon).simulate('click', event);
33-
expect(event.preventDefault).toHaveBeenCalled();
34-
expect(document.createRange).toHaveBeenCalled();
35-
expect(getSelection).toHaveBeenCalled();
36-
expect(document.execCommand).toHaveBeenCalledWith('copy');
23+
test('should call the copyToClipBoardUtility for copy to clipboard utility', () => {
24+
wrapper.find(CopyIcon).simulate('click');
25+
expect(copyToClipBoardUtility).toHaveBeenCalledWith(copyText);
3726
});
3827
});

src/utils/cli-utils.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { SyntheticEvent } from 'react';
2+
3+
import { copyToClipBoardUtility } from './cli-utils';
4+
5+
describe('copyToClipBoardUtility', () => {
6+
let originalGetSelection;
7+
8+
const mockGetSelectionResult = {
9+
removeAllRanges: jest.fn(),
10+
addRange: jest.fn(),
11+
};
12+
beforeEach(() => {
13+
originalGetSelection = window.getSelection;
14+
15+
window.getSelection = jest.fn().mockReturnValue(mockGetSelectionResult);
16+
});
17+
afterEach(() => {
18+
window.getSelection = originalGetSelection;
19+
jest.restoreAllMocks();
20+
});
21+
22+
test('should call the DOM APIs', () => {
23+
// Given
24+
const testEvent: { preventDefault: Function } = {
25+
preventDefault: jest.fn(),
26+
};
27+
const testCopy = 'copy text';
28+
const spys = {
29+
createElement: jest.spyOn(document, 'createElement'),
30+
execCommand: jest.spyOn(document, 'execCommand'),
31+
appendChild: jest.spyOn(document.body, 'appendChild'),
32+
removeChild: jest.spyOn(document.body, 'removeChild'),
33+
};
34+
const expectedDiv = document.createElement('div');
35+
expectedDiv.innerText = testCopy;
36+
37+
// When
38+
const copyFunc = copyToClipBoardUtility(testCopy);
39+
copyFunc(testEvent as SyntheticEvent<HTMLElement>);
40+
41+
// Then
42+
expect(mockGetSelectionResult.removeAllRanges).toHaveBeenCalledWith();
43+
expect(mockGetSelectionResult.addRange).toHaveBeenCalled();
44+
expect(spys.createElement).toHaveBeenCalledWith('div');
45+
expect(spys.appendChild).toHaveBeenCalledWith(expectedDiv);
46+
expect(spys.execCommand).toHaveBeenCalledWith('copy');
47+
expect(spys.removeChild).toHaveBeenCalledWith(expectedDiv);
48+
});
49+
});

0 commit comments

Comments
 (0)