diff --git a/types/fetch-mock/fetch-mock-tests.ts b/types/fetch-mock/fetch-mock-tests.ts index 1d5c5a51b3..594d78c127 100644 --- a/types/fetch-mock/fetch-mock-tests.ts +++ b/types/fetch-mock/fetch-mock-tests.ts @@ -30,22 +30,59 @@ fetchMock.mock(/test/, { } }); -fetchMock.restore().reset(); +fetchMock.restore().reset().resetHistory().resetBehavior(); -(fetchMock.calls().matched[0][1] as RequestInit).body; -fetchMock.calls().unmatched[0][0].toUpperCase(); -fetchMock.calls("http://test.com")[0][0].toUpperCase(); -(fetchMock.calls("http://test.com")[0][1] as RequestInit).body; +let calls: fetchMock.MockCall[] = fetchMock.calls(/https?:\/\/test.com/, { + method: 'GET', +}); +calls[0][0].toUpperCase(); +calls[0].identifier.toUpperCase(); +calls[0].isUnmatched; +calls = fetchMock.calls(); +calls = fetchMock.calls(true); +calls = fetchMock.calls("http://test.com", "GET"); -const doneStatus: boolean = fetchMock.done(); -const doneStatusArguments: boolean = fetchMock.done("http://test.com"); +let doneStatus: boolean = fetchMock.done(); +doneStatus = fetchMock.done(true); +doneStatus = fetchMock.done("http://test.com"); +doneStatus = fetchMock.done(/https?:\/\/test.com/); -const calledStatus: boolean = fetchMock.called(); -const calledStatusArguments = fetchMock.called("http://test.com"); +let calledStatus: boolean = fetchMock.called(); +calledStatus = fetchMock.called(true); +calledStatus = fetchMock.called("http://test.com"); +calledStatus = fetchMock.called(/https?:\/\/test.com/); +calledStatus = fetchMock.called("http://test.com", "GET"); +calledStatus = fetchMock.called("http://test.com", { + method: "GET", +}); +calledStatus = fetchMock.called((url: string, opts: fetchMock.MockRequest): boolean => { + return true; +}); +calledStatus = fetchMock.called(fetchMock.MATCHED); +calledStatus = fetchMock.called(fetchMock.UNMATCHED); -(fetchMock.lastCall()[1] as RequestInit).body; -const lastUrl: string = fetchMock.lastUrl(); -fetchMock.lastOptions(); +let lastCall: fetchMock.MockCall = fetchMock.lastCall(); +lastCall = fetchMock.lastCall(/https?:\/\/test.com/, { + method: "GET", +}); +lastCall = fetchMock.lastCall("https://test.com", "GET"); + +let lastUrl: string = fetchMock.lastUrl(); +lastUrl = fetchMock.lastUrl(true); +lastUrl = fetchMock.lastUrl("http://test.com"); +lastUrl = fetchMock.lastUrl(/https?:\/\/test.com/); +lastUrl = fetchMock.lastUrl("http://test.com", "GET"); +lastUrl = fetchMock.lastUrl("http://test.com", { + method: "GET", +}); +let lastOptions: fetchMock.MockOptions = fetchMock.lastOptions(); +lastOptions = fetchMock.lastOptions(true); +lastOptions = fetchMock.lastOptions("http://test.com"); +lastOptions = fetchMock.lastOptions(/https?:\/\/test.com/); +lastOptions = fetchMock.lastOptions("http://test.com", "GET"); +lastOptions = fetchMock.lastOptions("http://test.com", { + method: "GET", +}); fetchMock.get("http://test.com", 200); fetchMock.getOnce("http://test.com", 200); @@ -99,3 +136,7 @@ sandbox.get("http://test.com", { body: 'abc', redirectUrl: "http://example.org" }); + +const response: fetchMock.MockResponseObject = { + throws: new Error('error'), +}; diff --git a/types/fetch-mock/index.d.ts b/types/fetch-mock/index.d.ts index fdae338fce..4b2915756e 100644 --- a/types/fetch-mock/index.d.ts +++ b/types/fetch-mock/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for fetch-mock 6.0 +// Type definitions for fetch-mock 7.2 // Project: https://github.com/wheresrhys/fetch-mock // Definitions by: Alexey Svetliakov // Tamir Duberstein @@ -7,6 +7,7 @@ // Matt Tennison // Quentin Bouygues // Fumiaki Matsushima +// Colin Doig // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -33,6 +34,35 @@ declare namespace fetchMock { */ type MockMatcher = string | RegExp | MockMatcherFunction; + /** + * Inspection filter. Can be one of the following: + * boolean: + * * true retrieves all calls matched by fetch. + * fetchMock.MATCHED is an alias for true and may be used to make tests + * more readable. + * * false retrieves all calls not matched by fetch (i.e. those handled + * by catch() or spy(). fetchMock.UNMATCHED is an alias for false and + * may be used to make tests more readable. + * MockMatcher (routeIdentifier): + * All routes have an identifier: + * * If it’s a named route, the identifier is the route’s name + * * If the route is unnamed, the identifier is the matcher passed in to + * .mock() + * All calls that were handled by the route with the given identifier + * will be retrieved + * MockMatcher (matcher): + * Any matcher compatible with the mocking api can be passed in to filter + * the calls arbitrarily. + */ + type InspectionFilter = MockMatcher | boolean; + + /** + * Either an object compatible with the mocking api or a string specifying + * a http method to filter by. This will be used to filter the list of + * calls further. + */ + type InspectionOptions = MockOptions | string; + /** * Mock response object */ @@ -57,21 +87,7 @@ declare namespace fetchMock { * If this property is present then a Promise rejected with the value * of throws is returned */ - throws?: boolean; - - /** - * This property determines whether or not the request body should be - * JSON.stringified before being sent - * @default true - */ - sendAsJson?: boolean; - - /** - * Setting this property to true will automatically add the - * content-length header - * @default true - */ - includeContentLength?: boolean; + throws?: Error; /** * The URL the response should be from (to imitate followed redirects @@ -156,13 +172,29 @@ declare namespace fetchMock { * if nothing matches it). */ repeat?: number; + + /** + * Convert objects into JSON before delivering as stub reponses. Can + * be useful to set to false globally if e.g. dealing with a lot of + * array buffers. If true, will also add content-type: application/json + * header. + * @default true + */ + sendAsJson?: boolean; + + /** + * Automatically sets a content-length header on each response. + * @default true + */ + includeContentLength?: boolean; } - type MockCall = [string, MockRequest]; - - interface MatchedRoutes { - matched: MockCall[]; - unmatched: MockCall[]; + interface MockCall extends Array { + 0: string; + 1: RequestInit | undefined; + identifier: string; + isUnmatched: boolean | undefined; + request: Request | undefined; } interface MockOptionsMethodGet extends MockOptions { @@ -186,6 +218,9 @@ declare namespace fetchMock { } interface FetchMockStatic { + MATCHED: true; + UNMATCHED: false; + /** * Replaces fetch() with a stub which records its calls, grouped by * route, and optionally returns a mocked Response object or passes the @@ -381,16 +416,28 @@ declare namespace fetchMock { spy(response?: MockResponse | MockResponseFunction): this; /** - * Chainable method that restores fetch() to its unstubbed state and - * clears all data recorded for its calls. + * Restores fetch() to its unstubbed state and clears all data recorded + * for its calls. reset() is an alias for restore(). */ restore(): this; /** - * Chainable method that clears all data recorded for fetch()'s calls + * Restores fetch() to its unstubbed state and clears all data recorded + * for its calls. reset() is an alias for restore(). */ reset(): this; + /** + * Clears all data recorded for fetch()’s calls. It will not restore + * fetch to its default implementation. + */ + resetHistory(): this; + + /** + * Removes mocking behaviour without resetting call history. + */ + resetBehavior(): this; + /** * Returns a promise that resolves once all fetches handled by fetch-mock * have resolved. @@ -398,57 +445,80 @@ declare namespace fetchMock { flush(): Promise; /** - * Returns all calls to fetch, grouped by whether fetch-mock matched - * them or not. + * Returns an array of all calls to fetch matching the given filters. + * Each call is returned as a [url, options] array. If fetch was called + * using a Request instance, this will be available as a request + * property on this array. + * @param [filter] Allows filtering of calls to fetch based on various + * criteria + * @param [options] Either an object compatible with the mocking api or + * a string specifying a http method to filter by. This will be used to + * filter the list of calls further. */ - calls(): MatchedRoutes; + calls(filter?: InspectionFilter, options?: InspectionOptions): MockCall[]; /** - * Returns all calls to fetch matching matcherName. + * Returns a Boolean indicating whether any calls to fetch matched the + * given filter. + * @param [filter] Allows filtering of calls to fetch based on various + * criteria + * @param [options] Either an object compatible with the mocking api or + * a string specifying a http method to filter by. This will be used to + * filter the list of calls further. */ - calls(matcherName?: string): MockCall[]; - - /** - * Returns a Boolean indicating whether fetch was called and a route - * was matched (or a specific route if matcherName is passed). - * @param [matcherName] either the name of a route or equal to - * matcher.toString() for any unnamed route - */ - called(matcherName?: string): boolean; + called(filter?: InspectionFilter, options?: InspectionOptions): boolean; /** * Returns a Boolean indicating whether fetch was called the expected - * number of times (or at least once if the route defines no expectation - * is set) for every route (or for a specific route if matcherName is - * passed). - * @param [matcherName] either the name of a route or equal to - * matcher.toString() for any unnamed route + * number of times (or has been called at least once if repeat is + * undefined for the route). + * @param [filter] Rule for matching calls to fetch. */ - done(matcherName?: string): boolean; + done(filter?: InspectionFilter): boolean; /** - * Returns the arguments for the last matched call to fetch (or the - * last call to specific route is matcherName is passed). - * @param [matcherName] either the name of a route or equal to - * matcher.toString() for any unnamed route + * Returns the arguments for the last call to fetch matching the given + * filter. + * @param [filter] Allows filtering of calls to fetch based on various + * criteria + * @param [options] Either an object compatible with the mocking api or + * a string specifying a http method to filter by. This will be used to + * filter the list of calls further. */ - lastCall(matcherName?: string): MockCall; + lastCall( + filter?: InspectionFilter, + options?: InspectionOptions, + ): MockCall | undefined; /** - * Returns the url for the last matched call to fetch (or the last - * call to specific route is matcherName is passed). - * @param [matcherName] either the name of a route or equal to - * matcher.toString() for any unnamed route + * Returns the url for the last call to fetch matching the given + * filter. If fetch was last called using a Request instance, the url + * will be extracted from this. + * @param [filter] Allows filtering of calls to fetch based on various + * criteria + * @param [options] Either an object compatible with the mocking api or + * a string specifying a http method to filter by. This will be used to + * filter the list of calls further. */ - lastUrl(matcherName?: string): string; + lastUrl( + filter?: InspectionFilter, + options?: InspectionOptions, + ): string | undefined; /** - * Returns the options for the last matched call to fetch (or the - * last call to a specific route is matcherName is passed). - * @param [matcherName] either the name of a route or equal to - * matcher.toString() for any unnamed route + * Returns the options for the call to fetch matching the given filter. + * If fetch was last called using a Request instance, a set of options + * inferred from the Request will be returned. + * @param [filter] Allows filtering of calls to fetch based on various + * criteria + * @param [options] Either an object compatible with the mocking api or + * a string specifying a http method to filter by. This will be used to + * filter the list of calls further. */ - lastOptions(matcherName?: string): MockRequest; + lastOptions( + filter?: InspectionFilter, + options?: InspectionOptions, + ): MockOptions | undefined; /** * Set some global config options, which include diff --git a/types/fetch-mock/v6/fetch-mock-tests.ts b/types/fetch-mock/v6/fetch-mock-tests.ts new file mode 100644 index 0000000000..1d5c5a51b3 --- /dev/null +++ b/types/fetch-mock/v6/fetch-mock-tests.ts @@ -0,0 +1,101 @@ +import * as fetchMock from "fetch-mock"; + +fetchMock.mock("http://test.com", 200); +fetchMock.mock("http://test.com", 200, { + headers: { + test: "header" + } +}); +fetchMock.mock("http//test.com", 200, { + query: { + searchValue: "apples" + } +}); +fetchMock.mock("http://test.com", 200, { + repeat: 2 +}); +fetchMock.mock(/test\.com/, 200); +fetchMock.mock(() => true, 200); +fetchMock.mock((url, opts) => true, 200); +fetchMock.once("http://test.com", 200); + +fetchMock.mock(/test/, "test").mock(/test/, { a: "b" }); +fetchMock.mock(/test/, { + status: 200, + headers: { + test: "test" + }, + body: { + a: "b" + } +}); + +fetchMock.restore().reset(); + +(fetchMock.calls().matched[0][1] as RequestInit).body; +fetchMock.calls().unmatched[0][0].toUpperCase(); +fetchMock.calls("http://test.com")[0][0].toUpperCase(); +(fetchMock.calls("http://test.com")[0][1] as RequestInit).body; + +const doneStatus: boolean = fetchMock.done(); +const doneStatusArguments: boolean = fetchMock.done("http://test.com"); + +const calledStatus: boolean = fetchMock.called(); +const calledStatusArguments = fetchMock.called("http://test.com"); + +(fetchMock.lastCall()[1] as RequestInit).body; +const lastUrl: string = fetchMock.lastUrl(); +fetchMock.lastOptions(); + +fetchMock.get("http://test.com", 200); +fetchMock.getOnce("http://test.com", 200); +fetchMock.post("http://test.com", 200); +fetchMock.postOnce("http://test.com", 200); +fetchMock.put("http://test.com", 200); +fetchMock.putOnce("http://test.com", 200); +fetchMock.delete("http://test.com", 200); +fetchMock.deleteOnce("http://test.com", 200); +fetchMock.head("http://test.com", 200); +fetchMock.headOnce("http://test.com", 200); +fetchMock.patch("http://test.com", 200); +fetchMock.patchOnce("http://test.com", 200); + +fetchMock.get("http://test.com", 200, {method: "GET"}); +fetchMock.get("http://test.com", 200, {method: "GET", overwriteRoutes: true}); +fetchMock.get("http://test.com", 200, {overwriteRoutes: true}); +fetchMock.post("http://test.com", 200, {method: "POST"}); +fetchMock.put("http://test.com", 200, {method: "PUT"}); +fetchMock.delete("http://test.com", 200, {method: "DELETE"}); +fetchMock.head("http://test.com", 200, {method: "HEAD"}); + +fetchMock + .mock("http://test.com", 200) + .catch(503); + +fetchMock + .mock("http://test.com", 200) + .spy(); + +const myMatcher: fetchMock.MockMatcherFunction = ( + url: string, + opts: fetchMock.MockRequest +) => true; + +fetchMock.flush().then(resolved => resolved.forEach(console.log)); +fetchMock.flush().catch(r => r); + +fetchMock.get("http://test.com", { + body: 'abc', + includeContentLength: false +}); + +fetchMock.get("http://test.com", { + body: 'abc', + redirectUrl: "http://example.org" +}); + +const sandbox = fetchMock.sandbox(); +sandbox.get("http://test.com", { + body: 'abc', + redirectUrl: "http://example.org" +}); diff --git a/types/fetch-mock/v6/index.d.ts b/types/fetch-mock/v6/index.d.ts new file mode 100644 index 0000000000..fdae338fce --- /dev/null +++ b/types/fetch-mock/v6/index.d.ts @@ -0,0 +1,473 @@ +// Type definitions for fetch-mock 6.0 +// Project: https://github.com/wheresrhys/fetch-mock +// Definitions by: Alexey Svetliakov +// Tamir Duberstein +// Risto Keravuori +// Chris Sinclair +// Matt Tennison +// Quentin Bouygues +// Fumiaki Matsushima +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare namespace fetchMock { + type MockRequest = Request | RequestInit; + + /** + * Mock matcher function + */ + type MockMatcherFunction = (url: string, opts: MockRequest) => boolean; + + /** + * Mock matcher. Can be one of following: + * string: Either + * * an exact url to match e.g. 'http://www.site.com/page.html' + * * if the string begins with a `^`, the string following the `^` must + * begin the url e.g. '^http://www.site.com' would match + * 'http://www.site.com' or 'http://www.site.com/page.html' + * * '*' to match any url + * RegExp: A regular expression to test the url against + * Function(url, opts): A function (returning a Boolean) that is passed the + * url and opts fetch() is called with (or, if fetch() was called with one, + * the Request instance) + */ + type MockMatcher = string | RegExp | MockMatcherFunction; + + /** + * Mock response object + */ + interface MockResponseObject { + /** + * Set the response body + */ + body?: string | {}; + + /** + * Set the response status + * @default 200 + */ + status?: number; + + /** + * Set the response headers. + */ + headers?: { [key: string]: string }; + + /** + * If this property is present then a Promise rejected with the value + * of throws is returned + */ + throws?: boolean; + + /** + * This property determines whether or not the request body should be + * JSON.stringified before being sent + * @default true + */ + sendAsJson?: boolean; + + /** + * Setting this property to true will automatically add the + * content-length header + * @default true + */ + includeContentLength?: boolean; + + /** + * The URL the response should be from (to imitate followed redirects + * - will set redirected: true on the response) + */ + redirectUrl?: string; + } + + /** + * Response: A Response instance - will be used unaltered + * number: Creates a response with this status + * string: Creates a 200 response with the string as the response body + * object: As long as the object is not a MockResponseObject it is + * converted into a json string and returned as the body of a 200 response + * If MockResponseObject was given then it's used to configure response + * Function(url, opts): A function that is passed the url and opts fetch() + * is called with and that returns any of the responses listed above + */ + type MockResponse = Response | Promise + | number | Promise + | string | Promise + | {} | Promise<{}> + | MockResponseObject | Promise; + + /** + * Mock response function + */ + type MockResponseFunction = (url: string, opts: MockRequest) => MockResponse; + + /** + * Mock options object + */ + interface MockOptions { + /** + * A unique string naming the route. Used to subsequently retrieve + * references to the calls, grouped by name. + * @default matcher.toString() + * + * Note: If a non-unique name is provided no error will be thrown + * (because names are optional, auto-generated ones may legitimately + * clash) + */ + name?: string; + + /** + * http method to match + */ + method?: string; + + /** + * key/value map of headers to match + */ + headers?: { [key: string]: string }; + + /** + * key/value map of query strings to match, in any order + */ + query?: { [key: string]: string }; + + /** + * as specified above + */ + matcher?: MockMatcher; + + /** + * This option allows for existing routes in a mock to be overwritten. + * It’s also possible to define multiple routes with ‘the same’ matcher. + * Default behaviour is to error + */ + overwriteRoutes?: boolean; + + /** + * as specified above + */ + response?: MockResponse | MockResponseFunction; + + /** + * integer, n, limiting the number of times the matcher can be used. + * If the route has already been called n times the route will be + * ignored and the call to fetch() will fall through to be handled by + * any other routes defined (which may eventually result in an error + * if nothing matches it). + */ + repeat?: number; + } + + type MockCall = [string, MockRequest]; + + interface MatchedRoutes { + matched: MockCall[]; + unmatched: MockCall[]; + } + + interface MockOptionsMethodGet extends MockOptions { + method?: 'GET'; + } + + interface MockOptionsMethodPost extends MockOptions { + method?: 'POST'; + } + + interface MockOptionsMethodPut extends MockOptions { + method?: 'PUT'; + } + + interface MockOptionsMethodDelete extends MockOptions { + method?: 'DELETE'; + } + + interface MockOptionsMethodHead extends MockOptions { + method?: 'HEAD'; + } + + interface FetchMockStatic { + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Calls to .mock() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + mock(matcher: MockMatcher, response: MockResponse | MockResponseFunction, options?: MockOptions): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Calls to .mock() can be chained. + * @param options The route to mock + */ + mock(options: MockOptions): this; + + /** + * Returns a drop-in mock for fetch which can be passed to other mocking + * libraries. It implements the full fetch-mock api and maintains its + * own state independent of other instances, so tests can be run in + * parallel. + */ + sandbox(): FetchMockSandbox; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() limited to being + * called one time only. Calls to .once() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Optional additional properties defining the route to mock + */ + once(matcher: MockMatcher, response: MockResponse | MockResponseFunction, options?: MockOptions): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the GET + * method. Calls to .get() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + get(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodGet): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the GET + * method and limited to being called one time only. Calls to .getOnce() + * can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + getOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodGet): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the POST + * method. Calls to .post() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + post(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodPost): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the POST + * method and limited to being called one time only. Calls to .postOnce() + * can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + postOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodPost): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the PUT + * method. Calls to .put() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + put(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodPut): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the PUT + * method and limited to being called one time only. Calls to .putOnce() + * can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + putOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodPut): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the + * DELETE method. Calls to .delete() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + delete(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodDelete): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the + * DELETE method and limited to being called one time only. Calls to + * .deleteOnce() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + deleteOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodDelete): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the HEAD + * method. Calls to .head() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + head(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the HEAD + * method and limited to being called one time only. Calls to .headOnce() + * can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + headOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the PATCH + * method. Calls to .patch() can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + patch(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this; + + /** + * Replaces fetch() with a stub which records its calls, grouped by + * route, and optionally returns a mocked Response object or passes the + * call through to fetch(). Shorthand for mock() restricted to the PATCH + * method and limited to being called one time only. Calls to .patchOnce() + * can be chained. + * @param matcher Condition for selecting which requests to mock + * @param response Configures the http response returned by the mock + * @param [options] Additional properties defining the route to mock + */ + patchOnce(matcher: MockMatcher, reponse: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this; + + /** + * Chainable method that defines how to respond to calls to fetch that + * don't match any of the defined mocks. It accepts the same types of + * response as a normal call to .mock(matcher, response). It can also + * take an arbitrary function to completely customise behaviour of + * unmatched calls. If .catch() is called without any parameters then + * every unmatched call will receive a 200 response. + * @param [response] Configures the http response returned by the mock + */ + catch(response?: MockResponse | MockResponseFunction): this; + + /** + * Chainable method that records the call history of unmatched calls, + * but instead of responding with a stubbed response, the request is + * passed through to native fetch() and is allowed to communicate + * over the network. Similar to catch(). + */ + spy(response?: MockResponse | MockResponseFunction): this; + + /** + * Chainable method that restores fetch() to its unstubbed state and + * clears all data recorded for its calls. + */ + restore(): this; + + /** + * Chainable method that clears all data recorded for fetch()'s calls + */ + reset(): this; + + /** + * Returns a promise that resolves once all fetches handled by fetch-mock + * have resolved. + */ + flush(): Promise; + + /** + * Returns all calls to fetch, grouped by whether fetch-mock matched + * them or not. + */ + calls(): MatchedRoutes; + + /** + * Returns all calls to fetch matching matcherName. + */ + calls(matcherName?: string): MockCall[]; + + /** + * Returns a Boolean indicating whether fetch was called and a route + * was matched (or a specific route if matcherName is passed). + * @param [matcherName] either the name of a route or equal to + * matcher.toString() for any unnamed route + */ + called(matcherName?: string): boolean; + + /** + * Returns a Boolean indicating whether fetch was called the expected + * number of times (or at least once if the route defines no expectation + * is set) for every route (or for a specific route if matcherName is + * passed). + * @param [matcherName] either the name of a route or equal to + * matcher.toString() for any unnamed route + */ + done(matcherName?: string): boolean; + + /** + * Returns the arguments for the last matched call to fetch (or the + * last call to specific route is matcherName is passed). + * @param [matcherName] either the name of a route or equal to + * matcher.toString() for any unnamed route + */ + lastCall(matcherName?: string): MockCall; + + /** + * Returns the url for the last matched call to fetch (or the last + * call to specific route is matcherName is passed). + * @param [matcherName] either the name of a route or equal to + * matcher.toString() for any unnamed route + */ + lastUrl(matcherName?: string): string; + + /** + * Returns the options for the last matched call to fetch (or the + * last call to a specific route is matcherName is passed). + * @param [matcherName] either the name of a route or equal to + * matcher.toString() for any unnamed route + */ + lastOptions(matcherName?: string): MockRequest; + + /** + * Set some global config options, which include + * sendAsJson [default `true`] - by default fetchMock will + * convert objects to JSON before sending. This is overrideable + * for each call but for some scenarios, e.g. when dealing with a + * lot of array buffers, it can be useful to default to `false` + */ + configure(opts: {}): void; + } + + interface FetchMockSandbox extends FetchMockStatic { + /** + * Also callable as fetch(). Use `typeof fetch` in your code to define + * a field that accepts both `fetch()` and a fetch-mock sandbox. + */ + (input?: string | Request , init?: RequestInit): Promise; + } +} + +declare var fetchMock: fetchMock.FetchMockStatic; +export = fetchMock; diff --git a/types/fetch-mock/v6/tsconfig.json b/types/fetch-mock/v6/tsconfig.json new file mode 100644 index 0000000000..ea63a3d716 --- /dev/null +++ b/types/fetch-mock/v6/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "fetch-mock": [ "fetch-mock/v6" ] + } + }, + "files": [ + "index.d.ts", + "fetch-mock-tests.ts" + ] +} diff --git a/types/fetch-mock/v6/tslint.json b/types/fetch-mock/v6/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/fetch-mock/v6/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }