Last active
May 29, 2017 20:21
-
-
Save byverdu/ed7031879b1724492fe92717c987976b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//tests and code for utils.js | |
const query = { | |
name: 'how I met your mother', | |
type: 'series' | |
}; | |
describe( 'Util helper methods', () => { | |
it( 'Util.getImdbId returns id for an Imdb search', () => { | |
return getImdbId( query ) | |
.then(( data ) => { | |
expect( data ).to.eql( 'tt1219024' ); | |
}); | |
}); | |
it( 'Util.getImdbData returns the imdb data', ( done ) => { | |
getImdbData( 'tt1219024' ) | |
.then(( imdbData ) => { | |
expect( imdbData ).to.eql( sampleTvshow ); | |
done(); | |
}) | |
.catch( done ); | |
}).timeout( 4000 ); | |
it( 'Util.resolveImdbCall returns the data for our search', ( done ) => { | |
resolveImdbCall( query ) | |
.then(( resp ) => { | |
expect( resp ).to.eql( imdbSerie ); | |
done(); | |
}) | |
.catch( done ); | |
}).timeout( 4000 ); | |
}); | |
// utils.js | |
const imdbApi = require( 'imdb' ); | |
const getIdForName = require( 'name-to-imdb' ); | |
const getImdbId = query => new Promise(( resolve, reject ) => { | |
getIdForName( query, ( err, data ) => { | |
if ( err ) reject( err ); | |
resolve( data ); | |
}); | |
}); | |
const getImdbData = imdbId => new Promise(( resolve, reject ) => { | |
imdbApi( imdbId, ( err, data ) => { | |
if ( err ) reject( err ); | |
resolve( data ); | |
}); | |
}); | |
const resolveImdbCall = query => new Promise(( resolve, reject ) => { | |
getImdbId( query ) | |
.then(( imdbId ) => { | |
getImdbData( imdbId ) | |
.then(( data ) => { | |
if ( data ) { | |
resolve( data ); | |
} else { | |
reject( 'promise failed' ); | |
} | |
}); | |
}); | |
}); | |
export { | |
resolveImdbCall | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment