Я только начал использовать Jest и энзимы для тестирования Reactjs.Я использую асинхронную функцию для извлечения данных внутри componentDidMount.Я пытаюсь смоделировать функцию getData, но при ее сбое происходит сбой.
export class ListNamespaces extends React.Component<IListNamespacesProps, IListNamespacesState> {
constructor(props: IListNamespacesProps) {
super(props);
}
componentDidMount() {
this.getAllData()
.then((response: Types.ListNamespacesResponse) => {
this.setState({
...
})
});
}
getAllData() {
this.setState({
isLoading: true
});
const client = new Client();
return client.getAlldata();
}
...
}
export class Client {
public getAlldata() {
//async call
}
}
describe('ListNamespaces', () => {
test("Shallow Render matches initial snapshot", () => {
const listNamespaceView = <ListNamespaces/>;
listNamespaceView.prototype.getAllNamespaces = jest.fn();
const listNamespacesShallowView = shallow(listNamespaceView);
expect(listNamespacesShallowView).toMatchSnapshot();
});
});
Ошибка -
TypeError: Cannot read property 'then' of undefined
138 |
139 | componentDidMount() {
> 140 | this.getAllData()
141 | .then((response: Types.ListNamespacesResponse) => {
142 | ...
at ListNamespaces.Object.<anonymous>.ListNamespaces.componentDidMount (src/modules/bulk-namespace/views/list-namespaces.tsx:140:28)
at node_modules/enzyme/build/ShallowWrapper.js:215:22
at Object.batchedUpdates (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:474:22)
at new ShallowWrapper (node_modules/enzyme/build/ShallowWrapper.js:214:26)
at Object.shallow (node_modules/enzyme/build/shallow.js:21:10)
at Object.<anonymous> (tst/modules/bulk-namespace/list-namespaces.tsx:10:39)
Как правильно смоделировать эту функцию.