如何在React Jest酶中模拟HTTP服务

时间:2020-04-21 20:02:15

标签: reactjs jestjs enzyme ts-jest

我是Jest和React的新手。有一个带有https-service.ts文件的服务文件夹。代码从下面开始:

    import { IFlowEvent } from '@abc/mca-api-flow';
    import { ApiMCA } from '@abc/mca-api-mca';
    import { IServiceRequest } from '@abc/mca-api-service';

export class HttpService {
    private static api: ApiMCA;

    public static intialize(api: ApiMCA, callbackFn: Function) {
        HttpService.api = api;
        callbackFn();
    }

    public static dispatchAction<T>(
        serviceId: string, request?: IServiceRequest): Promise<{ data: T }> {

        return HttpService.api.service
            .call(serviceId, request)
            .then(({ data }) => data as { data: T });
    }

    public static dispatchFlow(payload: IFlowEvent) {
        return HttpService.api.flow.dispatch(payload);
    }

    public static get communication() {
        return this.api.communication;
    }
}

我想模拟上面的代码,并在我的测试用例中在beforeEach中使用。

0 个答案:

没有答案