简体   繁体   English

等待Promise.all在开玩笑的测试

[英]Awaiting Promise.all in jest test

In Comp component, I have a Promise.all that runs when the component is rendered. Comp组件中,我有一个Promise.all在组件渲染时运行。 I'm trying to test that OnItemsUpdate() is getting called once the Promise.all has resolved. 我试图测试Promise.all解决后, Promise.all已调用OnItemsUpdate()

const Comp = ({OnItemsUpdate}) => {
    Promise.all(items.map((item) => {
        return datasource.UpdateItem(item);
    })).then(() => {
        return OnItemsUpdate();
    });

    // not including rest of the component for brevity's sake
}

it("Calls OnItemsUpdate when promises resolve", async () => {
    const props = {
        OnItemsUpdate: jest.fn();
    }

    expect(props.OnItemsUpdate).tohavebeencalledtimes(0);

    const control = mount(<Comp {...props} />);
    await datasource.UpdateItem

    expect(props.OnItemsUpdate).tohavebeencalledtimes(1); // doesn't get called

})

Awaiting OnItemsUpdate isn't working as expect(props.OnItemsUpdate).tohavebeencalledtimes(1) still returns 0. 等待OnItemsUpdate无法正常工作expect(props.OnItemsUpdate).tohavebeencalledtimes(1)仍返回0。

You can fix this by using something like setImmediate to delay your assertions until after the callbacks in PromiseJobs have a chance to run. 您可以使用setImmediate类的setImmediate来延迟断言,直到PromiseJobs中的回调有机会运行后,才能解决此问题。

index.ts : index.ts

import React from 'react';

export const Comp = ({ OnItemsUpdate, items, datasource }) => {
  Promise.all(
    items.map(item => {
      return datasource.UpdateItem(item);
    })
  ).then(() => {
    return OnItemsUpdate();
  });

  // not including rest of the component for brevity's sake
  return <div>test</div>;
};

Unit test: 单元测试:

import React from 'react';
import { Comp } from './';
import { mount } from 'enzyme';

describe('Comp', () => {
  it('t1', done => {
    const props = {
      OnItemsUpdate: jest.fn(),
      items: [1, 2],
      datasource: {
        UpdateItem: jest.fn().mockResolvedValue('whatever')
      }
    };
    expect(props.OnItemsUpdate).toBeCalledTimes(0);
    const wrapper = mount(<Comp {...props}></Comp>);
    expect(props.datasource.UpdateItem).toBeCalledTimes(props.items.length);
    expect(wrapper.html()).toMatchSnapshot();
    setImmediate(() => {
      expect(props.OnItemsUpdate).toBeCalledTimes(1);
      done();
    });
  });
});

Test result with 100% coverage: 100%覆盖率的测试结果:

 PASS  src/stackoverflow/57248527/index.spec.tsx
  Comp
    ✓ t1 (63ms)

 › 1 snapshot written.
-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |      100 |      100 |      100 |      100 |                   |
 index.tsx |      100 |      100 |      100 |      100 |                   |
-----------|----------|----------|----------|----------|-------------------|
Snapshot Summary
 › 1 snapshot written from 1 test suite.

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 written, 1 total
Time:        5.322s

Snapshot: 快照:

// Jest Snapshot v1

exports[`Comp t1 1`] = `"<div>test</div>"`;

Here is the completed demo: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57248527 这是完整的演示: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57248527

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM