Last active
May 30, 2017 23:03
-
-
Save byverdu/57eb36c0ef35ddb6154c66028c57bb18 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
// test/server-test/serverSpec.js | |
// Test cases for routing | |
import { expect } from 'chai'; | |
import request from 'supertest'; | |
import mongoose from 'mongoose'; | |
import { ImdbSchema } from '../../app/server/models/ImdbSchema'; | |
import server from '../../app/server/'; | |
import sampleData from '../sampleData'; | |
let movie; | |
let movieId; | |
let Imdb; | |
const { imdbMovie, imdbSerie } = sampleData; | |
before(( done ) => { | |
Imdb = mongoose.model( 'Imdb', ImdbSchema ); | |
movie = new Imdb( imdbMovie ); | |
movie.save() | |
.then(( imdb ) => { | |
movieId = imdb._id; | |
done(); | |
}); | |
}); | |
after(() => { | |
Imdb.remove({ title: 'Rambo' }).exec(); | |
}); | |
describe( 'Routing test cases', () => { | |
describe( 'Api endpoint', () => { | |
it( 'should get all movies', ( done ) => { | |
request( server ) | |
.get( '/api/all/movie' ) | |
.expect( 200 ) | |
.end(( err, res ) => { | |
if ( err ) done( err ); | |
expect( res.type ).to.include( 'json' ); | |
expect( res.body ).to.be.an( 'array' ); | |
expect( res.body[ 0 ].type ).to.eql( 'movie' ); | |
done(); | |
}); | |
}); | |
it( 'a new movie can be add', ( done ) => { | |
request( server ) | |
.post( '/api/add' ) | |
.send({ data: imdbMovie }) | |
.expect( 200 ) | |
.end(( err, res ) => { | |
if ( err ) done( err ); | |
expect( res.type ).to.include( 'json' ); | |
expect( res.body ).to.be.an( 'object' ); | |
expect( res.body.data ).to.eql( 'Rambo has been saved' ); | |
done(); | |
}); | |
}); | |
it( 'A serie can be searched', ( done ) => { | |
request( server ) | |
.post( '/api/search/' ) | |
.query( 'q=Castle' ) | |
.query( 't=series' ) | |
.expect( 200 ) | |
.end(( err, res ) => { | |
console.log(res) | |
if ( err ) done( err ); | |
expect( res.type ).to.include( 'json' ); | |
expect( res.body ).to.be.an( 'object' ); | |
expect( res.body ).to.eql( imdbSerie ); | |
done(); | |
}); | |
}).timeout( 7000 ); | |
// ... resto de tests | |
}); | |
// methods for API | |
// app/server/api/index.js | |
import mongoose from 'mongoose'; | |
import { ImdbSchema } from '../models/ImdbSchema'; | |
import { resolveImdbCall } from '../../../utils'; | |
const Imdb = mongoose.model( 'Imdb', ImdbSchema ); | |
const getAll = ( req, res ) => { | |
Imdb.find({ type: req.params.imdb }) | |
.exec() | |
.then(( response ) => { | |
res.type( 'json' ); | |
res.json( response ); | |
}); | |
}; | |
const updateById = ( req, res ) => { | |
const postId = req.params.id; | |
const movieRating = req.body.rating; | |
Imdb.findOne({ _id: `${postId}` }) | |
.exec() | |
.then(( movie ) => { | |
movie.setMyRating( movieRating ); | |
movie.save() | |
.then(() => { | |
res.json({ | |
data: `${movieRating} rating for ${movie.title} has been saved`, | |
movie | |
}); | |
}); | |
}); | |
}; | |
const searchImdb = ( req, res ) => { | |
const query = { | |
name: req.query.q, | |
type: req.query.t | |
}; | |
resolveImdbCall( query ) | |
.then( resp => res.json( resp )); | |
}; | |
// .... resto metodos | |
export { | |
getAll, | |
updateById, | |
searchImdb | |
}; | |
// controller para las rutas | |
// app/server/routes/api.js | |
import * as api from '../api'; | |
module.exports = ( router ) => { | |
router.get( '/api/all/:imdb', api.getAll ); | |
router.post( '/api/search', api.searchImdb ); | |
router.put( '/api/update/:id', api.updateById ); | |
return router; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment