Skip to content

Commit 8689a58

Browse files
unit tests for the bulk edit columns
1 parent 1383ca6 commit 8689a58

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

desktop/core/src/desktop/js/apps/newimporter/ImporterFilePreview/EditColumns/EditColumnsModal.test.tsx

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,129 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
//Will write tests after first review
17+
import React from 'react';
18+
import { render, screen, waitFor } from '@testing-library/react';
19+
import userEvent from '@testing-library/user-event';
20+
import '@testing-library/jest-dom';
21+
import EditColumnsModal, { Column } from './EditColumnsModal';
22+
23+
jest.mock('cuix/dist/components/Modal', () => {
24+
const Modal = ({ children, title, className, onOk, onCancel, okText, cancelText }: any) => (
25+
<div data-testid="modal" className={className}>
26+
<h2>{title}</h2>
27+
{children}
28+
<button onClick={onCancel}>{cancelText || 'Cancel'}</button>
29+
<button onClick={onOk}>{okText || 'Done'}</button>
30+
</div>
31+
);
32+
return Modal;
33+
});
34+
jest.mock('cuix/dist/components/Table', () => ({ columns, dataSource }: any) => (
35+
<table>
36+
<tbody>
37+
{dataSource.map((row: any, idx: number) => (
38+
<tr key={row.key}>
39+
{columns.map((col: any, colIdx: number) => (
40+
<td key={colIdx}>
41+
{col.render ? col.render(row[col.dataIndex], row, idx) : row[col.dataIndex]}
42+
</td>
43+
))}
44+
</tr>
45+
))}
46+
</tbody>
47+
</table>
48+
));
49+
jest.mock('cuix/dist/components/Input', () => (props: any) => <input {...props} />);
50+
jest.mock('cuix/dist/components/Select', () => {
51+
const Select = ({ children, value, onChange, ...props }: any) => (
52+
<select {...props} value={value} onChange={e => onChange(e.target.value)}>
53+
{children}
54+
</select>
55+
);
56+
Select.Option = ({ children, ...props }: any) => <option {...props}>{children}</option>;
57+
return Select;
58+
});
59+
jest.mock(
60+
'../../../../reactComponents/LoadingErrorWrapper/LoadingErrorWrapper',
61+
() =>
62+
({ children }: any) => <>{children}</>
63+
);
64+
65+
jest.mock('../../../../utils/hooks/useLoadData/useLoadData', () => () => ({
66+
data: ['string', 'int', 'float'],
67+
loading: false,
68+
error: null
69+
}));
70+
71+
describe('EditColumnsModal', () => {
72+
const columns: Column[] = [
73+
{ title: 'col1', dataIndex: 'col1', type: 'string', comment: 'comment1' },
74+
{ title: 'col2', dataIndex: 'col2', type: 'int', comment: 'comment2' }
75+
];
76+
const sample = [{ col1: 'val1', col2: 42 }];
77+
78+
test('lists existing modal columns as expected', async () => {
79+
render(
80+
<EditColumnsModal
81+
isOpen={true}
82+
closeModal={jest.fn()}
83+
columns={columns}
84+
setColumns={jest.fn()}
85+
sample={sample}
86+
/>
87+
);
88+
89+
expect(screen.getByTestId('modal')).toBeInTheDocument();
90+
91+
expect(screen.getByDisplayValue('col1')).toBeInTheDocument();
92+
expect(screen.getByDisplayValue('col2')).toBeInTheDocument();
93+
expect(screen.getByDisplayValue('string')).toBeInTheDocument();
94+
expect(screen.getByDisplayValue('int')).toBeInTheDocument();
95+
expect(screen.getByDisplayValue('val1')).toBeInTheDocument();
96+
expect(screen.getByDisplayValue('42')).toBeInTheDocument();
97+
expect(screen.getByDisplayValue('comment1')).toBeInTheDocument();
98+
expect(screen.getByDisplayValue('comment2')).toBeInTheDocument();
99+
});
100+
101+
test('Save should call setColumns with modified data from the table', async () => {
102+
const setColumns = jest.fn();
103+
const closeModal = jest.fn();
104+
render(
105+
<EditColumnsModal
106+
isOpen={true}
107+
closeModal={closeModal}
108+
columns={columns}
109+
setColumns={setColumns}
110+
sample={sample}
111+
/>
112+
);
113+
114+
const user = userEvent.setup();
115+
116+
// Change the name of the first column
117+
const nameInputs = screen.getAllByPlaceholderText('Column Name');
118+
await user.clear(nameInputs[0]);
119+
await user.type(nameInputs[0], 'newCol1');
120+
121+
// Change the type of the second column
122+
const typeSelects = screen.getAllByDisplayValue('int');
123+
await user.selectOptions(typeSelects[0], 'float');
124+
125+
// Change the comment of the first column
126+
const commentInputs = screen.getAllByDisplayValue('comment1');
127+
await user.clear(commentInputs[0]);
128+
await user.type(commentInputs[0], 'new comment');
129+
130+
// Click Done (ok) button
131+
const doneButton = screen.getByText('Done');
132+
await user.click(doneButton);
133+
134+
await waitFor(() => {
135+
expect(setColumns).toHaveBeenCalledWith([
136+
{ ...columns[0], title: 'newCol1', type: 'string', comment: 'new comment' },
137+
{ ...columns[1], title: 'col2', type: 'float', comment: 'comment2' }
138+
]);
139+
expect(closeModal).toHaveBeenCalled();
140+
});
141+
});
142+
});

0 commit comments

Comments
 (0)