我使用 create-react-app 创建了一个反应应用程序,它产生了一个反应应用程序的准系统,它非常好! 我现在想更改页面的标题(在索引.html中(,然后对此进行测试。我正在使用酶来测试单个组件,这也很棒,但它只提供从组件生成的 html。 如何测试页面/dom,特别是页面的标题?
it('renders App', () => {
const wrapper = mount(<App />);
const title = 'test'
console.log(global.window.document.title)
expect(wrapper.contains(title)).toEqual(true);
});
global.window.document.title 为空
-
您可以在组件/应用程序中设置标题.js以
render()
或componentDidMount()
设置为document.title = 'titlename'
并在此处检查该标题
expect(global.window.document.title).toBe('titlename')
.为我工作! -
尝试使用
react-helmet
。然后检查wrapper.find(Helmet).prop("title")).toEqual('titlename');