Gopal Jee purwar
Gopal Jee purwar

Reputation: 41

How to mocking a http request with route params using axios-mock-adapter

How to mock http request with route params?

var axios = require('axios');
var MockAdapter = require('axios-mock-adapter');

// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);
mock.onGet('/api/colleges/:collegeId/branches/:branchesId').reply(200);

Upvotes: 4

Views: 3670

Answers (1)

Dave Stewart
Dave Stewart

Reputation: 2534

Convert your placeholders to wildcards and the path to a RegExp:

function route (path = '') {
  return typeof path === 'string'
    ? new RegExp(path.replace(/:\w+/g, '[^/]+'))
    : path
}

Use like this:

mock.onGet(route('/api/colleges/:collegeId/branches/:branchesId')).reply(200);

Upvotes: 2

Related Questions