Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate enzyme Matrix tests to React Testing Library #6762

Merged
merged 12 commits into from
Aug 3, 2022
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import {render, mount} from 'enzyme';
import {fireEvent, render, screen} from '@testing-library/react';
import React from 'react';
import Matrix from '../Matrix';
import Row from '../Row';
Expand All @@ -24,8 +24,7 @@ jest.mock('../../../utils/Translator', () => ({

test('Render the Matrix component', () => {
const handleChange = jest.fn();

expect(render(
const {container} = render(
<Matrix className="test" onChange={handleChange}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand All @@ -40,7 +39,9 @@ test('Render the Matrix component', () => {
<Item icon="su-plus" name="edit" />
</Row>
</Matrix>
)).toMatchSnapshot();
);

expect(container).toMatchSnapshot();
});

test('Render the Matrix component with values', () => {
Expand All @@ -60,7 +61,7 @@ test('Render the Matrix component with values', () => {
},
};

expect(render(
const {container} = render(
<Matrix onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand All @@ -75,7 +76,9 @@ test('Render the Matrix component with values', () => {
<Item icon="su-plus" name="edit" />
</Row>
</Matrix>
)).toMatchSnapshot();
);

expect(container).toMatchSnapshot();
});

test('Render the Matrix component with values in disabled state', () => {
Expand All @@ -95,7 +98,7 @@ test('Render the Matrix component with values in disabled state', () => {
},
};

expect(render(
const {container} = render(
<Matrix disabled={true} onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand All @@ -110,7 +113,9 @@ test('Render the Matrix component with values in disabled state', () => {
<Item icon="su-plus" name="edit" />
</Row>
</Matrix>
)).toMatchSnapshot();
);

expect(container).toMatchSnapshot();
});

test('Changing a value should call onChange ', () => {
Expand All @@ -130,7 +135,7 @@ test('Changing a value should call onChange ', () => {
},
};

const matrix = mount(
render(
<Matrix onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand Down Expand Up @@ -162,7 +167,9 @@ test('Changing a value should call onChange ', () => {
},
};

matrix.find(Item).at(3).simulate('click');
// eslint-disable-next-line testing-library/no-node-access
const item = screen.queryAllByLabelText('su-pen')[1].parentElement;
fireEvent.click(item);
b3h3m0th marked this conversation as resolved.
Show resolved Hide resolved
expect(handleChange).toHaveBeenCalledWith(expectedValues);
});

Expand All @@ -183,7 +190,7 @@ test('Deactivate all button should call onChange', () => {
},
};

const matrix = mount(
render(
<Matrix onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand Down Expand Up @@ -215,7 +222,8 @@ test('Deactivate all button should call onChange', () => {
},
};

matrix.find('.rowButton').at(0).simulate('click');
const disableRowButton = screen.queryAllByText('Deactivate all')[0];
fireEvent.click(disableRowButton);
expect(handleChange).toHaveBeenCalledWith(expectedValues);
});

Expand All @@ -236,7 +244,7 @@ test('Activate all button should call onChange', () => {
},
};

const matrix = mount(
render(
<Matrix onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand Down Expand Up @@ -268,7 +276,8 @@ test('Activate all button should call onChange', () => {
},
};

matrix.find('.rowButton').at(0).simulate('click');
const activateRowButton = screen.queryAllByText('Activate all')[0];
fireEvent.click(activateRowButton);
expect(handleChange).toHaveBeenCalledWith(expectedValues);
});

Expand All @@ -285,7 +294,7 @@ test('Activate all button should call onChange with all values, even when the va
},
};

const matrix = mount(
render(
<Matrix onChange={handleChange} values={values}>
<Row name="global.articles" title="articles">
<Item icon="su-pen" name="view" />
Expand Down Expand Up @@ -317,6 +326,7 @@ test('Activate all button should call onChange with all values, even when the va
},
};

matrix.find('.rowButton').at(2).simulate('click');
const activateRowButton = screen.queryAllByText('Activate all')[1];
fireEvent.click(activateRowButton);
expect(handleChange).toHaveBeenCalledWith(expectedValues);
});