Skip to content

Commit

Permalink
add interceptors usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yarilchenko committed Apr 13, 2022
1 parent cb28dfd commit 7be9efb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface HttpResponse {
}

interface Interceptor {
use: jest.Mock<number, [any?, any?]>;
use: jest.Mock<number, [onFulfilled?: (value: any) => any | Promise<any>, onRejected?: (error: any) => any]>;
eject: jest.Mock<void, [number]>;
}

Expand All @@ -18,6 +18,11 @@ interface Interceptors {
response: Interceptor;
}

export interface InterceptorsStack {
onFulfilled?(value: any): any | Promise<any>;
onRejected?(error: any): any;
}

interface AxiosDefaults {
headers: any;
}
Expand Down
30 changes: 24 additions & 6 deletions lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
*/

import {jest} from '@jest/globals';
import { SynchronousPromise, UnresolvedSynchronousPromise } from "synchronous-promise";

import { SynchronousPromise, UnresolvedSynchronousPromise } from "synchronous-promise";
import Cancel from "./cancel/Cancel";
import CancelToken from "./cancel/CancelToken";
import {
AxiosMockQueueItem,
AxiosMockRequestCriteria,
AxiosMockType,
HttpResponse,
InterceptorsStack,
} from "./mock-axios-types";

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

const _newReq: (config?: any) => UnresolvedSynchronousPromise<any> = (config: any = {}, actualConfig: any = {}) => {
if(typeof config === 'string') {
Expand Down Expand Up @@ -99,9 +101,13 @@ MockAxios.interceptors = {
},
response: {
// @ts-ignore
use: jest.fn(),
use: jest.fn((onFulfilled, onRejected) => {
return _responseInterceptors.push({onFulfilled, onRejected});
}),
// @ts-ignore
eject: jest.fn(),
eject: jest.fn((position: number) => {
_responseInterceptors.splice(position - 1, 1);
}),
},
};

Expand Down Expand Up @@ -163,6 +169,14 @@ const popQueueItem = (queueItem: SynchronousPromise<any> | AxiosMockQueueItem =
}
};

const processInterceptors = (data: any, type: keyof InterceptorsStack) => {
return _responseInterceptors.map(({[type]: interceptor}) => interceptor)
.filter((interceptor) => !!interceptor)
.reduce((_result, next) => {
return next(_result);
}, data);
}

MockAxios.mockResponse = (
response?: HttpResponse,
queueItem: SynchronousPromise<any> | AxiosMockQueueItem = null,
Expand All @@ -188,8 +202,10 @@ MockAxios.mockResponse = (
return;
}

const result = processInterceptors(response, 'onFulfilled');

// resolving the Promise with the given response data
promise.resolve(response);
promise.resolve(result);
};

MockAxios.mockResponseFor = (
Expand Down Expand Up @@ -228,8 +244,10 @@ MockAxios.mockError = (
error.isAxiosError = true;
}

const result = processInterceptors(error, 'onRejected');

// resolving the Promise with the given error
promise.reject(error);
promise.reject(result);
};

MockAxios.isAxiosError = (payload) => (typeof payload === 'object') && (payload.isAxiosError === true);
Expand Down

0 comments on commit 7be9efb

Please sign in to comment.