Skip to content

Commit

Permalink
Support requestHandler for new requests. (#95)
Browse files Browse the repository at this point in the history
* Support requestHandler for incoming requests.

* Update lib/mock-axios-types.ts

Co-authored-by: Jan Beckmann <[email protected]>

* Update lib/mock-axios.ts

Co-authored-by: Jan Beckmann <[email protected]>

* Update test/index.spec.ts

Co-authored-by: Jan Beckmann <[email protected]>

* Update test/index.spec.ts

Co-authored-by: Jan Beckmann <[email protected]>

* Update test/index.spec.ts

Co-authored-by: Jan Beckmann <[email protected]>

* Update test/index.spec.ts

Co-authored-by: Jan Beckmann <[email protected]>

---------

Co-authored-by: Ruben Duiveman <[email protected]>
Co-authored-by: Jan Beckmann <[email protected]>
  • Loading branch information
3 people authored Mar 15, 2023
1 parent 691dc11 commit ee15490
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface InterceptorsStack {
onRejected?(error: any): any;
}

export type RequestHandler = (req: AxiosMockQueueItem) => void;

interface AxiosDefaults {
headers: any;
}
Expand Down Expand Up @@ -189,6 +191,14 @@ export interface AxiosMockAPI {
*/
reset: () => void;

/**
* Set a request handler that gets invoked every time a new request comes in
* The handler is invoked with the new request item
*
* @param handler the function to invoke with the new request item every time a new request comes in
*/
useRequestHandler: (handler: RequestHandler) => void;

Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
isCancel(value: any): boolean;
Expand Down
12 changes: 12 additions & 0 deletions lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import {
AxiosMockType,
HttpResponse,
InterceptorsStack,
RequestHandler,
} from "./mock-axios-types";

/** a FIFO queue of pending request */
const _pending_requests: AxiosMockQueueItem[] = [];
const _responseInterceptors: InterceptorsStack[] = [];
const _requestInterceptors: InterceptorsStack[] = [];

let _requestHandler: RequestHandler;

const processInterceptors = (data: any, stack: InterceptorsStack[], type: keyof InterceptorsStack) => {
return stack.map(({[type]: interceptor}) => interceptor)
.filter((interceptor) => !!interceptor)
Expand Down Expand Up @@ -62,6 +65,11 @@ const _newReq: (config?: any) => UnresolvedSynchronousPromise<any> = (config: an
}, _requestInterceptors, 'onFulfilled');

_pending_requests.push(requestConfig);

if (typeof _requestHandler === "function") {
_requestHandler(requestConfig);
}

return promise;
};

Expand Down Expand Up @@ -356,6 +364,10 @@ MockAxios.reset = () => {
MockAxios.interceptors.response.clear();
};

MockAxios.useRequestHandler = (handler: RequestHandler) => {
_requestHandler = handler;
}

MockAxios.Cancel = Cancel;
MockAxios.CancelToken = CancelToken;
MockAxios.isCancel = (u): u is Cancel => {
Expand Down
30 changes: 30 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';

import { SynchronousPromise } from "synchronous-promise";
import MockAxios from "../lib/index";
import { AxiosMockQueueItem } from '../lib/mock-axios-types';

describe("MockAxios", () => {
afterEach(() => {
Expand Down Expand Up @@ -602,4 +603,33 @@ describe("MockAxios", () => {
expect(() => cancelToken.token.throwIfRequested()).toThrow();
});
});

describe("requestHandler", () => {
it("provides a method to set up a requestHandler", () => {
expect(MockAxios).toHaveProperty("useRequestHandler");
});

it("invokes the requestHandler on every incoming request", () => {
const requestHandler = jest.fn();
MockAxios.useRequestHandler(requestHandler);

MockAxios.get();
MockAxios.post();

expect(requestHandler).toHaveBeenCalledTimes(2);
});

it("supplies the latest queue item to the requestHandler", () => {
let lastRequestHanderRequest: AxiosMockQueueItem;

MockAxios.useRequestHandler((req: AxiosMockQueueItem) => {
lastRequestHanderRequest = req;
});

MockAxios.post();

const lastPromise = MockAxios.post();
expect(lastRequestHanderRequest.promise).toBe(lastPromise);
});
})
});

0 comments on commit ee15490

Please sign in to comment.