我正在尝试测试一个 React 组件,这个组件具有当用户登录时会更改的属性。
由于用户只能通过收到的魔法链接进行登录,因此我无法从 testcafe 测试套件本身更改登录属性。
在下面给出的示例组件中,我该如何测试“Logout”按钮?
TestCafe 将始终将 isLoggedIn 属性视为 false,并且在测试过程中不允许将组件属性设置为某些自定义值。
header.js
import React from 'react';
const Header = ({ isLoggedIn }) => (
<div>
{isLoggedIn ? (
<h1>text unreachable from testcafe</h1>
<button onClick={logout}>Logout</button>
) : (
<button onClick={login}>Login</button>
)}
</div>
);
export default Header;
test-cafe.js
import { ReactSelector } from 'testcafe-react-selectors';
import { waitForReact } from 'testcafe-react-selectors';
fixture`HEADER`
.page('http://localhost:3000')
.beforeEach(async () => {
await waitForReact();
});
test('Page should load the header', async (t) => {
const App = ReactSelector('App').withProps({ isLoggedIn: true });
const actual = App.find('h1').innerText;
const expected = 'text unreachable from testcafe';
await t.expect(actual).eql(expected);
});