|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import React from 'react'; |
| 11 | +import userEvent from '@testing-library/user-event'; |
| 12 | +import { buildCopyColumnNameButton, buildCopyColumnValuesButton } from './build_copy_column_button'; |
| 13 | +import { dataTableContextMock } from '../../__mocks__/table_context'; |
11 | 14 | import { EuiButton } from '@elastic/eui'; |
12 | | -import { mountWithIntl } from '@kbn/test-jest-helpers'; |
| 15 | +import { renderWithI18n } from '@kbn/test-jest-helpers'; |
| 16 | +import { screen } from '@testing-library/react'; |
13 | 17 | import { servicesMock } from '../../__mocks__/services'; |
14 | | -import { dataTableContextMock } from '../../__mocks__/table_context'; |
15 | | -import { buildCopyColumnNameButton, buildCopyColumnValuesButton } from './build_copy_column_button'; |
16 | 18 |
|
17 | 19 | const execCommandMock = (global.document.execCommand = jest.fn()); |
18 | | -const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 20 | +const originalClipboard = global.window.navigator.clipboard; |
| 21 | + |
| 22 | +const mockClipboard = () => { |
| 23 | + const writeText = jest.fn(); |
| 24 | + |
| 25 | + Object.assign(navigator, { |
| 26 | + clipboard: { writeText }, |
| 27 | + }); |
| 28 | + |
| 29 | + return writeText; |
| 30 | +}; |
19 | 31 |
|
20 | 32 | describe('Build a column button to copy to clipboard', () => { |
21 | | - it('should copy a column name to clipboard on click', () => { |
22 | | - const { label, iconType, onClick } = buildCopyColumnNameButton({ |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.restoreAllMocks(); |
| 39 | + Object.assign(navigator, { |
| 40 | + clipboard: originalClipboard, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should copy a column name to clipboard on click', async () => { |
| 45 | + const { iconType, label, onClick } = buildCopyColumnNameButton({ |
23 | 46 | columnDisplayName: 'test-field-name', |
24 | 47 | toastNotifications: servicesMock.toastNotifications, |
25 | 48 | }); |
26 | 49 | execCommandMock.mockImplementationOnce(() => true); |
27 | 50 |
|
28 | | - const wrapper = mountWithIntl( |
| 51 | + renderWithI18n( |
29 | 52 | <EuiButton iconType={iconType} onClick={onClick}> |
30 | 53 | {label} |
31 | 54 | </EuiButton> |
32 | 55 | ); |
33 | 56 |
|
34 | | - wrapper.find('button').simulate('click'); |
| 57 | + await userEvent.click(screen.getByRole('button', { name: 'Copy name' })); |
35 | 58 |
|
36 | 59 | expect(execCommandMock).toHaveBeenCalledWith('copy'); |
37 | | - expect(warn).not.toHaveBeenCalled(); |
| 60 | + expect(servicesMock.toastNotifications.addWarning).not.toHaveBeenCalled(); |
38 | 61 | }); |
39 | 62 |
|
40 | | - it('should copy column values to clipboard on click', async () => { |
41 | | - const originalClipboard = global.window.navigator.clipboard; |
42 | | - |
43 | | - Object.defineProperty(navigator, 'clipboard', { |
44 | | - value: { |
45 | | - writeText: jest.fn(), |
46 | | - }, |
47 | | - writable: true, |
48 | | - }); |
49 | | - |
50 | | - const { label, iconType, onClick } = buildCopyColumnValuesButton({ |
| 63 | + it('should copy regular column values to clipboard on click', async () => { |
| 64 | + const writeText = mockClipboard(); |
| 65 | + const { iconType, label, onClick } = buildCopyColumnValuesButton({ |
51 | 66 | columnId: 'extension', |
52 | 67 | columnDisplayName: 'custom_extension', |
53 | | - toastNotifications: servicesMock.toastNotifications, |
54 | 68 | rowsCount: 3, |
| 69 | + toastNotifications: servicesMock.toastNotifications, |
55 | 70 | valueToStringConverter: dataTableContextMock.valueToStringConverter, |
56 | 71 | }); |
57 | 72 |
|
58 | | - const wrapper = mountWithIntl( |
| 73 | + renderWithI18n( |
59 | 74 | <EuiButton iconType={iconType} onClick={onClick}> |
60 | 75 | {label} |
61 | 76 | </EuiButton> |
62 | 77 | ); |
63 | 78 |
|
64 | | - await wrapper.find('button').simulate('click'); |
| 79 | + await userEvent.click(screen.getByRole('button', { name: 'Copy column' })); |
65 | 80 |
|
66 | 81 | // first row out of 3 rows does not have a value |
67 | | - expect(navigator.clipboard.writeText).toHaveBeenCalledWith('"custom_extension"\n\njpg\ngif'); |
| 82 | + expect(writeText).toHaveBeenCalledWith('"custom_extension"\n\njpg\ngif'); |
| 83 | + }); |
68 | 84 |
|
69 | | - const { |
70 | | - label: labelSource, |
71 | | - iconType: iconTypeSource, |
72 | | - onClick: onClickSource, |
73 | | - } = buildCopyColumnValuesButton({ |
74 | | - columnId: '_source', |
| 85 | + it('should copy source column values to clipboard on click', async () => { |
| 86 | + const writeText = mockClipboard(); |
| 87 | + const { iconType, label, onClick } = buildCopyColumnValuesButton({ |
75 | 88 | columnDisplayName: 'Document', |
| 89 | + columnId: '_source', |
| 90 | + rowsCount: 3, |
76 | 91 | toastNotifications: servicesMock.toastNotifications, |
77 | 92 | valueToStringConverter: dataTableContextMock.valueToStringConverter, |
78 | | - rowsCount: 3, |
79 | 93 | }); |
80 | 94 |
|
81 | | - const wrapperSource = mountWithIntl( |
82 | | - <EuiButton iconType={iconTypeSource} onClick={onClickSource}> |
83 | | - {labelSource} |
| 95 | + renderWithI18n( |
| 96 | + <EuiButton iconType={iconType} onClick={onClick}> |
| 97 | + {label} |
84 | 98 | </EuiButton> |
85 | 99 | ); |
86 | 100 |
|
87 | | - await wrapperSource.find('button').simulate('click'); |
| 101 | + await userEvent.click(screen.getByRole('button', { name: 'Copy column' })); |
88 | 102 |
|
89 | 103 | // first row out of 3 rows does not have a value |
90 | | - expect(navigator.clipboard.writeText).toHaveBeenNthCalledWith( |
91 | | - 2, |
| 104 | + expect(writeText).toHaveBeenCalledWith( |
92 | 105 | 'Document\n{"bytes":20,"date":"2020-20-01T12:12:12.123","message":"test1","_index":"i","_score":1}\n' + |
93 | 106 | '{"date":"2020-20-01T12:12:12.124","extension":"jpg","name":"test2","_index":"i","_score":1}\n' + |
94 | 107 | '{"bytes":50,"date":"2020-20-01T12:12:12.124","extension":"gif","name":"test3","_index":"i","_score":1}' |
95 | 108 | ); |
96 | | - |
97 | | - Object.defineProperty(navigator, 'clipboard', { |
98 | | - value: originalClipboard, |
99 | | - }); |
100 | 109 | }); |
101 | 110 |
|
102 | | - it('should not copy to clipboard on click', () => { |
| 111 | + it('should not copy to clipboard on click', async () => { |
103 | 112 | const { label, iconType, onClick } = buildCopyColumnNameButton({ |
104 | 113 | columnDisplayName: 'test-field-name', |
105 | 114 | toastNotifications: servicesMock.toastNotifications, |
106 | 115 | }); |
107 | 116 | execCommandMock.mockImplementationOnce(() => false); |
| 117 | + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
108 | 118 |
|
109 | | - const wrapper = mountWithIntl( |
| 119 | + renderWithI18n( |
110 | 120 | <EuiButton iconType={iconType} onClick={onClick}> |
111 | 121 | {label} |
112 | 122 | </EuiButton> |
113 | 123 | ); |
114 | 124 |
|
115 | | - wrapper.find('button').simulate('click'); |
| 125 | + await userEvent.click(screen.getByRole('button', { name: 'Copy name' })); |
116 | 126 |
|
117 | 127 | expect(execCommandMock).toHaveBeenCalledWith('copy'); |
118 | 128 | expect(warn).toHaveBeenCalledWith('Unable to copy to clipboard.'); |
| 129 | + expect(servicesMock.toastNotifications.addWarning).toHaveBeenCalledWith({ |
| 130 | + title: 'Unable to copy to clipboard in this browser', |
| 131 | + }); |
119 | 132 | }); |
120 | 133 | }); |
0 commit comments