Skip to content

Commit

Permalink
Merge pull request #46 from sergiop/bugfix/handle-undefined-pagination
Browse files Browse the repository at this point in the history
Bugfix/handle undefined pagination
  • Loading branch information
sergiop authored Dec 24, 2020
2 parents c4c1847 + 3fbfeea commit beca4f0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 65 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 11 additions & 7 deletions src/hooks/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
27 changes: 27 additions & 0 deletions src/hooks/useApi.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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('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,
})
})
})
8 changes: 8 additions & 0 deletions tests/fixtures/giphySearchGetSuccessMissingPagination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": [],
"meta": {
"status": 200,
"msg": "OK",
"response_id": "5d41782a7350472f6ff3ad31"
}
}
56 changes: 0 additions & 56 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected], 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"
Expand Down Expand Up @@ -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"

[email protected], figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
Expand Down Expand Up @@ -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==

[email protected]:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -9842,11 +9800,6 @@ [email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit beca4f0

Please sign in to comment.