-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Modal controller issue with react testing library #2969
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
Comments
@rawadnasr thanks for reporting this issue! I am having trouble getting the same error logged locally, when I run the tests I receive:
However I noticed in your reproduction you are using Ionic 6, but your test setup is not calling import { setupIonicReact } from "@ionic/react";
setupIonicReact(); to Additionally, Jest defaults to the "test": "react-scripts test --env=jsdom --transformIgnorePatterns 'node_modules/(?!(@ionic/react|@ionic/react-router|@ionic/core|@stencil/core|ionicons)/)'", Lastly, you appear to be on an older version of both Node and NPM. I would recommend updating to:
Can you try this and let me know if you continue to experience the same error? |
Ok, I debugged this a little further and the issue is that we do not mock the implementation of You can do this manually in your implementation. For instance here is an example test that mocks the hook implementation and checks that the component is passed to the hook & that the present method is invoked when the button is clicked: import { fireEvent, render, screen } from "@testing-library/react";
import Home, { ModalContent } from "./Home";
const mockPresent = jest.fn();
const mockUseIonModal = jest.fn();
jest.mock("@ionic/react", () => {
const rest = jest.requireActual("@ionic/react");
return {
...rest,
useIonModal: (component: any) => {
mockUseIonModal(component);
return [mockPresent];
},
};
});
test("should present ModalContent when button is clicked", async () => {
render(<Home />);
fireEvent.click(screen.getByText("Open"));
expect(mockUseIonModal).toHaveBeenCalledWith(ModalContent);
expect(mockPresent).toHaveBeenCalled();
}); Can you try this out and let me know if you run into any issues? |
Thank you @sean-perkins for your response. |
Ionic version:
[x] 6.0
I'm submitting a ...
[x] bug report
[ ] feature request
Current behavior:
I'm working with react ionic library to test a button click that present Ion modal programatically (using modal controller). The issue is that the content of Ion Modal is not rendered and i can't reach any element in the modal, while when using inline Modals it is working normally.
Expected behavior:
Ion modal content should be rendered normally and I should be able to get any element from the modal
Steps to reproduce:
git clone https://github.com/rawadnasr/Modal-Controller-Issue.git
npm install
npm test
Related code:
Other information:
Ionic info:
The text was updated successfully, but these errors were encountered: