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
错误。如何解决并正确测试呢?