From e5586dd6cfb5c462e8e3b14b4cb42c2b2fe4f14d Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Thu, 8 Aug 2024 19:56:25 +0100 Subject: [PATCH] docs: merge fetchHandler docs with mocking and spying page --- docs/docs/@fetch-mock/core/fetchHandler.md | 14 ----------- .../@fetch-mock/core/mocking-and-spying.md | 24 ++++++++++++------- .../@fetch-mock/core/more-routing-methods.md | 2 ++ 3 files changed, 18 insertions(+), 22 deletions(-) delete mode 100644 docs/docs/@fetch-mock/core/fetchHandler.md diff --git a/docs/docs/@fetch-mock/core/fetchHandler.md b/docs/docs/@fetch-mock/core/fetchHandler.md deleted file mode 100644 index 64b01b290..000000000 --- a/docs/docs/@fetch-mock/core/fetchHandler.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -sidebar_position: 1 -sidebar_label: .fetchHandler ---- - -# fetchHandler - -`fetchMock.fetchHandler(url, requestInit)` - -A mock implementation of `fetch`. - -By default it will error. In order to return responses `.route()`, `.catch()` and other convenience methods of `fetchMock` must first be used to add routes to its internal router. - -All calls made using `fetchMock.fetchHandler` are recorded in `fetchMock.callHistory`. diff --git a/docs/docs/@fetch-mock/core/mocking-and-spying.md b/docs/docs/@fetch-mock/core/mocking-and-spying.md index 26f023786..863374e90 100644 --- a/docs/docs/@fetch-mock/core/mocking-and-spying.md +++ b/docs/docs/@fetch-mock/core/mocking-and-spying.md @@ -1,29 +1,37 @@ --- -sidebar_position: 3 +sidebar_position: 1 --- # Mocking and spying These methods allow mocking or spying on the `fetch` implementation used by your application. -Note that these methods are only implemented in `@fetch-mock/core` and are not avilable when using `@fetch-mock/jest`, `@fetch-mock/vitest` etc.... Those libraries provide ways to mock `fetch` that are more idiomatic to their own ecosystem. +## fetchHandler -## When using global fetch in your application +`fetchMock.fetchHandler(url, requestInit)` -### mockGlobal() +A mock implementation of `fetch`. + +By default it will error. In order to return responses `.route()`, `.catch()` and other routing methods of `fetchMock` must first be used to add routes to its internal router. + +All calls made using `fetchMock.fetchHandler` are recorded in `fetchMock.callHistory`. + +You can either pass `fetchMock.fetchHandler` into your choice of mocking library or use the methods below to mock global `fetch`. + +## mockGlobal() Replaces `globalThis.fetch` with `fm.fetchHandler` -### unmockGlobal() +## unmockGlobal() Restores `globalThis.fetch` to its original state -### spy(matcher, name) +## spy(matcher, name) Falls back to the `fetch` implementation set in `fetchMock.config.fetch` for a specific route (which can be named). -When no arguments are provided it will fallback to the native fetch implementation for all requests, similar to `.catch()` +When no arguments are provided it will fallback to the native fetch implementation for all requests, similar to `.catch()`. -### spyGlobal() +## spyGlobal() Equivalent to calling `.mockGlobal()` followed by `.spy()` diff --git a/docs/docs/@fetch-mock/core/more-routing-methods.md b/docs/docs/@fetch-mock/core/more-routing-methods.md index bfa6b7b6d..006add9aa 100644 --- a/docs/docs/@fetch-mock/core/more-routing-methods.md +++ b/docs/docs/@fetch-mock/core/more-routing-methods.md @@ -15,6 +15,8 @@ Specifies how to respond to calls to `fetch` that don't match any routes. It accepts any [response](#api-mockingmock_response) compatible with `.route()`. If no argument is passed, then every unmatched call will receive a `200` response. +See also [.spy()](/fetch-mock/docs/@fetch-mock/core/mocking-and-spying#spymatcher-name) + ## .sticky() Shorthand for `mock()` which creates a route that persists even when `restore()`, `reset()` or `resetbehavior()` are called;