TypeError:callback不是Jest测试中用于更新状态的函数

时间:2018-08-31 02:03:23

标签: reactjs jestjs enzyme

export interface MyState {
    DetailData?: any;

    BookID?: string;

    GetDetail: () => void;

    ShowWindow: (id: string) => void;
    HideWindow: () => void;
}

export class MyComponent extends React.Component<{}, MyState> {
    constructor(props: {}) {
        super(props);

        this.state = {
            GetDetail: () => {
                // external call here that returns DATA 
                this.setState({ DetailData: DATA });
            }
            ShowWindow: (id: string) => {
                this.setState({ BookID: id }, () => this.state.GetDetail());
            },
            HideWindow: () => {
                this.setState({
                    BookID: undefined,
                    DetailData: undefined,
                });
            },
        };
    }
}

我具有上述组件,其中调用ShowWindow使用一些ID更新BookID的状态,并在setState回调中触发this.state.GetDetail

我正在尝试像这样在Jest测试中对其进行测试:

it("Should update state with the BookID and call GetDetail", () => {
    const control = enzyme.mount(<MyComponent  />);

    const bookID= "ID123";

    control.state().ShowWindow(bookID);
    expect(control.state().BookID).toEqual(bookID);
});

但这将返回TypeError: callBack is not a function错误。如何解决并正确测试呢?

0 个答案:

没有答案