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

support buffers when mocking responses #223

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@ If you are using [Create-React-App](https://github.com/facebookincubator/create-
### Functions

Instead of passing body, it is also possible to pass a function that returns a promise.
The promise should resolve with a string or an object containing body and init props
The promise should resolve with a string or an object containing body and init props, or a `Response` object.

i.e:

```js
fetch.mockResponse(() => callMyApi().then(res => ({ body: 'ok' })))
// OR
fetch.mockResponse(() => callMyApi().then(res => 'ok'))
// OR
fetch.mockResponse(() => callMyApi().then(res => new Response('foo')))
```

The function may take an optional "request" parameter of type `http.Request`:
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ if (typeof DOMException === 'undefined') {
const ActualResponse = Response

function responseWrapper(body, init) {
if (body instanceof ActualResponse) {
return body;
}

if (
body &&
typeof body.constructor === 'function' &&
Expand Down
19 changes: 19 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { StringDecoder } = require('string_decoder');
const { APIRequest, APIRequest2, defaultRequestUri, request } = require('./api')

describe('testing mockResponse and alias once', () => {
Expand Down Expand Up @@ -358,6 +359,24 @@ describe('request', () => {
fetch('https://bar', {}).then((response) => response.headers.get('ding'))
).resolves.toEqual('dang')
})

it('accepts a promise that resolves with a repsonse', () => {
fetch.mockResponseOnce(() => Promise.resolve(new Response(Buffer.from('foo'))))
return expect(
fetch('https://bar', {})
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => new StringDecoder('utf8').write(new Uint8Array(arrayBuffer)))
).resolves.toEqual('foo')
})

it('accepts a response', () => {
fetch.mockResponseOnce(new Response(Buffer.from('foo')))
return expect(
fetch('https://bar', {})
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => new StringDecoder('utf8').write(new Uint8Array(arrayBuffer)))
).resolves.toEqual('foo')
})
})

describe('conditional mocking', () => {
Expand Down
18 changes: 16 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ export interface FetchMock
// Response mocking
mockResponse(fn: MockResponseInitFunction): FetchMock;
mockResponse(response: string, responseInit?: MockParams): FetchMock;
mockResponse(response: Response): FetchMock;

mockResponseOnce(fn: MockResponseInitFunction): FetchMock;
mockResponseOnce(response: string, responseInit?: MockParams): FetchMock;
mockResponseOnce(response: Response): FetchMock;

// alias for mockResponseOnce
once(fn: MockResponseInitFunction): FetchMock;
once(response: Response): FetchMock;
once(url: string, responseInit?: MockParams): FetchMock;

mockResponses(...responses: Array<string | [string, MockParams] | MockResponseInitFunction>): FetchMock;
mockResponses(...responses: Array<string | Response | [string, MockParams] | MockResponseInitFunction>): FetchMock;

// Error/Reject mocking
mockReject(error?: ErrorOrFunction): FetchMock;
Expand All @@ -45,36 +48,47 @@ export interface FetchMock

doMock(fn?: MockResponseInitFunction): FetchMock;
doMock(response: string, responseInit?: MockParams): FetchMock;
doMock(response: Response): FetchMock;

doMockOnce(fn?: MockResponseInitFunction): FetchMock;
doMockOnce(response: string, responseInit?: MockParams): FetchMock;
doMockOnce(response: Response): FetchMock;
// alias for doMockOnce
mockOnce(fn?: MockResponseInitFunction): FetchMock;
mockOnce(response: string, responseInit?: MockParams): FetchMock;
mockOnce(response: Response): FetchMock;

doMockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
doMockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
doMockIf(urlOrPredicate: UrlOrPredicate, response: Response) : FetchMock;
// alias for doMockIf
mockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
mockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
mockIf(urlOrPredicate: UrlOrPredicate, response: Response) : FetchMock;

doMockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
doMockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
doMockOnceIf(urlOrPredicate: UrlOrPredicate, response: Response) : FetchMock;
// alias for doMocKOnceIf
mockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
mockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
mockOnceIf(urlOrPredicate: UrlOrPredicate, response: Response) : FetchMock;

dontMock(fn?: MockResponseInitFunction): FetchMock;
dontMock(response: string, responseInit?: MockParams): FetchMock;
dontMock(response: Response): FetchMock;

dontMockOnce(fn?: MockResponseInitFunction): FetchMock;
dontMockOnce(response: string, responseInit?: MockParams): FetchMock;
dontMockOnce(response: Response): FetchMock;

dontMockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
dontMockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
dontMockIf(urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;

dontMockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
dontMockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
dontMockOnceIf(urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;

resetMocks(): void;
enableMocks(): void;
Expand All @@ -98,7 +112,7 @@ export interface MockResponseInit extends MockParams {
export type ErrorOrFunction = Error | ((...args: any[]) => Promise<any>);
export type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);

export type MockResponseInitFunction = (request: Request) => MockResponseInit | string | Promise<MockResponseInit | string>;
export type MockResponseInitFunction = (request: Request) => MockResponseInit | string | Response | Promise<MockResponseInit | string | Response>;

// alias of fetchMock.enableMocks() for ES6 import syntax to not clash with other libraries
export function enableFetchMocks(): void;
Expand Down
13 changes: 13 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
]
});
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {});
fetchMock.mockResponse(new Response('foo'));
fetchMock.mockResponse(someAsyncHandler);
fetchMock.mockResponse(someAsyncStringHandler);
fetchMock.mockResponse(someAsyncResponseHandler)

fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}));
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {
Expand All @@ -19,8 +21,10 @@ fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {
]
});
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {});
fetchMock.mockResponseOnce(new Response('foo'));
fetchMock.mockResponseOnce(someAsyncHandler);
fetchMock.mockResponseOnce(someAsyncStringHandler);
fetchMock.mockResponseOnce(someAsyncResponseHandler)

fetchMock.once(JSON.stringify({foo: "bar"}));
fetchMock.once(JSON.stringify({foo: "bar"}), {
Expand All @@ -30,19 +34,24 @@ fetchMock.once(JSON.stringify({foo: "bar"}), {
]
});
fetchMock.once(JSON.stringify({foo: "bar"}), {});
fetchMock.once(new Response('foo'));
fetchMock.once(someAsyncHandler);
fetchMock.once(someAsyncResponseHandler);

fetchMock.mockResponses(JSON.stringify({}), JSON.stringify({foo: "bar"}));
fetchMock.mockResponses(someAsyncHandler, someAsyncHandler);
fetchMock.mockResponses(JSON.stringify({}), someAsyncHandler);
fetchMock.mockResponses(someAsyncHandler, JSON.stringify({}));
fetchMock.mockResponses(someAsyncHandler);
fetchMock.mockResponses([JSON.stringify({foo: "bar"}), {status: 200}]);
fetchMock.mockResponses(new Response('foo'))
fetchMock.mockResponses(JSON.stringify({}), new Response('foo'))
fetchMock.mockResponses(
someSyncHandler,
someAsyncHandler,
someSyncStringHandler,
someAsyncStringHandler,
someAsyncResponseHandler,
[JSON.stringify({foo: "bar"}), {status: 200}]
);

Expand Down Expand Up @@ -105,6 +114,10 @@ function someSyncStringHandler(): string {
return JSON.stringify({foo: "bar"});
}

async function someAsyncResponseHandler(): Promise<Response> {
return new Response(Buffer.from('foo'));
}

enableFetchMocks();
disableFetchMocks();
fm.enableMocks();
Expand Down