Skip to content

Commit

Permalink
Merge pull request #44 from sergiop/bugfix/api-error-handling
Browse files Browse the repository at this point in the history
Fix a bug that prevents to handle API errors.
  • Loading branch information
sergiop authored Dec 23, 2020
2 parents 7eec11c + f82331e commit c4c1847
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-giphy-searchbox",
"version": "1.5.0",
"version": "1.5.1",
"description": "React Giphy Searchbox",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ const useApi = () => {
}

fetch(url)
.then(response => response.json())
.then(response => {
if (!response.ok) {
return response.json().then(json => {
throw json
})
}

return response.json()
})
.then(response => {
if (isMore) {
dispatch({
Expand Down
56 changes: 31 additions & 25 deletions src/hooks/useApi.spec.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,82 @@
import fetchMock from 'fetch-mock-jest'
import { renderHook, act } from '@testing-library/react-hooks'
import giphyTrendingGet404Error from '../../tests/fixtures/giphyTrendingGet404Error.json'
import giphyTrendingGetSuccess from '../../tests/fixtures/giphyTrendingGetSuccess.json'
import useApi from './useApi'

describe('useApi', () => {
const fetchingValues = {
const fetchingInitValues = {
loading: true,
error: false,
data: [],
lastPage: false,
}

fetchMock.mock('/foo', {
status: 200,
data: [{ foo: 'foo' }],
pagination: {
total_count: 200,
count: 25,
offset: 0,
},
})

fetchMock.mock('/foo2', { status: 500 })

test('perform a get request and receive some data', async () => {
const { result, waitForNextUpdate } = renderHook(() => useApi())

const [, fetchImages] = result.current

window.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphyTrendingGetSuccess,
})

act(() => {
fetchImages('/foo')
fetchImages()
})

expect(result.current[0]).toEqual(fetchingValues)
expect(result.current[0]).toEqual(fetchingInitValues)
await waitForNextUpdate()
expect(result.current[0]).toEqual({
loading: false,
error: false,
data: [{ foo: 'foo' }],
data: giphyTrendingGetSuccess.data,
lastPage: false,
})
})

test('perform a get request with `isMore` option and receive some data', async () => {
const { result, waitForNextUpdate } = renderHook(() => useApi())

const [, fetchImages] = result.current

window.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphyTrendingGetSuccess,
})

act(() => {
fetchImages('/foo', true)
fetchImages('', true)
})

expect(result.current[0]).toEqual(fetchingValues)
expect(result.current[0]).toEqual(fetchingInitValues)
await waitForNextUpdate()
expect(result.current[0]).toEqual({
loading: false,
error: false,
data: [{ foo: 'foo' }],
data: giphyTrendingGetSuccess.data,
lastPage: false,
})
})

test('perform a get request and receive an error', async () => {
const { result, waitForNextUpdate } = renderHook(() => useApi())
const [, fetchImages] = result.current

const [, fetch] = result.current
window.fetch = jest.fn().mockResolvedValueOnce({
ok: false,
status: 404,
json: async () => giphyTrendingGet404Error,
})

act(() => {
fetch('/foo2')
fetchImages()
})

expect(result.current[0]).toEqual(fetchingValues)
expect(result.current[0]).toEqual(fetchingInitValues)

await waitForNextUpdate()

expect(result.current[0]).toEqual({
loading: false,
error: true,
Expand Down
47 changes: 30 additions & 17 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import assetsPoweredByGiphy from '../src/assets/poweredByGiphy.png'

// TO-DO: Test the loading more (infinite scrolling)

const fetchMock = data => {
return Promise.resolve({
json: () => Promise.resolve(data),
})
}

describe('ReactGiphySearchbox', () => {
const onSelect = jest.fn()
const onSearch = jest.fn()
Expand Down Expand Up @@ -51,12 +45,13 @@ describe('ReactGiphySearchbox', () => {
})

test('fetches Giphy Api and displays trending gifs', async () => {
window.fetch = jest
.fn()
.mockImplementationOnce(() => fetchMock(giphyTrendingGetSuccess))
window.fetch = jest.fn().mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphyTrendingGetSuccess,
})

const { getByTestId } = buildSubject()

// Loading message displayed
const Loader = await waitForElement(() => getByTestId('SpinnerText'))
expect(Loader).toHaveTextContent(defaults.messageLoading)
Expand All @@ -71,9 +66,11 @@ describe('ReactGiphySearchbox', () => {
})

test('fetches Giphy Api and returns an error', async () => {
window.fetch = jest
.fn()
.mockRejectedValueOnce(() => fetchMock(giphyTrendingGet404Error))
window.fetch = jest.fn().mockResolvedValueOnce({
ok: false,
status: 404,
json: async () => giphyTrendingGet404Error,
})
const { getByTestId } = buildSubject()

expect(getByTestId('SpinnerText')).toHaveTextContent(
Expand All @@ -91,8 +88,16 @@ describe('ReactGiphySearchbox', () => {
let Loader
window.fetch = jest
.fn()
.mockImplementationOnce(() => fetchMock(giphyTrendingGetSuccess))
.mockImplementationOnce(() => fetchMock(giphySearchGetSuccess))
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphyTrendingGetSuccess,
})
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphySearchGetSuccess,
})

const { getByTestId } = buildSubject()

Expand Down Expand Up @@ -132,8 +137,16 @@ describe('ReactGiphySearchbox', () => {
let Loader
window.fetch = jest
.fn()
.mockImplementationOnce(() => fetchMock(giphyTrendingGetSuccess))
.mockImplementationOnce(() => fetchMock(giphySearchGetSuccessEmpty))
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphyTrendingGetSuccess,
})
.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => giphySearchGetSuccessEmpty,
})

const { getByTestId } = buildSubject()

Expand Down

0 comments on commit c4c1847

Please sign in to comment.