Skip to content

Commit

Permalink
fix(jsonError): update to handle NO CONTENT response
Browse files Browse the repository at this point in the history
  • Loading branch information
sowmyab9 authored Mar 1, 2021
1 parent 40916f5 commit 030f886
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
38 changes: 38 additions & 0 deletions packages/fetchye-core/__tests__/defaultFetcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('defaultFetcher', () => {
headers: new global.Headers({
'Content-Type': 'application/json',
}),
clone: () => ({
ok: true,
status: 200,
headers: new global.Headers({
'Content-Type': 'application/json',
}),
}),
}));
const data = await defaultFetcher(fetchClient, 'http://example.com');
expect(data).toMatchInlineSnapshot(`
Expand All @@ -52,6 +59,11 @@ describe('defaultFetcher', () => {
status: 200,
json: async () => true,
headers: undefined,
clone: () => ({
ok: true,
status: 200,
headers: undefined,
}),
}));
const data = await defaultFetcher(fetchClient, 'http://example.com');
expect(data).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -79,4 +91,30 @@ describe('defaultFetcher', () => {
}
`);
});
it('should return payload and undefined error when status 204 (No content)', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
status: 204,
json: async () => { throw new SyntaxError('Unexpected end of JSON input'); },
headers: new global.Headers({}),
clone: () => ({
ok: true,
status: 204,
headers: new global.Headers({}),
text: async () => '',
}),
}));
const data = await defaultFetcher(fetchClient, 'http://example.com');
expect(data).toMatchInlineSnapshot(`
Object {
"error": undefined,
"payload": Object {
"body": "",
"headers": Object {},
"ok": true,
"status": 204,
},
}
`);
});
});
3 changes: 2 additions & 1 deletion packages/fetchye-core/src/defaultFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const defaultFetcher = async (fetchClient, key, options) => {
let error;
try {
res = await fetchClient(key, options);
const body = await res.json();
const responseClone = res.clone();
const body = await res.json().catch(() => responseClone.text());
payload = {
body,
ok: res.ok,
Expand Down
7 changes: 7 additions & 0 deletions packages/fetchye/__tests__/makeServerFetchye.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const defaultPayload = {
json: async () => ({
fakeData: true,
}),
clone: () => ({
headers: new global.Headers({
'Content-Type': 'application/json',
}),
ok: true,
status: 200,
}),
};

describe('makeServerFetchye', () => {
Expand Down
49 changes: 48 additions & 1 deletion packages/fetchye/__tests__/useFetchye.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ const defaultPayload = {
json: async () => ({
fakeData: true,
}),
clone: () => ({
headers: new global.Headers({
'Content-Type': 'application/json',
}),
ok: true,
status: 200,
}),
};

const ReduxSetup = (props) => {
Expand Down Expand Up @@ -113,6 +120,44 @@ describe('useFetchye', () => {
}
`);
});
it('should return data success state when response is empty (204 no content)', async () => {
let fetchyeRes;
global.fetch = jest.fn(async () => ({
...defaultPayload,
status: 204,
json: async () => { throw new SyntaxError('Unexpected end of JSON input'); },
clone: () => ({
ok: true,
status: 204,
headers: new global.Headers({}),
text: async () => '',
}),
}));
render(
<AFetchyeProvider cache={cache}>
{React.createElement(() => {
fetchyeRes = useFetchye('http://example.com');
return null;
})}
</AFetchyeProvider>
);
await waitFor(() => fetchyeRes.isLoading === false);
expect(fetchyeRes).toMatchInlineSnapshot(`
Object {
"data": Object {
"body": "",
"headers": Object {
"content-type": "application/json",
},
"ok": true,
"status": 204,
},
"error": undefined,
"isLoading": false,
"run": [Function],
}
`);
});
it('should delay execution when awaiting the existence of a variable', async () => {
let fetchyeResOne;
let fetchyeResTwo;
Expand Down Expand Up @@ -295,7 +340,9 @@ describe('useFetchye', () => {
].forEach(([name, AFetchyeProvider]) => {
describe(name, () => {
it('ensures fetch is called once per key', async () => {
const fakeFetchClient = jest.fn();
const fakeFetchClient = jest.fn({
clone: () => {},
});
global.fetch = fakeFetchClient;
render(
<AFetchyeProvider cache={cache}>
Expand Down

0 comments on commit 030f886

Please sign in to comment.