[Help wanted] Renovate unit tests against deprecated findDOMNode
API
#3200
Replies: 12 comments 23 replies
-
The additional commentThis comment is meant to provide hints about how to refactor the tests in specific scenarios. Rendering a React componentTo render a React component, old tests use getDOMNode(<Component />); New tests uses render(<Component />); Rendering multiple instances of a React component with different propsIn old tests, we do this by calling const instance1 = getDOMNode(<Component {...props1} />);
const instance2 = getDOMNode(<Component {...props2} />);
expect(instance1).some.thing;
expect(instance2).some.thing; In new tests, we use const { rerender } = render(<Component {...props1} />);
expect(...).some.thing;
rerender(<Component {...props2} />);
expect(...).some.thing; Getting the root element of rendered componentTo access the root element of rendered element, old tests uses the returned value of const instance = getDOMNode(<Component />); In new tests, most of the time we don't directly access the root element, but follow the guiding principles of React Testing Library. But if it does consider the element detail (e.g. testing it has expected class names), most of time you can access the root element via the const elementRef = render(<Component ref={elementRef} />);
expect(elementRef.current).some.thing; For those components where const { container } = render(<Component />)
expect(container.firstChild).some.thing; Testing class names, styles, attributesTo check that an element has expected class names, old tests cases might go const instance = getDOMNode(<Component />);
expect(instance.className).to.include('some-class');
// or
assert.include(instance.className, 'some-class'); New tests make use of the const { container } = render(<Component />);
expect(container.firstChild).to.have.class('some-class'); Same rule applies to checking element's style and attributes. Use Common test casesMost components has these 3 common test cases:
In old tests, they usually look like this: it('Should have a custom className', () => {
const instance = getDOMNode(<Component className="custom" />);
assert.include(instance.className, 'custom');
});
it('Should have a custom style', () => {
const fontSize = '12px';
const instance = getDOMNode(<Component style={{ fontSize }} />);
assert.equal(instance.style.fontSize, fontSize);
});
it('Should have a custom className prefix', () => {
const instance = getDOMNode(<Component classPrefix="custom-prefix" />);
assert.include(instance.className, 'custom-prefix');
}); In new tests, they are replaced by a test util testStandardProps(<Component />) |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
hi @SevenOutman, I'd like to help with components below (from Header -to Modal) during this weekend :)
|
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman
By the way, if you have the time, I would appreciate it if you could review these pull requests related to the previous task: |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
@SevenOutman |
Beta Was this translation helpful? Give feedback.
-
Question over here. What do you prefer, this way: |
Beta Was this translation helpful? Give feedback.
-
Hi community! I've tried to make a few tests but got some strange behavior. |
Beta Was this translation helpful? Give feedback.
-
Hi community, here's the first "good first issue" in rsuite repo (not an actual issue though), which means here're some tasks that we want to invite you to participate.
I think it's a good opportunity for you if you either:
What's the task?
TL;DR To update the way we used to render React components in test scripts - which is using
getDOMNode()
util fromtest/testUtils.js
- to using React Testing Libraryrender()
.Background
Old unit tests in rsuite repo uses a util function called
getDOMNode()
to render a React component and retrieves its root element. This util function does not have proper cleanup and uses the deprecatedfindDOMNode
API under the hood. Last year we adopted React Testing Library for new tests, but left old tests as is. But as we're updating the React version, it becomes necessary to drop the usage of deprecated APIs.What could I learn?
How do I contribute?
If you find yourself interested in contributing, please leave a comment below and tell me which components/modules you would like to work on.
Here's a (long) list of components/modules that are using
getDOMNode()
util in their unit tests and needs to be renovated.Expand
You would checkout a new branch based on latest
main
branch, finish your edit, then open a pull request targetingmain
branch.For your reference, I'll post an additional comment to provide several examples of how to refactor the tests in specific scenarios.
Additionally, I've already updated some modules (excluded from the list) in #3046. You could always refer to that PR to see real-world examples.
Beta Was this translation helpful? Give feedback.
All reactions