Skip to content

Commit

Permalink
feat(hardening): ensure that an instance is provided to mock
Browse files Browse the repository at this point in the history
properly test expected constructor error
  • Loading branch information
symptomatik committed Apr 19, 2021
1 parent 103f8a5 commit a326853
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ function resetHistory() {
function MockAdapter(axiosInstance, options) {
reset.call(this);

// TODO throw error instead when no axios instance is provided
if (axiosInstance) {
this.axiosInstance = axiosInstance;
this.originalAdapter = axiosInstance.defaults.adapter;
this.delayResponse =
options && options.delayResponse > 0 ? options.delayResponse : null;
this.onNoMatch = (options && options.onNoMatch) || null;
axiosInstance.defaults.adapter = this.adapter.call(this);
} else {
throw new Error("Please provide an instance of axios to mock");
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe("MockAdapter basics", function () {
expect(instance.defaults.adapter).to.exist;
});

it("correctly throws an error when attempting to instantiate an undefined axios instance", function () {
var emptyInstance = undefined;
var constructorFunc = function () {
new MockAdapter(emptyInstance);
};
expect(constructorFunc).to.throw(
"Please provide an instance of axios to mock"
);
});

it("calls interceptors", function () {
instance.interceptors.response.use(
function (config) {
Expand Down

0 comments on commit a326853

Please # to comment.