typescript 模拟Observable.ajax.get
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 模拟Observable.ajax.get相关的知识,希望对你有一定的参考价值。
jest.mock('./process-env');
import Mock = jest.Mock;
import {Observable} from 'rxjs';
import {get} from './requests';
import {getProcessEnv} from './process-env';
describe('get', () => {
const originalAjaxGet = Observable.ajax.get;
const API_KEY = 'some';
beforeEach(() => {
// because "get" method uses "API_KEY", that returned by "getProcessEnv" method
(getProcessEnv as Mock<any>).mockImplementationOnce(() => ({
API_KEY
}));
});
afterEach(() => {
Observable.ajax.get = originalAjaxGet;
});
it('Observable.ajax.get is called once', () => {
Observable.ajax.get = jest.fn(() => Observable.of(null));
get('http://some-url.com');
const calls = (Observable.ajax.get as Mock<any>).mock.calls;
expect(Array.isArray(calls)).toBeTruthy();
expect(calls.length).toEqual(1);
});
it('Observable.ajax.get is called with 1-st arg which equals to a passed URL', () => {
Observable.ajax.get = jest.fn(() => Observable.of(null));
get('http://some-url.com');
const firstCall = (Observable.ajax.get as Mock<any>).mock.calls[0];
const firstArg = firstCall[0];
expect(firstArg).toEqual('http://some-url.com');
});
});
以上是关于typescript 模拟Observable.ajax.get的主要内容,如果未能解决你的问题,请参考以下文章
Typescript 期望 mockImplementation 为所有方法和属性提供模拟
使用 Typescript 从 Jest 手动模拟中导入函数
服务中的模拟存储库,节点 TypeScript
使用 Mocha 和 Typescript 模拟依赖项
使用 typescript 模拟 forwardRef 组件 jest mockImplementation
如何在 Typescript 中模拟 Cognito 响应