简体   繁体   English

如何使用 Jest 和 React-Testing-Library 模拟和测试 scrollBy?

[英]How to mock and test scrollBy with Jest and React-Testing-Library?

Given the following component:给定以下组件:

import * as React from "react";
import "./styles.css";

export default function App() {
  const scrollContainerRef = React.useRef<HTMLDivElement | null>(null);

  const handleClick = () => {
    scrollContainerRef?.current?.scrollBy({ top: 0, left: 100 });
  };

  return (
    <div aria-label="wrapper" ref={scrollContainerRef}>
      <button onClick={handleClick}>click</button>
    </div>
  );
}

How do I write a test using Jest and React Testing library to check that when the button is clicked, scrollBy is triggered on the wrapper?如何使用 Jest 和 React 测试库编写测试以检查单击按钮时是否在包装器上触发了scrollBy

I have tried the following and it doesn't seem to be working:我尝试了以下方法,但似乎不起作用:

test('Clicking on button should trigger scroll',() => {
  const myMock = jest.fn();
  Object.defineProperty(HTMLElement.prototype, 'scrollBy', {
    configurable: true,
    value: myMock(),
  })
  render(<MyComponent />)
  fireEvent.click(screen.getByText(/click/i))
  expect(myMock).toHaveBeenCalledWith({top: 0, left: 100})
})

Since jsdom does NOT implements Element.scrollBy() method, see PR .由于jsdom没有实现Element.scrollBy()方法,请参阅PR We can create a mocked ref object with getter and setter to intercept React's assignment process to ref.current , and install spy or add mock in the process.我们可以用getter和setter创建一个mocked ref对象来拦截React对ref.current的赋值过程,并在这个过程中安装spy或者添加mock。

Eg例如

App.tsx : App.tsx

import * as React from 'react';

export default function App() {
  const scrollContainerRef = React.useRef<HTMLDivElement | null>(null);

  const handleClick = () => {
    scrollContainerRef?.current?.scrollBy({ top: 0, left: 100 });
  };

  return (
    <div aria-label="wrapper" ref={scrollContainerRef}>
      <button onClick={handleClick}>click</button>
    </div>
  );
}

App.test.tsx : App.test.tsx :

import React, { useRef } from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { mocked } from 'ts-jest/utils';
import App from './App';

jest.mock('react', () => {
  return {
    ...jest.requireActual<typeof React>('react'),
    useRef: jest.fn(),
  };
});

const useMockRef = mocked(useRef);

describe('63702104', () => {
  afterAll(() => {
    jest.resetAllMocks();
  });
  test('should pass', () => {
    const ref = { current: {} };
    const mScrollBy = jest.fn();
    Object.defineProperty(ref, 'current', {
      set(_current) {
        if (_current) {
          _current.scrollBy = mScrollBy;
        }
        this._current = _current;
      },
      get() {
        return this._current;
      },
    });
    useMockRef.mockReturnValueOnce(ref);
    render(<App />);
    fireEvent.click(screen.getByText(/click/i));
    expect(mScrollBy).toBeCalledWith({ top: 0, left: 100 });
  });
});

test result:测试结果:

 PASS  examples/63702104/App.test.tsx (9.436 s)
  63702104
    ✓ should pass (33 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |       75 |     100 |     100 |                   
 App.tsx  |     100 |       75 |     100 |     100 | 7                 
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.21 s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用 jest 和 react-testing-library 测试 react-toastify - How to test react-toastify with jest and react-testing-library 如何使用 Jest 和 react-testing-library 测试 react-dropzone? - How to test react-dropzone with Jest and react-testing-library? 带有 jest 和 react-testing-library 的测试方法 - test method with jest and react-testing-library 如何使用 Jest 和 react-testing-library 测试 useRef? - How to test useRef with Jest and react-testing-library? 如何使用 Jest 和 react-testing-library 测试 Websocket 客户端 - How to test Websocket Client with Jest and react-testing-library 如何使用 react-testing-library 和 jest 测试链接 - How to test link using react-testing-library and jest 如何使用 jest、react-testing-library 在 React 中模拟元素上的点击事件 - How to mock a click event on an element in React using jest , react-testing-library 使用React,Jest,React-Testing-Library测试失败的案例 - Test failing cases with React, Jest, React-Testing-Library 使用 React、Jest、React-Testing-Library 测试失败案例 - Test failing case with React, Jest, React-Testing-Library 如何在 jest 和 react-testing-library 中模拟 document.getElementsByClassName() 以进行代码覆盖? - How do I mock document.getElementsByClassName() in jest and react-testing-library for the code-coverage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM