小编jay*_*pee的帖子

如何使用 react-testing-library 测试锚点的 href

我正在尝试测试我的锚标签。一旦我点击它,我想看看它是否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. 也许有比我正在做的更好的方法来测试它们。???

anchor href reactjs jestjs react-testing-library

50
推荐指数
8
解决办法
4万
查看次数

在 react-testing-library 中按 Enter 提交表单不起作用

描述:

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).

Code:

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)

forms events reactjs jestjs react-testing-library

12
推荐指数
2
解决办法
8226
查看次数

在 redux-form 上使用 React-testing-library 时,表单输入不会改变

我正在尝试测试当用户将文本输入到 a 时input, a会从button变为。它显然可以在浏览器中运行,但我无法通过测试。我正在使用和。但是,如果我使用本机而不是s ,测试就会通过。disabledenabledredux-formreact-testing-libraryinputredux-formField

我用最少的代码创建了一个存储库来复制问题。

forms reactjs redux redux-form react-testing-library

2
推荐指数
1
解决办法
2281
查看次数

如何使用 jest/react-testing-library 模拟 socket.io-client

我建立一个聊天应用程序,并会使用喜欢写集成测试react-testing-library并不能弄清楚如何嘲笑socket.io-clientsocket.onsocket.emit等等。

我尝试按照这篇文章并尝试使用mock-socket.iosocket.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)

socket.io reactjs jestjs react-testing-library react-hooks

1
推荐指数
1
解决办法
3458
查看次数