测试Redux箭头功能Action Creators

时间:2018-06-10 18:11:59

标签: unit-testing redux react-redux jestjs

我的React-Redux应用程序中有一个箭头函数,它只调度一个没有有效负载的动作,它将擦除(重置)我的Redux存储的一部分。没有涉及HTTP(or Promises)。

export const resetSearch = () => dispatch => {
  dispatch({
    type: RESET_SEARCH
  });
};

我想测试它返回正确的Action类型:

import * as actions from '../actions/actionCreators';
import * as types from '../actions/actionTypes';

describe('actions', () => {
  it('should create an action to reset part of the store', () => {
    const expectedAction = {
      type: types.RESET_SEARCH
    };
    expect(actions.resetSearch()).toEqual(expectedAction);
  });
});

测试没有通过,因为我需要返回一个Object,而是发送这个无限的箭头函数。这是Jest输出

期望值等于:{“type”:“RESET_SEARCH”}

收到:[功能匿名]

测试应该如何? 所有的帮助都是准备好的!

由于

1 个答案:

答案 0 :(得分:2)

你能不能尝试下面的代码片段,它应该:

import tkinter as tk
import threading
import time


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        # initialize the main window
        tk.Tk.__init__(self, *args, **kwargs)

        # add a container which will take all the widgets
        container = tk.Frame(self, bg="green")
        container.pack(side="top", fill="both", expand=True)

        # Add a titlebar object (defined below)
        self.titleBar = TitleBar(container, controller=self) 
        self.titleBar.grid(row=0, column=0, columnspan=2, sticky=tk.N+tk.W+tk.E)


class TitleBar(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        # the title bar contains only one label element
        self.titleLabel = tk.Label(self, text="This is the initial text")
        self.titleLabel.pack(side=tk.LEFT)


# Define a thread that runs in the background to perform intensive calculations
class MyTestThread(threading.Thread):
    def run(self):
        for i in range(10):
            time.sleep(1)
            a = i+100        # intensive calculation

            # from time to time: inform use about status
            print(a)      # printing to console works fine
            app.titleBar.titleLabel['text'] = "status: " + str(a)


if __name__ == "__main__":
    app = SampleApp()

    #app.titleBar.titleLabel['text'] = "test 2"

    t = MyTestThread()
    t.start()

    app.mainloop()

这里 resetSearch 返回一个用动作对象调用的函数,所以只是模仿了它。

如果您需要更多帮助,请与我们联系!

相关问题