From e999905fbd83fc8a6cc463d2cf4edd952c703130 Mon Sep 17 00:00:00 2001 From: Sergio Pedercini Date: Thu, 24 Dec 2020 12:15:36 +0100 Subject: [PATCH 1/4] If api returns 200 with undef pagination, show a generic error. --- src/hooks/useApi.js | 18 ++++++++----- src/hooks/useApi.spec.js | 27 +++++++++++++++++++ ...iphySearchGetSuccessMissingPagination.json | 8 ++++++ 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/giphySearchGetSuccessMissingPagination.json diff --git a/src/hooks/useApi.js b/src/hooks/useApi.js index 7f5425b..03249cf 100644 --- a/src/hooks/useApi.js +++ b/src/hooks/useApi.js @@ -27,19 +27,23 @@ const useApi = () => { return response.json() }) .then(response => { + if (!response.pagination) { + return dispatch({ type: 'FETCH_FAILURE' }) + } + if (isMore) { - dispatch({ + return dispatch({ type: 'FETCH_MORE_SUCCESS', payload: response.data, pagination: response.pagination, }) - } else { - dispatch({ - type: 'FETCH_SUCCESS', - payload: response.data, - pagination: response.pagination, - }) } + + return dispatch({ + type: 'FETCH_SUCCESS', + payload: response.data, + pagination: response.pagination, + }) }) .catch(() => { dispatch({ type: 'FETCH_FAILURE' }) diff --git a/src/hooks/useApi.spec.js b/src/hooks/useApi.spec.js index 54b32b8..6802132 100644 --- a/src/hooks/useApi.spec.js +++ b/src/hooks/useApi.spec.js @@ -1,6 +1,7 @@ import { renderHook, act } from '@testing-library/react-hooks' import giphyTrendingGet404Error from '../../tests/fixtures/giphyTrendingGet404Error.json' import giphyTrendingGetSuccess from '../../tests/fixtures/giphyTrendingGetSuccess.json' +import giphySearchGetSuccessMissingPagination from '../../tests/fixtures/giphySearchGetSuccessMissingPagination.json' import useApi from './useApi' describe('useApi', () => { @@ -84,4 +85,30 @@ describe('useApi', () => { lastPage: false, }) }) + + // It has been reported that sometimes Giphy API, probably due to a bug, return a malformed + // response, missing the pagination key. + test.only('perform a get request and receive a response without the pagination', async () => { + const { result, waitForNextUpdate } = renderHook(() => useApi()) + const [, fetchImages] = result.current + + window.fetch = jest.fn().mockResolvedValueOnce({ + ok: true, + status: 200, + json: async () => giphySearchGetSuccessMissingPagination, + }) + + act(() => { + fetchImages() + }) + + expect(result.current[0]).toEqual(fetchingInitValues) + await waitForNextUpdate() + expect(result.current[0]).toEqual({ + loading: false, + error: true, + data: [], + lastPage: false, + }) + }) }) diff --git a/tests/fixtures/giphySearchGetSuccessMissingPagination.json b/tests/fixtures/giphySearchGetSuccessMissingPagination.json new file mode 100644 index 0000000..c980ae0 --- /dev/null +++ b/tests/fixtures/giphySearchGetSuccessMissingPagination.json @@ -0,0 +1,8 @@ +{ + "data": [], + "meta": { + "status": 200, + "msg": "OK", + "response_id": "5d41782a7350472f6ff3ad31" + } +} From 906aa2786897e51cd26c839a159ae5a0467f0a0e Mon Sep 17 00:00:00 2001 From: Sergio Pedercini Date: Thu, 24 Dec 2020 12:15:57 +0100 Subject: [PATCH 2/4] Remove unused fetch-mock-jest. --- package.json | 1 - yarn.lock | 56 ---------------------------------------------------- 2 files changed, 57 deletions(-) diff --git a/package.json b/package.json index 35af334..44a4735 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "eslint-plugin-prettier": "^3.1.0", "eslint-plugin-react": "^7.13.0", "eslint-plugin-react-hooks": "^2.5.0", - "fetch-mock-jest": "^1.2.7", "flow-bin": "^0.119.1", "flow-typed": "^3.0.0", "gh-pages": "^2.2.0", diff --git a/yarn.lock b/yarn.lock index 3e01cb4..eb1f438 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4109,11 +4109,6 @@ core-js@^2.2.0, core-js@^2.4.0, core-js@^2.5.0: resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.9.tgz#6b4b214620c834152e179323727fc19741b084f2" integrity sha512-HOpZf6eXmnl7la+cUdMnLvUxKNqLUzJvgIziQ0DiF3JwSImNphIqdGqzj6hIKyX04MmV0poclQ7+wjWvxQyR2A== -core-js@^3.0.0: - version "3.6.5" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.5.tgz#7395dc273af37fb2e50e9bd3d9fe841285231d1a" - integrity sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA== - core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -5574,28 +5569,6 @@ fd-slicer@~1.0.1: dependencies: pend "~1.2.0" -fetch-mock-jest@^1.2.7: - version "1.2.7" - resolved "https://registry.yarnpkg.com/fetch-mock-jest/-/fetch-mock-jest-1.2.7.tgz#b62e96de710516f2f7188a80e6c4ea45bdec4d10" - integrity sha512-6MUyXCBQE41OAL529b2l5XJGRspsu22NWRHVBrZx1v+/VZM9LyaQeGNHMGbbULQb6wR5znAsAfnLaIEwzHxqgA== - dependencies: - fetch-mock "^9.0.0" - -fetch-mock@^9.0.0: - version "9.4.0" - resolved "https://registry.yarnpkg.com/fetch-mock/-/fetch-mock-9.4.0.tgz#9be073577bcfa57af714ca91f7536aff8450ec88" - integrity sha512-tqnFmcjYheW5Z9zOPRVY+ZXjB/QWCYtPiOrYGEsPgKfpGHco97eaaj7Rv9MjK7PVWG4rWfv6t2IgQAzDQizBZA== - dependencies: - babel-runtime "^6.26.0" - core-js "^3.0.0" - debug "^4.1.1" - glob-to-regexp "^0.4.0" - is-subset "^0.1.1" - lodash.isequal "^4.5.0" - path-to-regexp "^2.2.1" - querystring "^0.2.0" - whatwg-url "^6.5.0" - figures@2.0.0, figures@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962" @@ -6124,11 +6097,6 @@ glob-parent@^5.0.0: dependencies: is-glob "^4.0.1" -glob-to-regexp@^0.4.0: - version "0.4.1" - resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e" - integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw== - glob@7.1.2: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" @@ -7324,11 +7292,6 @@ is-string@^1.0.5: resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.5.tgz#40493ed198ef3ff477b8c7f92f644ec82a5cd3a6" integrity sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ== -is-subset@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/is-subset/-/is-subset-0.1.1.tgz#8a59117d932de1de00f245fcdd39ce43f1e939a6" - integrity sha1-ilkRfZMt4d4A8kX83TnOQ/HpOaY= - is-symbol@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.2.tgz#a055f6ae57192caee329e7a860118b497a950f38" @@ -8282,11 +8245,6 @@ lodash.get@^4.4.2: resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99" integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk= -lodash.isequal@^4.5.0: - version "4.5.0" - resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0" - integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" @@ -9842,11 +9800,6 @@ path-to-regexp@0.1.7: resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w= -path-to-regexp@^2.2.1: - version "2.4.0" - resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-2.4.0.tgz#35ce7f333d5616f1c1e1bfe266c3aba2e5b2e704" - integrity sha512-G6zHoVqC6GGTQkZwF4lkuEyMbVOjoBKAEybQUypI1WTkqinCOrq2x6U2+phkJ1XsEMTy4LjtwPI7HW+NVrRR2w== - path-type@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441" @@ -12884,15 +12837,6 @@ whatwg-mimetype@^2.2.0, whatwg-mimetype@^2.3.0: resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf" integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g== -whatwg-url@^6.5.0: - version "6.5.0" - resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.5.0.tgz#f2df02bff176fd65070df74ad5ccbb5a199965a8" - integrity sha512-rhRZRqx/TLJQWUpQ6bmrt2UV4f0HCQ463yQuONJqC6fO2VoEb1pTYddbe59SkYq87aoM5A3bdhMZiUiVws+fzQ== - dependencies: - lodash.sortby "^4.7.0" - tr46 "^1.0.1" - webidl-conversions "^4.0.2" - whatwg-url@^7.0.0: version "7.1.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-7.1.0.tgz#c2c492f1eca612988efd3d2266be1b9fc6170d06" From 73f0fc2f71900dc014f87a50b0ac74944d24e308 Mon Sep 17 00:00:00 2001 From: Sergio Pedercini Date: Thu, 24 Dec 2020 15:23:44 +0100 Subject: [PATCH 3/4] Remove 'only' from a test. --- src/hooks/useApi.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useApi.spec.js b/src/hooks/useApi.spec.js index 6802132..fc9435f 100644 --- a/src/hooks/useApi.spec.js +++ b/src/hooks/useApi.spec.js @@ -88,7 +88,7 @@ describe('useApi', () => { // It has been reported that sometimes Giphy API, probably due to a bug, return a malformed // response, missing the pagination key. - test.only('perform a get request and receive a response without the pagination', async () => { + test('perform a get request and receive a response without the pagination', async () => { const { result, waitForNextUpdate } = renderHook(() => useApi()) const [, fetchImages] = result.current From 3fbfeea193d81f443675f573b4cec248d348f822 Mon Sep 17 00:00:00 2001 From: Sergio Pedercini Date: Thu, 24 Dec 2020 15:24:25 +0100 Subject: [PATCH 4/4] Bump to v1.5.2. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 44a4735..5ff0491 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-giphy-searchbox", - "version": "1.5.1", + "version": "1.5.2", "description": "React Giphy Searchbox", "main": "lib/index.js", "module": "es/index.js",