I am still new to unit testing with jest and enzyme, I have a data-test-id and I am trying to make a unit test using that. Yet, I get an error saying that the data-test-id was not found.
I have the following
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, mount, shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';
import ConfirmationModal from '../src/components/GlobalForm/ConfirmationModal/ConfirmationModal';
configure({ adapter: new Adapter() });
describe('ConfirmationModal', () => {
it('should be defined', () => {
expect(ConfirmationModal).toBeDefined();
});
it('should render correctly', () => {
const tree = mount(<ConfirmationModal />);
expect(shallowToJson(tree)).toMatchSnapshot();
});
it('should have a cancel button', () => {
const wrapper = shallow(<ConfirmationModal />);
const ConfirmationModalComponent = wrapper.find("[data-test-id='cancel-submit-btn']");
expect(ConfirmationModalComponent.length).toBe(1);
});
});
Even my snapshot test shows that I have this data-test-id
exports[`ConfirmationModal should render correctly 1`] = `
<confirmationModal>
<Styled(div)>
<div
className="css-d2ogfy"
>
<Styled(div)>
<div
className="css-ywnjte"
>
Confirm your submission
</div>
</Styled(div)>
<Styled(p)>
<p
className="css-1v1a1rr"
>
The submission of your changes will directly affect the call center with which they are assigned.
</p>
</Styled(p)>
<Styled(p)>
<p
className="css-1v1a1rr"
>
Are you sure that you want to proceed with these changes?
</p>
</Styled(p)>
<Styled(div)>
<div
className="css-wqsxq7"
>
<Button
dataTestId="cancel-submit-btn"
name="Cancel"
>
<Styled(button)
data-test-id="cancel-submit-btn"
>
<button
className="css-2ba12r"
data-test-id="cancel-submit-btn"
>
Cancel
</button>
</Styled(button)>
</Button>
<Button
dataTestId="confirm-submit-btn"
name="Confirm"
>
<Styled(button)
data-test-id="confirm-submit-btn"
>
<button
className="css-2ba12r"
data-test-id="confirm-submit-btn"
>
Confirm
</button>
</Styled(button)>
</Button>
</div>
</Styled(div)>
</div>
</Styled(div)>
</confirmationModal>
`;
but my test results with the error:
ConfirmationModal › should have a cancel button
expect(received).toBe(expected) // Object.is equality Expected: 1 Received: 0
How???? it's right there...