UNPKG

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