You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// tester.test.tsximportReactfrom"react";import{render,unmountComponentAtNode}from"react-dom";importTesterfrom"./tester";letcontainer: any=null;beforeEach(()=>{container=document.createElement("div");document.body.appendChild(container);});afterEach(()=>{unmountComponentAtNode(container);container.remove();container=null;});it("the method should change the message",()=>{// @ts-ignoreconst{ changeMessage }=render(<Tester/>,container);changeMessage("new message");constmessage=document.querySelector(".message");expect(message.innerHTML).toBe("new message");});
This works just fine when using the regular render function from react-dom.
But when I need to use the render function from @testing-library/react (because I need some extra features like the wrapper option etc.) I can no longer access the methods from my class component. The following code doesn't work as expected, because I can't get the method like I was able to with the regular render function from react-dom.
// tester.test.tsx
import React from "react";
import { unmountComponentAtNode } from "react-dom";
import { render } from '@testing-library/react';
import Tester from "./tester";
let container: any = null;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});
it("the method should change the message", () => {
// @ts-ignore
const { changeMessage } = render(<Tester />, { container });
changeMessage("new message");
const message = document.querySelector(".message");
expect(message.innerHTML).toBe("new message");
});
This doesn't work because changeMessage is now undefined.
Problem description:
There are many cases where we need to access the methods of components for unit testing. If this is supposed to be seen as a replacement for the render of react-dom (is it? 🤔), then it would be expected behavior to be able to access methods from components. This would allow for cases like this where we need to get a method from a child component, for instance:
it("the method should change the message",()=>{// @ts-ignoreconst{ changeMessage }=render(<Tester/>,{container,wrapper: Router,});changeMessage("new message");constmessage=document.querySelector(".message");expect(message.innerHTML).toBe("new message");});
Suggested solution:
Make the methods accessible as they are with render from react-dom
The text was updated successfully, but these errors were encountered:
The point of @testing-library/ is to not test implementation details. If this component is used at an application level rethink how this state change is triggered in the UI and replay these interactions in your test.
If this is a test at a library level imperative methods might be part of your public API. In this case you can (just like your library consumer) attach a ref:
In any case, I don't see the need for exposing imperative handles from @testing-library/react since it's impossible to detect safely if a component can hold a ref or not.
Ok, I can see now how this is against the principles, that @testing-library/react is guided by. Instead of trying to get and test the individual internal methods (which would be private/inaccessible in other cases anyhow) I am going to try to test things as they are used in the app by mocking other functions etc, kind of like this. Sorry that this was maybe inappropriate to file as a bug report. Seems that this was not expected behavior anyways.
react-testing-library
version: 9.1.4react
version: 16.9.0node
version: 12.7.0npm
(oryarn
) version: 6.11.3Relevant code or config:
Let's say I'm trying to test a component like this:
With a test like this:
This works just fine when using the regular
render
function fromreact-dom
.But when I need to use the
render
function from@testing-library/react
(because I need some extra features like thewrapper
option etc.) I can no longer access the methods from my class component. The following code doesn't work as expected, because I can't get the method like I was able to with the regularrender
function fromreact-dom
.This doesn't work because
changeMessage
is nowundefined
.Problem description:
There are many cases where we need to access the methods of components for unit testing. If this is supposed to be seen as a replacement for the
render
ofreact-dom
(is it? 🤔), then it would be expected behavior to be able to access methods from components. This would allow for cases like this where we need to get a method from a child component, for instance:Suggested solution:
Make the methods accessible as they are with
render
fromreact-dom
The text was updated successfully, but these errors were encountered: