1.3.0 • Published 2 years ago
@codexcentral/mock-axios-request v1.3.0
MockAxiosRequest
Functions to allow test the request and responses in Axios format.
Installation
npm install @codexcentral/mock-axios-request
Usage
1. Importing and creating a new instance
import { MockApiClient, MockApiClientConfig } from '@codexcentral/mock-axios-request';
const baseUrl = 'https://example.com/api';
const mockApi = new MockApiClient(baseUrl);2. Mocking a request
GET
try {
  const config: MockApiClientConfig = {
    endpoint: '/users',
    status: 200, // Change this for the status you want to test
    response: [
      { id: 1, name: 'User 1' },
      { id: 2, name: 'User 2' },
      { id: 3, name: 'User 3' },
      { id: 4, name: 'User 4' },
    ],
  };
  const data = await mockApi.get<[{ id: string; name: string }]>(config);
  console.log('Data:', data);
} catch (error) {
  console.error('Error:', error);
}POST
try {
  const config: MockApiClientConfig = {
    endpoint: '/users/5',
    status: 201, // Change this for the status you want to test
    request: { name: 'User 5' },
    response: { id: 5, name: 'User 5' },
  };
  const data = await mockApi.post<{ id: string; name: string }>(config);
  console.log('Data:', data);
} catch (error) {
  console.error('Error:', error);
}PUT
try {
  const config: MockApiClientConfig = {
    endpoint: '/users/5',
    status: 200, // Change this for the status you want to test
    request: { name: 'User 5' },
    response: { id: 5, name: 'User 5' },
  };
  const data = await mockApi.put<{ id: string; name: string }>(config);
  console.log('Data:', data);
} catch (error) {
  console.error('Error:', error);
}DELETE
try {
  const config: MockApiClientConfig = {
    endpoint: '/users/5',
    status: 200, // Change this for the status you want to test
    response: { id: 5, name: 'User 5' },
  };
  const data = await mockApi.delete<{ id: string; name: string }>(config);
  console.log('Data:', data);
} catch (error) {
  console.error('Error:', error);
}MockApiClientConfig
| Attribute | Type | Mandatory | 
|---|---|---|
| endpoint | string | true | 
| status | number | true (100 to 599) | 
| headers | array | false | 
| response | array | false | 
| response | array | false | 
| delayResponse | number | false (default: 1000 - in milliseconds) | 
Example of MockApiClientConfig
{
  "endpoint": "/users",
  "status": 200,
  "headers": [],
  "response": [],
  "response": [],
  "delayResponse": 1000
}Credits
These code was written by Roberto Silva Z.