它对我来说很好用。无法重现您的问题。例如
index.tsx:
import React, { Component } from 'react';
interface PageProps {
foo: string;
disabled?: boolean;
}
export default class Page extends Component<PageProps> {
UNSAFE_componentWillReceiveProps(nextProps: PageProps) {
console.log('this.props: ', this.props);
console.log('nextProps: ', nextProps);
}
render() {
return <button disabled={this.props.disabled}>Done</button>;
}
}
index.test.tsx:
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import Page from './';
describe('58031892', () => {
test('should pass', () => {
const { rerender, getByText } = render(<Page foo="a" disabled />);
const button = getByText('Done');
expect(button).toBeDisabled();
rerender(<Page foo="b" />);
expect(button).toBeEnabled();
});
});
测试结果:
PASS examples/58031892/index.test.tsx (12.803 s)
58031892
✓ should pass (52 ms)
console.log
this.props: { foo: 'a', disabled: true }
at Page.UNSAFE_componentWillReceiveProps (examples/58031892/index.tsx:9:13)
console.log
nextProps: { foo: 'b' }
at Page.UNSAFE_componentWillReceiveProps (examples/58031892/index.tsx:10:13)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.tsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.505 s
包版本:
"@testing-library/react": "^11.2.2",
"@testing-library/jest-dom": "^5.11.6",
"jest": "^26.6.3",
"react": "^16.14.0"