react-fetch-hook
Advanced tools
Comparing version 1.0.4 to 1.0.6
{ | ||
"name": "react-fetch-hook", | ||
"version": "1.0.4", | ||
"version": "1.0.6", | ||
"description": "React fetch hook", | ||
@@ -17,5 +17,3 @@ "main": "./dist/index.js", | ||
"jest": { | ||
"setupFiles": [ | ||
"./setupTests.js" | ||
], | ||
"setupTestFrameworkScriptFile": "./setupTests.js", | ||
"moduleNameMapper": { | ||
@@ -22,0 +20,0 @@ "\\.(svg|png)$": "<rootDir>/__mocks__/fileMock.js" |
@@ -15,6 +15,6 @@ import React from "react"; | ||
const result = useFetch("https://google.com"); | ||
return <div>{result.isLoading}</div>; | ||
return result.data && result.data.data; | ||
}; | ||
const { rerender } = render(<Component />); | ||
const { container, rerender } = render(<Component />); | ||
@@ -25,2 +25,3 @@ await wait(() => { | ||
expect(fetch.mock.calls.length).toEqual(1); | ||
expect(container).toHaveTextContent("12345"); | ||
expect(fetch.mock.calls[0][0]).toEqual("https://google.com"); | ||
@@ -54,2 +55,31 @@ }); | ||
}); | ||
it("call with url, options with formatter", async () => { | ||
fetch.mockResponse(JSON.stringify({ data: "12345" })); | ||
const options = { | ||
headers: { | ||
Accept: "application/json, application/xml, text/plain, text/html, *.*", | ||
"Content-Type": "application/json; charset=utf-8" | ||
} | ||
}; | ||
const formatterMock = jest.fn(); | ||
formatterMock.mockReturnValueOnce("xxx"); | ||
const Component = () => { | ||
const result = useFetch("https://google.com", { ...options, formatter: formatterMock }); | ||
return result.data; | ||
}; | ||
const { container, rerender } = render(<Component />); | ||
await wait(() => { | ||
rerender(<Component />); | ||
expect(fetch.mock.calls.length).toEqual(1); | ||
expect(formatterMock.mock.calls.length).toEqual(1); | ||
expect(container).toHaveTextContent("xxx"); | ||
expect(fetch.mock.calls[0][0]).toEqual("https://google.com"); | ||
expect(fetch.mock.calls[0][1]).toMatchObject({ ...options }); | ||
}); | ||
}); | ||
}); |
@@ -15,8 +15,12 @@ // @flow | ||
const defaultFormatter = response => response.json(); | ||
const fetchInstance = (path, options) => { | ||
return fetch(path, options).then( | ||
(options && typeof options.formatter === "function" && options.formatter) || defaultFormatter | ||
); | ||
const fetchInstance = formatter => (path, options) => { | ||
return fetch(path, options).then((typeof formatter === "function" && formatter) || defaultFormatter); | ||
}; | ||
return usePromise(fetchInstance, path, options); | ||
if (options) { | ||
const { formatter, ...fetchOptions } = options; | ||
return usePromise(fetchInstance(formatter), path, fetchOptions); | ||
} else { | ||
return usePromise(fetchInstance(), path); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13389
234
14