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

Make sure useFetch rejects with an Error type. #114

Merged
merged 3 commits into from
Sep 19, 2019
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
4 changes: 4 additions & 0 deletions packages/react-async/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,8 @@ type FetchRun<T> = {
run(): void
}

export class FetchError extends Error {
response: Response
}

export default Async
2 changes: 1 addition & 1 deletion packages/react-async/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Async from "./Async"
export { default as Async, createInstance } from "./Async"
export { default as useAsync, useFetch } from "./useAsync"
export { default as useAsync, useFetch, FetchError } from "./useAsync"
export default Async
export { statusTypes } from "./status"
export { default as globalScope } from "./globalScope"
Expand Down
9 changes: 8 additions & 1 deletion packages/react-async/src/useAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ const useAsync = (arg1, arg2) => {
)
}

export class FetchError extends Error {
constructor(response) {
super(`${response.status} ${response.statusText}`)
this.response = response
}
}

const parseResponse = (accept, json) => res => {
if (!res.ok) return Promise.reject(res)
if (!res.ok) return Promise.reject(new FetchError(res))
if (typeof json === "boolean") return json ? res.json() : res
return accept === "application/json" ? res.json() : res
}
Expand Down
22 changes: 19 additions & 3 deletions packages/react-async/src/useAsync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import "@testing-library/jest-dom/extend-expect"
import React from "react"
import { render, fireEvent, cleanup } from "@testing-library/react"
import { useAsync, useFetch, globalScope } from "./index"
import { useAsync, useFetch, globalScope, FetchError } from "./index"
import {
sleep,
resolveTo,
Expand All @@ -20,11 +20,11 @@ const abortCtrl = { abort: jest.fn(), signal: "SIGNAL" }
globalScope.AbortController = jest.fn(() => abortCtrl)

const json = jest.fn(() => ({}))
globalScope.fetch = jest.fn(() => Promise.resolve({ ok: true, json }))
globalScope.fetch = jest.fn()

beforeEach(abortCtrl.abort.mockClear)
beforeEach(globalScope.fetch.mockClear)
beforeEach(json.mockClear)
beforeEach(() => globalScope.fetch.mockReset().mockResolvedValue({ ok: true, json }))
afterEach(cleanup)

const Async = ({ children = () => null, ...props }) => children(useAsync(props))
Expand Down Expand Up @@ -250,4 +250,20 @@ describe("useFetch", () => {
expect.objectContaining({ preventDefault: expect.any(Function) })
)
})

test("throws a FetchError for failed requests", async () => {
const errorResponse = { ok: false, status: 400, statusText: "Bad Request", json }
globalScope.fetch.mockResolvedValue(errorResponse)
const onResolve = jest.fn()
const onReject = jest.fn()
render(<Fetch input="/test" options={{ onResolve, onReject }} />)
expect(globalScope.fetch).toHaveBeenCalled()
await sleep(10)
expect(onResolve).not.toHaveBeenCalled()
expect(onReject).toHaveBeenCalled()
let [err] = onReject.mock.calls[0]
expect(err).toBeInstanceOf(FetchError)
expect(err.message).toEqual("400 Bad Request")
expect(err.response).toBe(errorResponse)
})
})