MockRequests

MockRequests

Source:
MockRequests will mock both `XMLHttpRequest` and `fetch` such that one single configure call, and the entire app is provided with mocks. URLs not configured will be unaffected and still trigger an async request as normal.

Members

(static) OriginalXHR :XMLHttpRequest|undefined

Source:
Original XMLHttpRequest class, as defined in the global environment.
Type:
  • XMLHttpRequest | undefined

(static) originalFetch :function|undefined

Source:
Original fetch function, as defined in the global environment.
Type:
  • function | undefined

Methods

(static) configure(apiUrlResponseConfig, overwritePreviousConfigopt)

Source:
Initialize the mock with response objects.
Parameters:
Name Type Attributes Default Description
apiUrlResponseConfig Object.<string, JsonPrimitive> Config object containing URL strings as keys and respective mock response objects as values
overwritePreviousConfig boolean <optional>
true If the map from a previous configure call should be overwritten by this call (true) or not (false)

(static) configureDynamicResponses(dynamicApiUrlResponseConfig, overwritePreviousConfigopt)

Source:
Initialize the mock with response objects and their dynamic update functions
Parameters:
Name Type Attributes Default Description
dynamicApiUrlResponseConfig Object.<string, MockResponseConfig> URL-MockResponseConfig mappings
overwritePreviousConfig boolean <optional>
true If the map from a previous configure call should be overwritten by this call (true) or not (false)

(static) setMockUrlResponse(url, response)

Source:
Mock any network requests to the given URL using the given responseObject
Parameters:
Name Type Default Description
url string URL to mock
response JsonPrimitive null Mock response object

(static) setDynamicMockUrlResponse(url, mockResponseConfig)

Source:
Mock any network requests to the given URL using the given responseObject and dynamic response modification function
Parameters:
Name Type Description
url string URL to mock
mockResponseConfig MockResponseConfig Config object with the fields desired to be configured

(static) getResponse(url) → {JsonPrimitive}

Source:
Get the mock response object associated with the passed URL
Parameters:
Name Type Description
url string URL that was previously mocked
Returns:
- Configured response object
Type
JsonPrimitive

(static) deleteMockUrlResponse(url) → {boolean}

Source:
Deletes the URL and respective mock object
Parameters:
Name Type Description
url string URL that was previously mocked
Returns:
- Value returned from `delete Object.url`
Type
boolean

(static) clearAllMocks()

Source:
Deletes all entries in the MockRequests configuration

(static) mapStaticConfigToDynamic(staticConfig) → {Object.<string, MockResponseConfig>}

Source:
Reformats a static URL-response config object to match the dynamic MockResponseConfig object structure
Parameters:
Name Type Description
staticConfig Object.<string, JsonPrimitive> URL-staticResponse map
Returns:
- URL-MockResponseConfig object with default configuration fields
Type
Object.<string, MockResponseConfig>

Type Definitions

JsonPrimitive

Source:

DynamicResponseModFn

Source:

MockResponseConfig

Source: