用Jest&Enzyme模拟`getAppState()`

时间:2018-08-22 21:12:32

标签: reactjs unit-testing redux jestjs enzyme

我正在尝试在React / Redux应用程序中测试我的动作,并且在我的动作之一中,我正在使用getAppState()来获取Redux的当前状态。该动作没有参数,只是从返回getAppState()

来解构状态属性

actions / myFile / index.test.js

it('should call myAction successfully', () => {
  const expected = [
    {
      type: MY_TYPE,
      payload: {
        ...mockPayload
      }
    }
  ];

  store.dispatch(myAction());
  expect(store.getActions().toEqual(expected));
});

actions / myFile / index.js

export const myAction = () => (dispatch, getAppState) => {
    const { myReducer: { reducerProp } } = getAppState();
    const { valOne, valTwo, valThree } = reducerProp;

    return myServiceCallPromise({ valOne, valTwo }, valThree)
        .then((res) => {
            dispatch(anotherAction());
        });
}

尝试用Jest测试我的动作时,出现错误提示:

TypeError: Cannot read property 'reducerProp' of undefined

我的问题是我该如何模拟getAppState()函数的返回值并在测试中使用它们?我曾经用Google搜索过一次,看到了结果并弄清楚了,为什么不去StackOverflow并问自己大声笑

1 个答案:

答案 0 :(得分:1)

(我还假设您正在使用using(MyDisposable d = new MyDisposable()) { ... } )。

您将需要配置#!/usr/bin/env python import pyotp import os import sys import math import time from appJar import gui udstart = 1 app = gui("2FA Keys","480x320") def count(n): while n >= 0: time.sleep(1) n -= 1 if n == 0: return 1 #Keygenertor function pass the base32 code def keyGen(secret): secretcalc = pyotp.TOTP(secret) code = secretcalc.now() Lcode = list(code) Lcode.insert(3, " ") Ocode = ''.join(Lcode) return Ocode #INIT CODES GO HERE CoinbaseCode = keyGen("3JCAJVDGIW4KHUHL") SiaCoinCode = keyGen("PFFO3KKKRQL6ACU5") app.stretch(columns) app.setFont(50) app.addLabel("l2", "Sia Coin: " + SiaCoinCode, 0,0,0,0) app.setLabelbg("l2", "blue") app.addLabel("l1", "Coinbase: " + CoinbaseCode, 1,0,0,0) #Coinbase 2FA app.setLabelBg("l1", "red") while True: if udstart == 1: break else: time.sleep(30) break def update(): #UPDATE CODES GO HERE CoinbaseCode = keyGen("3JCAJVDGIW4KHUHL") #Coinbase SiaCoinCode = keyGen("PFFO3KKKRQL6ACU5") app.setLabel("l2", "Sia Coin: " + SiaCoinCode) app.setLabelBg("l2", "blue") app.setLabel("l1", "Coinbase: " + CoinbaseCode) app.setLabelBg("l1", "red") #profit??? udstart = 0 app.registerEvent(update) app.go()

redux-thunk

有关如何使用商店的更多信息,请参见here