1 | import { jest } from '@jest/globals';
|
2 | import { UnresolvedSynchronousPromise, SynchronousPromise } from 'synchronous-promise';
|
3 |
|
4 | interface HttpResponse {
|
5 | data: any;
|
6 | status?: number;
|
7 | statusText?: string;
|
8 | headers?: object;
|
9 | config?: object;
|
10 | }
|
11 | interface Interceptor {
|
12 | use: jest.Mock<(onFulfilled?: (value: any) => any | Promise<any>, onRejected?: (error: any) => any) => number>;
|
13 | eject: jest.Mock<(index: number) => void>;
|
14 | clear: jest.Mock<() => void>;
|
15 | }
|
16 | interface Interceptors {
|
17 | request: Interceptor;
|
18 | response: Interceptor;
|
19 | }
|
20 | interface InterceptorsStack {
|
21 | onFulfilled?(value: any): any | Promise<any>;
|
22 | onRejected?(error: any): any;
|
23 | }
|
24 | type RequestHandler = (req: AxiosMockQueueItem) => void;
|
25 | interface AxiosDefaults {
|
26 | headers: any;
|
27 | }
|
28 | type MockUnresolvedPromise = (url?: string, options?: any) => UnresolvedSynchronousPromise<any>;
|
29 | type MockUnresolvedPromiseBody = (url?: string, body?: any, options?: any) => UnresolvedSynchronousPromise<any>;
|
30 | interface AxiosAPI {
|
31 | get: jest.Mock<MockUnresolvedPromise>;
|
32 | post: jest.Mock<MockUnresolvedPromiseBody>;
|
33 | put: jest.Mock<MockUnresolvedPromiseBody>;
|
34 | patch: jest.Mock<MockUnresolvedPromiseBody>;
|
35 | delete: jest.Mock<MockUnresolvedPromise>;
|
36 | head: jest.Mock<MockUnresolvedPromise>;
|
37 | options: jest.Mock<MockUnresolvedPromise>;
|
38 | request: jest.Mock<(options?: any) => UnresolvedSynchronousPromise<any>>;
|
39 | all: jest.Mock<(promises: any) => Promise<any>>;
|
40 | create: jest.Mock<() => AxiosMockType>;
|
41 | interceptors: Interceptors;
|
42 | defaults: AxiosDefaults;
|
43 | isAxiosError: (error: any) => boolean;
|
44 | }
|
45 | interface Cancel {
|
46 | message: string;
|
47 | }
|
48 | type CancelStatic = new (message?: string) => Cancel;
|
49 | interface CancelToken {
|
50 | promise: Promise<Cancel>;
|
51 | reason?: Cancel;
|
52 | throwIfRequested(): void;
|
53 | }
|
54 | type Canceler = (message?: string) => void;
|
55 | interface CancelTokenSource {
|
56 | token: CancelToken;
|
57 | cancel: Canceler;
|
58 | }
|
59 | interface CancelTokenStatic {
|
60 | new (executor: (cancel: Canceler) => void): CancelToken;
|
61 | source(): CancelTokenSource;
|
62 | }
|
63 | interface AxiosMockAPI {
|
64 | /**
|
65 | * Simulates a server response, (optionally) with the given data
|
66 | * @param response (optional) response returned by the server
|
67 | * @param queueItem (optional) request promise for which response should be resolved
|
68 | * @param silentMode (optional) specifies whether the call should throw an error or
|
69 | * only fail quietly if no matching request is found. Defaults to false.
|
70 | */
|
71 | mockResponse: (response?: HttpResponse, queueItem?: Promise<any> | AxiosMockQueueItem, silentMode?: boolean) => void;
|
72 | /**
|
73 | * Simulates a server response for a specific request, (optionally) with the given data
|
74 | * @param criteria specifies which request should be resolved; it can be just the URL
|
75 | * or an object containing url and/or method
|
76 | * @param response (optional) response returned by the server
|
77 | * @param silentMode (optional) specifies whether the call should throw an error or
|
78 | * only fail quietly if no matching request is found. Defaults to false.
|
79 | */
|
80 | mockResponseFor: (criteria: string | AxiosMockRequestCriteria, response?: HttpResponse, silentMode?: boolean) => void;
|
81 | /**
|
82 | * Simulates an error in server request
|
83 | * @param error (optional) error object
|
84 | * @param queueItem (optional) request promise for which response should be resolved
|
85 | * @param silentMode (optional) specifies whether the call should throw an error or
|
86 | * only fail quietly if no matching request is found.
|
87 | */
|
88 | mockError: (error?: any, queueItem?: Promise<any> | AxiosMockQueueItem, silentMode?: boolean) => void;
|
89 | /**
|
90 | * Returns promise of the most recent request
|
91 | */
|
92 | lastPromiseGet: () => SynchronousPromise<any>;
|
93 | /**
|
94 | * Removes the give promise from the queue
|
95 | * @param promise
|
96 | */
|
97 | popPromise: (promise?: UnresolvedSynchronousPromise<any>) => UnresolvedSynchronousPromise<any>;
|
98 | /**
|
99 | * Returns request item of the most recent request
|
100 | */
|
101 | lastReqGet: () => AxiosMockQueueItem;
|
102 | /**
|
103 | * Returns request item of the most recent request with the given criteria
|
104 | * Returns undefined if no matching request could be found
|
105 | *
|
106 | * The result can then be used with @see mockResponse; in most cases it is better
|
107 | * to use @see mockResponseFor instead of calling this function yourself
|
108 | *
|
109 | * @param criteria the criteria by which to find the request
|
110 | */
|
111 | getReqMatching: (criteria: AxiosMockRequestCriteria) => AxiosMockQueueItem;
|
112 | /**
|
113 | * Returns request item of the most recent request with the given url
|
114 | * The url must equal the url given in the 1st parameter when the request was made
|
115 | * Returns undefined if no matching request could be found
|
116 | *
|
117 | * The result can then be used with @see mockResponse
|
118 | *
|
119 | * @param url the url of the request to be found
|
120 | */
|
121 | getReqByUrl: (url: string) => AxiosMockQueueItem;
|
122 | /**
|
123 | * Returns request item of the most recent request with the given regex url
|
124 | * Returns undefined if no matching request could be found
|
125 | *
|
126 | * The result can then be used with @see mockResponse
|
127 | *
|
128 | * @param url the url of the request to be found
|
129 | */
|
130 | getReqByMatchUrl: (url: RegExp) => AxiosMockQueueItem;
|
131 | /**
|
132 | * Returns the most recent request matching any key with the regex (e.g.: url, data, config)
|
133 | * Can also use multiple keys for research. For example: getReqByRegex({ url: /batch/, data: /employees/ })
|
134 | *
|
135 | * Returns undefined if no matching request could be found
|
136 | *
|
137 | * The result can then be used with @see mockResponse
|
138 | *
|
139 | * @param opts { key_a: RegExp_a, ..., key_n: RegExp_n }
|
140 | * key_x: The key of the request to be found
|
141 | * RegExp_x: The RegExp to be tested against that key
|
142 | */
|
143 | getReqByRegex: (opts: {
|
144 | [key in keyof AxiosMockQueueItem]+?: RegExp;
|
145 | }) => AxiosMockQueueItem;
|
146 | /**
|
147 | * Removes the give request from the queue
|
148 | * @param promise
|
149 | */
|
150 | popRequest: (promise?: AxiosMockQueueItem) => AxiosMockQueueItem;
|
151 | /**
|
152 | * Return the queued requests
|
153 | */
|
154 | queue: () => AxiosMockQueueItem[];
|
155 | /**
|
156 | * Clears all of the queued requests
|
157 | */
|
158 | reset: () => void;
|
159 | /**
|
160 | * Set a request handler that gets invoked every time a new request comes in
|
161 | * The handler is invoked with the new request item
|
162 | *
|
163 | * @param handler the function to invoke with the new request item every time a new request comes in
|
164 | */
|
165 | useRequestHandler: (handler: RequestHandler) => void;
|
166 | Cancel: CancelStatic;
|
167 | CancelToken: CancelTokenStatic;
|
168 | isCancel(value: any): boolean;
|
169 | }
|
170 | interface AxiosMockQueueItem {
|
171 | promise: UnresolvedSynchronousPromise<any>;
|
172 | method: string;
|
173 | url: string;
|
174 | data?: any;
|
175 | config?: any;
|
176 | }
|
177 | interface AxiosMockRequestCriteria {
|
178 | url?: string;
|
179 | method?: string;
|
180 | params?: any;
|
181 | }
|
182 | /**
|
183 | * Axios object can be called like a function,
|
184 | * that's why we're defining it as a spy
|
185 | */
|
186 | type AxiosMockType = AxiosAPI & AxiosMockAPI & jest.Mock<(options?: any) => any>;
|
187 |
|
188 | /**
|
189 | * TypeScript version of Axios mock for unit testing with [Jest](https://facebook.github.io/jest/).
|
190 | * This file is based on https://gist.github.com/tux4/36006a1859323f779ab0
|
191 | *
|
192 | * @author knee-cola <nikola.derezic@gmail.com>
|
193 | * @license @license MIT License, http://www.opensource.org/licenses/MIT
|
194 | */
|
195 |
|
196 | declare const MockAxios: AxiosMockType;
|
197 |
|
198 | export { AxiosAPI, AxiosMockAPI, AxiosMockQueueItem, AxiosMockRequestCriteria, AxiosMockType, HttpResponse, InterceptorsStack, RequestHandler, MockAxios as default };
|