Essentially, I'm trying to test if my modal opens when my button is clicked & I'm a noob with Jest & Enzyme.
I am hitting an issue accessing the props on the button & I'm not sure if it's because it's nested inside of a third party package or if I'm not importing it correctly into my test. Please see my condensed(ish) code below as I wasn't about to recreate the code on CodePen..
DataTable.jsx
const UploadDownloadComponent = ({ handleOpen }) => (
<UploadDownloadButtonContainer>
<PrimaryButton
id="bulk-add-button"
onClick={handleOpen} //this is what I need to access
>
Bulk Add Members
</PrimaryButton>
<SecondaryButton id="email-csv-button">
</SecondaryButton>
</UploadDownloadButtonContainer>
);
//beginning of data table component
export const Table = () => {
const [bulkUpload, setBulkUpload] = useState(false);
//upload modal
const openUpload = () => {
setBulkUpload(true);
};
const closeUpload = () => {
setBulkUpload(false);
};
//query container
const subHeaderComponentMemo = useMemo(() => {
{*/ other code /*}
return (
<div>
<UploadDownloadComponent
handleOpen={openUpload}
bulkUpload={bulkUpload}
/>
</div>
);
}, []);
return (
<React.Fragment>
<DataTable
{*/ bunch of other things unrelated /*}
subHeaderComponent={subHeaderComponentMemo}
/>
<UploadModal closeModal={closeUpload} open={bulkUpload} />
</React.Fragment>
);
};
DataTable.test.js
import React from "react";
import { configure, mount, shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import { Table } from "../components/MemberView/DataTable";
import { UploadModal } from "../components/MemberView/UploadModal";
import Modal from "react-modal";
configure({ adapter: new Adapter() });
describe("<Table />", () => {
const wrapper = mount(<Table />);
//using .upDate() is the only way I can get this test to pass
it("should have upload button", () => {
const uploadButton = wrapper.find("#bulk-add-button").update();
expect(uploadButton).toHaveLength(1);
});
//this passes
it("renders Upload Modal", () => {
const upModal = shallow(<UploadModal />);
expect(upModal.find(Modal)).toHaveLength(1);
});
it("opens Upload Modal when state is changed", () => {
const modal = mount(<UploadModal />);
expect(modal.find(Modal).prop("isOpen")).toBe(false);
const uploadButton = wrapper.find("#bulk-add-button").update();
expect(uploadButton.length).toEqual(1);
//this is where my test fails as it cannot simulate click on uploadButton
uploadButton.simulate("click");
//if I change it to:
wrapper.find("#bulk-add-button").simulate("click')
//my error message says it expected 1 Node. ) found instead.
//I don't make it this far
expect(modal.find(Modal).prop("isOpen")).toBe(true);
});
});
I am also using Hooks if that makes any difference...
Any and all help/advice welcome!
Thanks