小编Ant*_*iev的帖子

如何在 React 功能组件中模拟 ipcRenderer.on 函数

我正在编写一个 Electron 应用程序,使用 React 作为前端,使用 JEST + React 测试库来运行测试。我在模块中有以下简化代码:

import React from 'react';
import { ipcRenderer } from 'electron';
import Paper from '@material-ui/core/Paper';
import LinearProgress from '@material-ui/core/LinearProgress';


const AccountCheckModule = () => {

  const [listingsCount, setListingsCount] = React.useState(0);

  React.useEffect(() => {
    ipcRenderer.on('count-listings', (event, count) => {
      setListingsCount(count);
    });

    ipcRenderer.send('count-listings');

    // Cleanup the listener events so that memory leaks are avoided.
    return function cleanup() {
      ipcRenderer.removeAllListeners('count-listings');
    };
  }, []);

  return (
    <Paper elevation={2} data-testid="paper">
        <p
          className={classes.listingsNumberTracker}
          data-testid="free-listings-counter"
        >
          Free listings: {listingsCount}/100 …
Run Code Online (Sandbox Code Playgroud)

mocking reactjs jestjs electron react-testing-library

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