taro icon indicating copy to clipboard operation
taro copied to clipboard

单元测试怎么触发component重渲染?

Open masteroy opened this issue 7 years ago • 4 comments

请问在单元测试中怎么触发component重渲染?期望能够在组件状态更新后去match snapshot。

let component = renderIntoDocument(<TestComponent />);
const searchInput = findComponetByType(component, 'SearchInput')[0];
searchInput.props.onToggleAutoComplete(true, 'test');
// 此时component内部的state已经改变,想要强制刷新,确保rerender的dom正确
expect(component).toMatchSnapshot();

masteroy avatar Sep 09 '19 05:09 masteroy

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

taro-bot avatar Sep 09 '19 05:09 taro-bot

let inst
class A extends Component {
	constructor (props) {
		super(props)
		inst = this
	}

	render (...)
}

// forceUpdate 就是重新渲染
inst.forceUpdate()

yuche avatar Sep 09 '19 05:09 yuche

@yuche 我找到了一个简单点的方法

const component = renderIntoDocument(<TestComponent />);
const searchInput = findComponentsByType(component, 'SearchInput')[0];
searchInput.props.onToggleAutoComplete(true, 'test');
const result = Nerv.renderComponent(component);
expect(result).toMatchSnapshot();

另外,官方关于单元测试的参考文档很少,测试中遇到的各种需求很难被满足,都需要翻看源码看有什么趁手的工具。 我之前提的一个issue: 1.3.13 Component使用多个useEffect单元测试报错 有计划去修复吗?

masteroy avatar Sep 09 '19 06:09 masteroy

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

taro-bot avatar Sep 09 '19 06:09 taro-bot