等待酶的固定剂酶

发布于 2025-02-04 19:48:44 字数 718 浏览 2 评论 0原文

下面我正在尝试进行使用component.setProps的测试,如何等待useffect在此之后运行< meowcomponent>之后setProps

const history = createMemoryHistory();

history.push('/3');

const fetch = jest.fn()

const Component = mount(
  <Router history={history}>
    <Route path="/meow/:number">
      <MeowComponent fetch={fetch} />
    </Route>
  </Router>
);

// This is the use case where component initially mounts
expect(fetch).toHaveBeenNthCalledWith(1, '3', false, false);

Component.setProps({ number: '2' });

// this fails because we haven't awaited the changes..
expect(fetch).toHaveBeenNthCalledWith(2, '2', false, false);

Below I'm trying to have a test that uses Component.setProps, how to I await the useEffect running within <MeowComponent> after this setProps?

const history = createMemoryHistory();

history.push('/3');

const fetch = jest.fn()

const Component = mount(
  <Router history={history}>
    <Route path="/meow/:number">
      <MeowComponent fetch={fetch} />
    </Route>
  </Router>
);

// This is the use case where component initially mounts
expect(fetch).toHaveBeenNthCalledWith(1, '3', false, false);

Component.setProps({ number: '2' });

// this fails because we haven't awaited the changes..
expect(fetch).toHaveBeenNthCalledWith(2, '2', false, false);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2025-02-11 19:48:44

尝试:

import { mount } from 'enzyme';
import { createMemoryHistory } from 'history';
import React, { useEffect } from 'react';
import { Route, Router, useParams } from 'react-router-dom';

function flushPromises() {
  return new Promise((resolve) => setTimeout(resolve, 0));
}

const MeowComponent = ({ fetch }) => {
  const { number } = useParams<{ number: string }>();
  console.log('number', number);
  useEffect(() => {
    fetch(number, false, false);
  }, [number]);
  return null;
};

describe('72495434', () => {
  test('should pass', async () => {
    const fetch = jest.fn();
    const history = createMemoryHistory();
    history.push('/meow/3');
    mount(
      <Router history={history}>
        <Route path="/meow/:number">
          <MeowComponent fetch={fetch} />
        </Route>
      </Router>
    );
    expect(fetch).toHaveBeenNthCalledWith(1, '3', false, false);
    history.push('/meow/2');
    await flushPromises();
    expect(fetch).toHaveBeenNthCalledWith(2, '2', false, false);
  });
});

测试结果:

 PASS  stackoverflow/72495434/index.test.tsx (10.054 s)
  72495434
    ✓ should pass (62 ms)

  console.log
    number 3

      at MeowComponent (stackoverflow/72495434/index.test.tsx:12:11)

  console.log
    number 2

      at MeowComponent (stackoverflow/72495434/index.test.tsx:12:11)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.512 s, estimated 11 s

Try:

import { mount } from 'enzyme';
import { createMemoryHistory } from 'history';
import React, { useEffect } from 'react';
import { Route, Router, useParams } from 'react-router-dom';

function flushPromises() {
  return new Promise((resolve) => setTimeout(resolve, 0));
}

const MeowComponent = ({ fetch }) => {
  const { number } = useParams<{ number: string }>();
  console.log('number', number);
  useEffect(() => {
    fetch(number, false, false);
  }, [number]);
  return null;
};

describe('72495434', () => {
  test('should pass', async () => {
    const fetch = jest.fn();
    const history = createMemoryHistory();
    history.push('/meow/3');
    mount(
      <Router history={history}>
        <Route path="/meow/:number">
          <MeowComponent fetch={fetch} />
        </Route>
      </Router>
    );
    expect(fetch).toHaveBeenNthCalledWith(1, '3', false, false);
    history.push('/meow/2');
    await flushPromises();
    expect(fetch).toHaveBeenNthCalledWith(2, '2', false, false);
  });
});

Test result:

 PASS  stackoverflow/72495434/index.test.tsx (10.054 s)
  72495434
    ✓ should pass (62 ms)

  console.log
    number 3

      at MeowComponent (stackoverflow/72495434/index.test.tsx:12:11)

  console.log
    number 2

      at MeowComponent (stackoverflow/72495434/index.test.tsx:12:11)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.512 s, estimated 11 s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文