# API

# Default

The main export of this API is the Server object, which is extended for any API mocking. Important components of the object to take node of are:

  • data - Method for declaring data in mocked server by default.
  • api - Method for declaring Functionality for mocked endpoints.
  • model - Method for abstracting common GET/PUT/DELETE functionality for models.
  • collection - Method for abstracting common GET/POST functionality for models.

# class Server()

Abstract base class for mocking server data. New mock servers can be created by inheriting this class via `extends` and overriding the `data()` and `api()` class methods.

# function index(max)

Function for returning index of new item in server. Can be overridden to use UUID indexes.

Parameters

  • max (integer) - Current max index in table.

# function get(model, id)

Get model data for specified id, accounting for relationship definitions between models.

Parameters

  • model (string) - Model name.
  • id (integer) - Model key/identifier.

# function collection(model, exclude, relation, key)

Generate default request processors for collection endpoints, overriding the `get` and `post` handlers.

Parameters

  • model (string) - Database model.
  • exclude (array) - Model keys to exclude from response payload.
  • relation (string) - Relation to subset queries by.
  • key (string) - Foreign key on relation linking model and relation.

# function model(model, exclude, relation, key)

Generate default request processors for model endpoints, overriding the `get`, `put`, and `delete` handlers.

Parameters

  • model (string) - Database model.
  • exclude (array) - Model keys to exclude from response payload.
  • relation (string) - Relation to subset queries by.
  • key (string) - Foreign key on model linking model and relation.

# function singleton(model, exclude)

Generate default request processors for singleton model endpoints, overridding the `get`, `put`, and `delete` handlers.

Parameters

  • model (object) - Database model.
  • exclude (array) - Model keys to exclude from response payload.

# function data()

Method for defining internal database that will be used throughout requests. This method allows users to configure an initial `state` for the database and all internal data models.

# function api()

Method returning server endpoints with get/post/put/delete request processing callables.

# function reset(model)

Reset internal database for server mock to original state.

Parameters

  • model (object) - Database model to reset.

# function dump()

Dump current state of database into json object.

# function init()

Initialize server mock and create fake callables for all axios requests. This method should be called before tests run or at the beginning of a test session.

# Database

Internally, data are managed via Model objects that provide utilities for crud operations on database models. Below are examples of how these model objects are used under the hood for a Server object (see the example in the Guide section for context):

// get model with rendered functions and id
server.db.posts.get(1);

// update model
server.db.posts.update(1, { title: 'Foo', body: 'bar' });

// add new model to collection (resolving id)
server.db.posts.add({ title: 'Foo', body: 'bar' });

// remove model from collection
server.db.posts.remove(1);

# class Collection(name, data, index)

Create a new Collection.

Parameters

  • name (string) - Name of model.
  • data (array) - Data to store.
  • index (function) - Indexing function for model ids.

# function get(id)

Get single item from collection.

Parameters

  • id (number) - Identifier for model to get from database.

# function all()

Get all models in collection.

# function add(data)

Add data to collection.

Parameters

  • data (object) - Data to add to collection.

# function update()

Update data for model.

# function remove(id)

Remove record from collection.

Parameters

  • id (string) - Identifier for record.

# class Singleton(name, data)

Singleton class for managing singleton objects. This objects provides some proxy methods for interacting with faux databases provided by this package.

Parameters

  • name (string) - Name of model.
  • data (array) - Data to store.

# function update()

Update data for model.

# function get()

Get formatted collection results.

# function all()

Get formatted collection results.

# Errors

Below are error classes that can be used in in the api service for returning responses. Trowing any of these errors in an endpoint callable will propagate to the UI. For example:

import { Server } from 'jest-axios';
import { NotFound, Forbidden } from 'jest-axios/errors';

class App extends Server {

  data() {
    return {
      todos: [
        { name: 'foo', done: false },
        { name: 'bar', done: true },
    };
  }

  api() {
    return {
      '/todos': this.collection('todos'),
      '/todos/:id': {
          get: this.model('todos').get,
          post: () => throw NotFound(),
          delete: () => throw Forbidden(),
      },
    };
  }
}

# function NotFound(url, method)

Generate promise response for missing resource.

Parameters

  • url (string) - Url to reject.
  • method (string) - Request method to reject.

# function Forbidden(url)

Generate promise response for missing page.

Parameters

  • url (string) - Url to reject.

# function Missing(id)

Generate promise response for missing resource.

Parameters

  • id (object) - Id of missing resource.