Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
"eslint": "^6.8.0",
"eslint-config-developit": "^1.1.1",
"file-loader": "^5.0.2",
"isomorphic-fetch": "^2.2.1",
"jest": "^24.9.0",
"karmatic": "^1.4.0",
"microbundle": "^0.11.0",
"sinon": "^8.0.4",
"webpack": "^4.41.5"
}
}
}
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export default (function create(defaults) {
}
}
}


const fetchFunc = options.fetch || fetch;
const customHeaders = {};

if (data && typeof data === 'object') {
Expand All @@ -168,7 +169,7 @@ export default (function create(defaults) {
const response = {};
response.config = config;

return fetch(url, {
return fetchFunc(url, {
method: options.method,
body: data,
headers: deepMerge(options.headers, customHeaders, true)
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import axios from '../src/index.js';
import textExample from 'file-loader!./fixtures/example.txt';
import jsonExample from 'file-loader!./fixtures/example.json.txt';
import fetch from 'isomorphic-fetch';

describe('redaxios', () => {
it('smoke test', () => {
Expand Down Expand Up @@ -73,4 +74,13 @@ describe('redaxios', () => {
window.fetch = oldFetch;
}
});

it('should accept a custom fetch implementation', async () => {
const req = axios.get(jsonExample, { fetch });
expect(req).toBeInstanceOf(Promise);
const res = await req;
expect(res).toBeInstanceOf(Object);
expect(res.status).toEqual(200);
expect(JSON.parse(res.data)).toEqual({ hello: 'world' });
});
});