@@ -4,6 +4,7 @@ import Review from '../src/models/review';
44import User from '../src/models/user' ;
55import { describe , it } from 'mocha' ;
66import last5 from '../src/api/Vehicle/findById' ;
7+ import findByVehicle from '../src/api/Review/findByVehicle' ;
78import assert from 'assert' ;
89import sinon from 'sinon' ;
910
@@ -60,4 +61,51 @@ describe('Vehicle', function() {
6061 assert . equal ( res . json . getCall ( 0 ) . args [ 0 ] . reviews . length , 5 ) ;
6162 assert ( res . json . getCall ( 0 ) . args [ 0 ] . reviews [ 0 ] . text . endsWith ( '6' ) ) ;
6263 } ) ;
64+
65+ it ( 'Should find all the reviews for the given vehicleId, adhering to the skip and limit parameters' , async function ( ) {
66+ const mockRequest = ( body ) => ( {
67+ body
68+ } )
69+ const mockResponse = ( ) : ResponseStub => {
70+ const res : ResponseStub = {
71+ status : sinon . stub ( ) . returnsThis ( ) ,
72+ json : sinon . stub ( ) . returnsThis ( )
73+ } ;
74+ return res ;
75+ } ;
76+ const user = await User . create ( {
77+ email : 'test@localhost.com' ,
78+ firstName : 'Test' ,
79+ lastName : 'Testerson'
80+ } ) ;
81+ const vehicle = await Vehicle . create (
82+ {
83+ make : 'Tesla' ,
84+ model : 'Model S' ,
85+ year : 2022 ,
86+ images : [
87+ 'https://tesla-cdn.thron.com/delivery/public/image/tesla/6139697c-9d6a-4579-837e-a9fc5df4a773/bvlatuR/std/1200x628/Model-3-Homepage-Social-LHD' ,
88+ 'https://www.tesla.com/sites/default/files/images/blogs/models_blog_post.jpg'
89+ ] ,
90+ numReviews : 0 ,
91+ averageReview : 0
92+ } ,
93+ ) ;
94+ for ( let i = 0 ; i < 7 ; i ++ ) {
95+ await Review . create ( {
96+ rating : i > 5 ? 5 : i ,
97+ text : 'This is a review that must have length greater than 30. ' + i ,
98+ vehicleId : vehicle . _id ,
99+ userId : user . _id
100+ } ) ;
101+ }
102+ vehicle . numReviews = 6 ;
103+ vehicle . averageReview = 3 ;
104+ await vehicle . save ( ) ;
105+ const req = mockRequest ( { vehicleId : vehicle . _id , limit : 0 , skip : 0 } ) ;
106+ const res = mockResponse ( ) ;
107+ await findByVehicle ( req , res ) ;
108+ assert . equal ( res . json . getCall ( 0 ) . args [ 0 ] . reviews . length , 7 ) ;
109+ assert ( res . json . getCall ( 0 ) . args [ 0 ] . reviews [ 0 ] . text . endsWith ( '6' ) ) ;
110+ } ) ;
63111} ) ;
0 commit comments