-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added @testing-library/react-hooks library to unit test custom hooks
Importing regenerator runtime to polyfill transpiled generator functions for react hooks testing library Added custom hook to fetch api data Added tests for custom hook and updated tests for edit.js
- Loading branch information
Showing
7 changed files
with
300 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
projects/plugins/jetpack/extensions/blocks/gif/hooks/use-fetch-giphy-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
const useFetchGiphyData = ( initialValue = [] ) => { | ||
const [ isFetching, setIsFetching ] = useState( false ); | ||
const [ giphyData, setGiphyData ] = useState( initialValue ); | ||
const [ fetchUrl, setFetchUrl ] = useState( '' ); | ||
|
||
useEffect( () => { | ||
if ( ! fetchUrl ) { | ||
return; | ||
} | ||
|
||
const fetchResults = async () => { | ||
setIsFetching( true ); | ||
|
||
const giphyFetch = await fetch( fetchUrl ) | ||
.then( ( response ) => { | ||
if ( response.ok ) { | ||
return response; | ||
} | ||
return false; | ||
} ) | ||
.catch( () => { | ||
return false; | ||
} ); | ||
|
||
if ( giphyFetch ) { | ||
const giphyResponse = await giphyFetch.json(); | ||
// If there is only one result, Giphy's API does not return an array. | ||
// The following statement normalizes the data into an array with one member in this case. | ||
const giphyResults = typeof giphyResponse.data.images !== 'undefined' ? [ giphyResponse.data ] : giphyResponse.data; | ||
|
||
// Try to grab the first result. We're going to show this as the main image. | ||
const firstResult = giphyResults[ 0 ]; | ||
|
||
// Check for results. | ||
if ( firstResult.images ) { | ||
setGiphyData( giphyResults ); | ||
} | ||
} | ||
setIsFetching( false ); | ||
}; | ||
|
||
fetchResults(); | ||
}, [ fetchUrl ] ); | ||
|
||
return { isFetching, giphyData, fetchGiphyData: setFetchUrl }; | ||
}; | ||
|
||
export default useFetchGiphyData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.