UNPKG

5.48 kBTypeScriptView Raw
1import { AxiosInstance, AxiosRequestConfig } from "axios";
2
3interface Item {
4 response?: any;
5 responseText?: string | undefined;
6 status?: number | undefined;
7 statusText?: string | undefined;
8 headers?: any;
9}
10
11declare class Tracker {
12 constructor();
13
14 /**
15 * Reset all the items being tracked
16 */
17 reset(): void;
18
19 /**
20 * Add an item to be tracked
21 *
22 * @param item An item to be tracked
23 */
24 track(item: Item): void;
25
26 /**
27 * The count of items being tracked
28 */
29 count(): number;
30
31 /**
32 * Get an item being tracked at a given index
33 *
34 * @param index The index for the item to retrieve
35 */
36 at(index: number): Request;
37
38 /**
39 * Get the first item being tracked
40 */
41 first(): Request;
42
43 /**
44 * Get the most recent (last) item being tracked
45 */
46 mostRecent(): Request;
47
48 /**
49 * Dump the items being tracked to the console.
50 */
51 debug(): void;
52
53 /**
54 * Find and return element given the HTTP method and the URL.
55 */
56 get(method: string, url?: string): Request;
57
58 /**
59 * Stop an element from being tracked by removing it. Finds and returns the element,
60 * given the HTTP method and the URL.
61 */
62 remove(method: string, url: string): Request;
63}
64
65declare class Request {
66 /**
67 * Create a new Request object
68 *
69 * @param resolve The function to call when Promise is resolved
70 * @param reject The function to call when Promise is rejected
71 * @param config The config object to be used for the request
72 */
73 constructor(resolve: (arg: any) => void, reject: (arg: any) => void, config: AxiosRequestConfig);
74
75 config: AxiosRequestConfig;
76 headers: any;
77 url: string;
78 timeout: number;
79 withCredentials: boolean;
80 responseType: string;
81
82 /**
83 * Respond to this request with a timeout result
84 *
85 * @return A Promise that rejects with a timeout result
86 */
87 respondWithTimeout(): Promise<Response>;
88
89 /**
90 * Respond to this request with a specified result
91 *
92 * @param res The data representing the result of the request
93 * @return A Promise that resolves once the response is ready
94 */
95 respondWith(res: Item): Promise<Response>;
96}
97
98declare class Response {
99 /**
100 * Create a new Response object
101 *
102 * @param req The Request that this Response is associated with
103 * @param res The data representing the result of the request
104 */
105 constructor(req: Request, res: any);
106
107 config: AxiosRequestConfig;
108 data?: any;
109 status?: number | undefined;
110 statusText?: string | undefined;
111 headers: any;
112 request: Request;
113 code?: string | undefined;
114}
115
116declare let moxios: {
117 stubs: Tracker;
118 requests: Tracker;
119 delay: number;
120 timeoutException: Error;
121
122 /**
123 * Install the mock adapter for axios
124 */
125 install(instance?: AxiosInstance): void;
126
127 /**
128 * Uninstall the mock adapter and reset state
129 */
130 uninstall(instance?: AxiosInstance): void;
131
132 /**
133 * Stub a response to be used to respond to a request matching a URL or RegExp
134 *
135 * @param urlOrRegExp A URL or RegExp to test against
136 * @param response The response to use when a match is made
137 */
138 stubRequest(urlOrRegExp: string | RegExp, response: Item): void;
139
140 /**
141 * Stub a response to be used to respond to a request matching a URL or RegExp
142 *
143 * @param method An axios command
144 * @param urlOrRegExp A URL or RegExp to test against
145 * @param response The response to use when a match is made
146 */
147 stubRequest(method: string, urlOrRegExp: string | RegExp, response: Item): void;
148
149 /**
150 * Stub a response to be used one or more times to respond to a request matching a
151 * method and a URL or RegExp.
152 *
153 * @param method An axios command
154 * @param urlOrRegExp A URL or RegExp to test against
155 * @param response The response to use when a match is made
156 */
157 stubOnce(method: string, urlOrRegExp: string | RegExp, response: Item): Promise<void>;
158
159 /**
160 * Stub a timed response to a request matching a method and a URL or RegExp. If
161 * timer fires, reject with a TimeoutException for simple assertions. The goal is
162 * to show that a certain request was not made.
163 *
164 * @param method An axios command
165 * @param urlOrRegExp A URL or RegExp to test against
166 * @param response The response to use when a match is made
167 */
168 stubFailure(method: string, urlOrRegExp: string | RegExp, response: Item): Promise<void>;
169
170 /**
171 * Stub a timeout to be used to respond to a request matching a URL or RegExp
172 *
173 * @param urlOrRegExp A URL or RegExp to test against
174 */
175 stubTimeout(urlOrRegExp: string | RegExp): string;
176
177 /**
178 * Run a single test with mock adapter installed.
179 * This will install the mock adapter, execute the function provided,
180 * then uninstall the mock adapter once complete.
181 *
182 * @param fn The function to be executed
183 */
184 withMock(fn: () => void): void;
185
186 /**
187 * Wait for request to be made before proceding.
188 * This is naively using a `setTimeout`.
189 * May need to beef this up a bit in the future.
190 *
191 * @param fn The function to execute once waiting is over
192 * @param delay How much time in milliseconds to wait
193 */
194 wait(fn: () => void, delay?: number): void;
195};
196
197export = moxios;