Skip to content

Commit

Permalink
Merge branch '5-interceptors'
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Mar 19, 2019
2 parents f7bf2cc + 852bbf1 commit 9ae8f44
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ The promise object returned by this function corresponds to the one returned by
`reset` method clears state of the Axios mock to initial values. It should be called after each test, so that we can start fresh with our next test (i.e. from `afterEach` method).

# Additional examples
Since AxiosMock is relatively simple, most of its functionality was covered in [basic example](#basic-example) at the begining of this document. In this section we'll explore features not covered by that initial example.
Since AxiosMock is relatively simple, most of its functionality was covered in [basic example](#basic-example) at the beginning of this document. In this section we'll explore features not covered by that initial example.

## Values returned by `lastReqGet` and `lastPromiseGet` methods

Expand Down Expand Up @@ -294,17 +294,21 @@ it('when resolving a request an appropriate handler should be called', () => {
```
Although this might not be the most realistic use-case of this functionality, it does illustrate how `lastReqGet` method can be used to alter the default behaviour of the `mockResponse` method.

**NOTE:** the identical effect can be achived by using the [`lastPromiseGet`](#axioslastpromiseget) method. These two methods perform a similar task, as described in the corresponding documentation.
**NOTE:** the identical effect can be achieved by using the [`lastPromiseGet`](#axioslastpromiseget) method. These two methods perform a similar task, as described in the corresponding documentation.

## Interceptors

AxiosMock offers basic support for interceptors (i.e. it does not break when interceptors are used in tested code). However, interceptors are not applied to the mocked requests / responses at the moment.

# Missing features
AxiosMock coveres the most popular parts of Axios API, meaning that some of the features are missing (i.e. interceptors).
AxiosMock covers the most popular parts of Axios API, meaning that some of the features are missing or only partially implemented (i.e. interceptors).

If you need an additional feature, you can request it by creating a new issue on [project's GitHub page](https://github.com/knee-cola/jest-mock-axios/issues/).

Also you are welcome to implement the missing feature yourself and make a pull request :)

# Synchronous promise
Tha magic which enables axio mock to work synchronously is hidden away in [`jest-mock-promise`](https://www.npmjs.com/package/jest-mock-promise), which enables promises to be settled in synchronous manner.
Tha magic which enables axios mock to work synchronously is hidden away in [`jest-mock-promise`](https://www.npmjs.com/package/jest-mock-promise), which enables promises to be settled in synchronous manner.

The [`jest-mock-promise`](https://www.npmjs.com/package/jest-mock-promise) can be used to mock any asyc component which uses promises.

Expand Down
10 changes: 10 additions & 0 deletions lib/mock-axios-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export type SpyFn = AnyFunction & { mockClear: AnyFunction };

export type AxiosFn = (...args: any[]) => SpyFn;

export type Interceptors = {
request: {
use: SpyFn
},
response: {
use: SpyFn
}
};

export interface AxiosAPI {
// mocking Axios methods
get: SpyFn;
Expand All @@ -26,6 +35,7 @@ export interface AxiosAPI {
options: SpyFn;
all: SpyFn;
create: SpyFn;
interceptors: Interceptors
}

export interface AxiosMockAPI {
Expand Down
9 changes: 9 additions & 0 deletions lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ MockAxios.head = jest.fn(_newReq);
MockAxios.options = jest.fn(_newReq);
MockAxios.create = jest.fn(() => MockAxios);

MockAxios.interceptors = {
request: {
use: jest.fn()
},
response: {
use: jest.fn()
}
}

MockAxios.popPromise = (promise?: SyncPromise) => {

if (promise) {
Expand Down
3 changes: 3 additions & 0 deletions test/UppercaseProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import axios from '../lib/index';

const UppercaseProxy = (clientMessage) => {

axios.interceptors.request.use((config) => config);
axios.interceptors.response.use((config) => config);

// requesting data from server
let axiosPromise = axios.post('/web-service-url/', { data: clientMessage });

Expand Down

0 comments on commit 9ae8f44

Please sign in to comment.