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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何模擬作為參數(shù)傳遞的回調(diào)調(diào)用?

如何模擬作為參數(shù)傳遞的回調(diào)調(diào)用?

桃花長(zhǎng)相依 2022-07-21 20:56:37
我想知道如何模擬作為參數(shù)傳遞的回調(diào)調(diào)用:type YesOrNoHandler = (yesOrNo: boolean) => voidtype CheckValue = (val: string, handler: YesOrNoHandler)在這個(gè)例子中,我想模擬參數(shù)的調(diào)用handler: YesOrNoHandler,它代表一個(gè)回調(diào)。const check = jest.fn().mockImplementation((_, handler) => {   handler(true) // how to call `handler` outside of this scope ?})實(shí)際上,我什至不確定我是否必須為此開(kāi)玩笑。有人知道如何實(shí)現(xiàn)這一目標(biāo)嗎?
查看完整描述

1 回答

?
滄海一幻覺(jué)

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

只需將 分配給handler外部定義的變量。


例如


describe('61504714', () => {

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

    let handlerRef;

    const check = jest.fn().mockImplementation((_, handler) => {

      console.log('call check');

      handlerRef = handler;

    });

    const callback = jest.fn((yesOrNo: boolean) => (yesOrNo ? 'yes' : 'no'));

    check('_', callback);

    expect(callback).not.toBeCalled();

    const rval = handlerRef(true);

    expect(rval).toBe('yes');

    expect(check).toBeCalledWith('_', callback);

    expect(callback).toBeCalledWith(true);

  });

});

試驗(yàn)結(jié)果:


 PASS  stackoverflow/61504714/index.test.ts (11.463s)

  61504714

    ? should pass (33ms)


  console.log stackoverflow/61504714/index.test.ts:5

    call check


Test Suites: 1 passed, 1 total

Tests:       1 passed, 1 total

Snapshots:   0 total

Time:        13.581s


查看完整回答
反對(duì) 回復(fù) 2022-07-21
  • 1 回答
  • 0 關(guān)注
  • 78 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)