1.1.2 • Published 12 months ago

@klient/mock v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Klient Mock

badge-coverage

Introduction

This Klient extension allows you to mock Klient responses. That's means that you can work on an entrypoints even if it's not really ready yet.

Setup

Install package with your favorite package manager :

# With NPM
$ npm install @klient/mock

# With YARN
$ yarn add @klient/mock

Then import the extension in your code :

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient('...');


//
// See loaded extension
//
console.log(klient.extensions); // Print ["@klient/mock"]

Usage

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient(...);


//
// Mock a route - see Mock method doc for more details
//
klient.mock({
  req: { url: '/posts' },         // Request criterias
  res: { status: 200, data: [] }  // Mocked response
});


//
// Consume a mocked route
//
klient.request('/posts').then(response => {
  console.log(response.data); // Print []
});

Parameters

const klient = new Klient({
  delay: 800, // Default response delay to apply to all responses (milliseconds)
  mock: {
    // This parameter is analyzed only on Klient instanciation 
    load: [
      // Mock GET /posts
      {
        delay: 800,  // Simulate response delay (milliseconds)
        req: { url: '/posts' },
        res: { status: 200, data: [] }
      },
    ]
  }
});

Mock

A mock is a fake response build for a request whose matches some criterias.

The mocked response must be an object contening at least the "status" property. It must contains only Axios object properties, that will be "normalized" by mock extension to a final Axios result. The returned status code determines if the request must be rejected or resolved (as Axios does).

To match a mock, the request config must contain all properties/values defined in criterias (it's not strict, request config can contains more properties not defined in criterias). The mock request url can contains dynamic parameters whoses will be treated separately of the rest of config, then they will be injected in mock response callback if match.

mock({
  req: AxiosRequestConfig,
  res: AxiosResponse | ((config: AxiosRequestConfig, parameters: object) => AxiosResponse)
}): Klient

// Can be multiple
mock(...Mocks): Klient

Example

import Klient from '@klient/core';

//
// Register extension
//
import '@klient/mock';


//
// Build Klient instance
//
const klient = new Klient(...);


//
// Mock a route
//
klient.mock({
  req: { url: '/posts' },         // Request criterias
  res: { status: 200, data: [] }  // Mocked response
});


//
// Mock multiple routes
//
klient.mock(
  {
    req: { url: '/posts', method: 'GET' },
    res: { status: 200, data: [] }
  },
  {
    req: { url: '/posts', method: 'POST' },
    res: { status: 400, data: {...} }
  }
);


//
// Mock complexe routes
//
klient.mock({
  delay: 600, // (milliseconds)
  req: { url: '/posts', method: 'POST' },
  // Use a callback to make complex response
  res: (config) => {
    // Simulate API validation errors
    if (!config.data.title) {
      return { status: 400, data: {...} };
    }

    return { status: 200, data: {...} };
  }
});

//
// Mock routes with params
//
klient.mock({
  req: { url: '/posts/{id}', method: 'GET' },
  // Use a callback to make complex response
  res: (config, parameters) => {
    // Simulate API not found
    if (parameters.id !== '1') {
      return {
        status: 404,
        data: {...}
      };
    }

    return { status: 200, data: {...} };
  }
});
1.1.1

12 months ago

1.1.0

1 year ago

1.1.2

12 months ago

2.0.0

12 months ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago