Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add correct interceptors usage #82

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 35 additions & 10 deletions lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@
*/

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 _requestInterceptors: InterceptorsStack[] = [];

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

const _newReq: (config?: any) => UnresolvedSynchronousPromise<any> = (config: any = {}, actualConfig: any = {}) => {
if(typeof config === 'string') {
Expand All @@ -42,13 +53,15 @@ const _newReq: (config?: any) => UnresolvedSynchronousPromise<any> = (config: an
})
}

_pending_requests.push({
const requestConfig = processInterceptors({
config,
data,
method,
promise,
url
});
}, _requestInterceptors, 'onFulfilled');

_pending_requests.push(requestConfig);
return promise;
};

Expand Down Expand Up @@ -93,15 +106,23 @@ MockAxios.create = jest.fn(() => MockAxios);
MockAxios.interceptors = {
request: {
// @ts-ignore
use: jest.fn(),
use: jest.fn((onFulfilled, onRejected) => {
return _requestInterceptors.push({ onFulfilled, onRejected })
}),
// @ts-ignore
eject: jest.fn(),
eject: jest.fn((position: number) => {
_requestInterceptors.splice(position - 1, 1);
}),
},
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 @@ -188,8 +209,10 @@ MockAxios.mockResponse = (
return;
}

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

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

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

const result = processInterceptors(error, _responseInterceptors, '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