我有 React 应用程序,它允许用户注册,然后从主页登录我的应用程序(到仪表板组件)。现在我想测试我的应用程序。首先,我想测试我的登录和注销。
我对如何测试它的想法如下:
问题在于登录是使用 AWS Cognito 用户池实现的,当应呈现仪表板时,我收到来自仪表板组件的错误 const { attributes: { email, profile } } = await Auth.currentUserInfo();
attributes are undefined
。我尝试按照 this 的回答进行操作,但这不起作用。
我还认为我可以通过在测试中首先调用 Auth.SignIn() 来消除这种情况,但我收到了讨论 here 的错误 Auth: Native crypto module could not be used to get a secure random number.
。不幸的是,我无法解决任何一个问题。你能解决我的问题或建议其他更好的方法吗?
附注。我希望 await Auth.SignIn()
工作,因为我会在仪表板中的其他测试中使用它(我将呈现仪表板组件,然后从数据库发送/接收一些数据)。
顺便提一句。 Auth.SignIn()
函数在 React 应用中运行良好。
test("sign out user", async () => {
Amplify.configure(config);
// await Auth.signIn(TEST_USERNAME, TEST_PASSWORD); // want this to work
const history = useHistory();
render(<Dashboard />);
// sign out svg exists
const logoutSvg = screen.getByTestId("logoutSvg");
expect(logoutSvg).toBeInTheDocument();
// click it
await userEvent.click(logoutSvg);
// wait for disaperance of the dashboard
await waitFor(() => {
expect(logoutSvg).not.toBeInTheDocument()
})
// check if home page is shown
expect(screen.getByText(/Homepage/i)).toBeInTheDocument();
expect(screen.getByText(/Log in/i)).toBeInTheDocument();
// screen.debug();
})
PPS。使用 await waitFor(()=>{})
的正确方法是什么。我的方法不行。
我很乐意回答任何后续问题。非常感谢!