MockServer

A backend "server" to be used for creating jsonapi-compliant responses.

Constructor

new MockServer(options)

Sets properties needed internally

  • factoryFarm: a pre-existing factory to use on this server
  • responseOverrides: An array of alternative responses that can be used to override the ones that would be served from the internal store.
Parameters:
NameTypeDescription
optionsobject

currently responseOverrides and factoriesForTypes

Methods

add(name, options) → {*}

Alias for this._backendFactoryFarm.add

Parameters:
NameTypeDescription
namestring

the name to use for the factory

optionsobject

properties and other options for adding a model to the store

Returns:

Object or Array

Type: 
*

build(factoryName, overrideOptions, numberOfRecords) → {*}

Alias for this._backendFactoryFarm.build

Parameters:
NameTypeDescription
factoryNamestring

the name of the factory to use

overrideOptionsobject

overrides for the factory

numberOfRecordsnumber

optional number of models to build

Returns:

Object or Array

Type: 
*

define(name, options) → {*}

Alias for this._backendFactoryFarm.define

Parameters:
NameTypeDescription
namestring

the name to use for the factory

optionsobject

options for defining a factory

Returns:

Object or Array

Type: 
*

respond(options)

Adds a response override to the server

Parameters:
NameTypeDescription
optionsobject

path, method, status, and response to override

  • path
  • method: defaults to GET
  • status: defaults to 200
  • response: a method that takes the server as an argument and returns the body of the response

start(options)

Sets up fetch mocking to intercept requests. It will then either use overrides, or use its own internal store to simulate serving JSON responses of new data.

  • responseOverrides: An array of alternative responses that can be used to override the ones that would be served from the internal store.
  • factoriesForTypes: A key map that can be used to build factories if a queried id does not exist
Parameters:
NameTypeDescription
optionsobject

currently responseOverrides and factoriesForTypes

stop()

Clears mocks and the store