第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 JEST-Enzyme 中編寫測試用例以在構造函數(shù)中調用 promise

在 JEST-Enzyme 中編寫測試用例以在構造函數(shù)中調用 promise

倚天杖 2022-01-01 21:07:44
我需要為具有在構造函數(shù)中調用的承諾的 React 類組件編寫渲染測試用例。React 類組件的構造函數(shù):constructor(props) {    super(props);    this.props.getPaypalAuthUrl().then((result) => {        this.setState({authUrl: result})    });}我的測試用例:test(testIDAndStatement.settings.TC086, async () => {        const wrapper = await shallow(<Payment getPaypalAuthUrl={getPaypalAuthUrl}/>);        const instance = wrapper.instance();        instance.constructor();        wrapper.setState({ connectedToPaypal: true });        const paymentComponent = wrapper.find('.settings-payment__wrapper');        expect(paymentComponent.length).toBe(1);    });錯誤獲?。篢ypeError: this.props.getPaypalAuthUrl(...).then 不是函數(shù)我嘗試過的解決方案:我嘗試使用 async-await 編寫我的測試用例,并嘗試獲取構造函數(shù)的實例,就像我們?yōu)樯芷诨蚱胀ǚ椒ǐ@取的一樣。
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

副作用是否在構造函數(shù)中正確完成。這是您案例的單元測試解決方案:


index.jsx:


import React, { Component } from 'react';


class SomeCompoennt extends Component {

  constructor(props) {

    super(props);

    this.state = { authUrl: '' };

    this.props.getPaypalAuthUrl().then((result) => {

      this.setState({ authUrl: result });

    });

  }

  render() {

    return <div>some component</div>;

  }

}


export default SomeCompoennt;

index.spec.jsx:


import SomeCompoennt from './index';

import React from 'react';

import { shallow } from 'enzyme';


describe('58877501', () => {

  it('should pass', async () => {

    const mProps = {

      getPaypalAuthUrl: jest.fn().mockResolvedValueOnce('http://github.com'),

    };

    const wrapper = shallow(<SomeCompoennt {...mProps}></SomeCompoennt>);

    expect(wrapper.text()).toBe('some component');

    expect(wrapper.state()).toEqual({ authUrl: '' });

    await new Promise((resolve) => setTimeout(resolve));

    expect(wrapper.state()).toEqual({ authUrl: 'http://github.com' });

  });

});

100% 覆蓋率的單元測試結果:


 PASS  src/stackoverflow/58877501/index.spec.tsx (10.985s)

  58877501

    ? should pass (24ms)


-----------|----------|----------|----------|----------|-------------------|

File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |

-----------|----------|----------|----------|----------|-------------------|

All files  |      100 |      100 |      100 |      100 |                   |

 index.jsx |      100 |      100 |      100 |      100 |                   |

-----------|----------|----------|----------|----------|-------------------|

Test Suites: 1 passed, 1 total

Tests:       1 passed, 1 total

Snapshots:   0 total

Time:        12.808s

源代碼:https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58877501


查看完整回答
反對 回復 2022-01-01
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號