Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-fetch-hook

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-fetch-hook - npm Package Compare versions

Comparing version 1.0.4 to 1.0.6

6

package.json
{
"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);
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc