Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 671
Gopal Jee purwar Avatar asked Sep 15 '25 06:09

Gopal Jee purwar


1 Answers

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);
like image 133
Dave Stewart Avatar answered Sep 17 '25 03:09

Dave Stewart