我正在尝试测试我的锚标签。一旦我点击它,我想看看它是否window.location.href是我所期望的。
我尝试渲染锚点,单击它,然后进行测试window.location.href:
test('should navigate to ... when link is clicked', () => {
const { getByText } = render(<a href="https://test.com">Click Me</a>);
const link = getByText('Click Me');
fireEvent.click(link);
expect(window.location.href).toBe("https://www.test.com/");
});
Run Code Online (Sandbox Code Playgroud)
我期待测试通过,但 window.location.href 只是"http://localhost/"意味着它没有因任何原因得到更新。我什至尝试用 包装我的期望await wait,但这也不起作用。我找不到关于使用react-testing-library. 也许有比我正在做的更好的方法来测试它们。???
I am trying to test that a form submits when the user presses the "Enter" key. I have a passing test for when pressing the Submit button, but I also want to be sure the form submits with the keyboard (convenience and a11y).
test("should submit when pressing enter", () => {
const handleSubmit = jest.fn();
const { getByLabelText } = render(<App handleSubmit={handleSubmit} />);
const input = getByLabelText("Name:");
fireEvent.change(input, { target: { value: "abc" } });
fireEvent.keyPress(input, { key: …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试当用户将文本输入到 a 时input, a会从button变为。它显然可以在浏览器中运行,但我无法通过测试。我正在使用和。但是,如果我使用本机而不是s ,测试就会通过。disabledenabledredux-formreact-testing-libraryinputredux-formField
我用最少的代码创建了一个存储库来复制问题。
我建立一个聊天应用程序,并会使用喜欢写集成测试react-testing-library并不能弄清楚如何嘲笑socket.io-client的socket.on,socket.emit等等。
我尝试按照这篇文章并尝试使用mock-socket.io和socket.io-mock都没有运气。
这是我要测试的组件:
import React, { useEffect, useState } from 'react';
import io from 'socket.io-client';
import 'dotenv/config';
import ChatInput from './ChatInput';
import Messages from './Messages';
function App() {
const [messages, setMessages] = useState([]);
const port = process.env.REACT_APP_SERVER_PORT;
const socket = io(`http://localhost:${port}`);
useEffect(() => {
socket
.emit('app:load', messageData => {
setMessages(messages => [...messages, ...messageData]);
})
.on('message:new', newMessage => {
setMessages(messages => [...messages, newMessage]);
});
}, []);
const …Run Code Online (Sandbox Code Playgroud) reactjs ×4
jestjs ×3
forms ×2
anchor ×1
events ×1
href ×1
react-hooks ×1
redux ×1
redux-form ×1
socket.io ×1