|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiHttp from 'chai-http'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import app from '../src/index'; |
| 5 | +import db from '../src/database/models'; |
| 6 | +import users from './mockData/mockAuth'; |
| 7 | +import ratings from './mockData/mockRatings'; |
| 8 | + |
| 9 | +const { expect } = chai; |
| 10 | +chai.use(chaiHttp); |
| 11 | + |
| 12 | +const baseURL = '/api/v1'; |
| 13 | + |
| 14 | +const { Rating } = db; |
| 15 | +const { user10 } = users; |
| 16 | +const { accomodationRatings, userOne } = ratings; |
| 17 | + |
| 18 | +let validToken; |
| 19 | + |
| 20 | +// check successful post |
| 21 | +describe('Ratings', () => { |
| 22 | + describe('Users post a request to rate an accomodation', () => { |
| 23 | + it('should return a valid token', (done) => { |
| 24 | + chai |
| 25 | + .request(app) |
| 26 | + .post(`${baseURL}/auth/login`) |
| 27 | + .send(user10) |
| 28 | + .end((err, res) => { |
| 29 | + const { user } = res.body.data; |
| 30 | + // eslint-disable-next-line prefer-destructuring |
| 31 | + validToken = user.token; |
| 32 | + done(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + it('should succesfully update if previous rating is found for an accomodation', (done) => { |
| 36 | + chai |
| 37 | + .request(app) |
| 38 | + .post(`${baseURL}/accomodation/ratings`) |
| 39 | + .send(accomodationRatings[1]) |
| 40 | + .set('authorization', validToken) |
| 41 | + .end((err, res) => { |
| 42 | + expect(res).to.have.status(200); |
| 43 | + expect(res.body.message).to.eq('Thank you for rating this accomodation'); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + it('should succesfully rate an accomodation', (done) => { |
| 48 | + chai |
| 49 | + .request(app) |
| 50 | + .post(`${baseURL}/accomodation/ratings`) |
| 51 | + .send(accomodationRatings[0]) |
| 52 | + .set('authorization', validToken) |
| 53 | + .end((err, res) => { |
| 54 | + expect(res).to.have.status(201); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + it('should return an error if accomodation does not exist', (done) => { |
| 59 | + chai |
| 60 | + .request(app) |
| 61 | + .post(`${baseURL}/accomodation/ratings`) |
| 62 | + .send(accomodationRatings[2]) |
| 63 | + .set('authorization', validToken) |
| 64 | + .end((err, res) => { |
| 65 | + expect(res).to.have.status(404); |
| 66 | + expect(res.body).to.be.an('object'); |
| 67 | + done(err); |
| 68 | + }); |
| 69 | + }); |
| 70 | + it('should return an error if accomodation Id is not supplied', (done) => { |
| 71 | + chai |
| 72 | + .request(app) |
| 73 | + .post(`${baseURL}/accomodation/ratings`) |
| 74 | + .send(accomodationRatings[3]) |
| 75 | + .set('authorization', validToken) |
| 76 | + .end((err, res) => { |
| 77 | + expect(res).to.have.status(400); |
| 78 | + expect(res.body).to.be.an('object'); |
| 79 | + expect(res.body.message).to.eql('Validation Error!'); |
| 80 | + expect(res.body.data.accomodationId).to.eql('Accomodation Id is required'); |
| 81 | + done(err); |
| 82 | + }); |
| 83 | + }); |
| 84 | + it('should return an error if rating value is not supplied', (done) => { |
| 85 | + chai |
| 86 | + .request(app) |
| 87 | + .post(`${baseURL}/accomodation/ratings`) |
| 88 | + .send(accomodationRatings[4]) |
| 89 | + .set('authorization', validToken) |
| 90 | + .end((err, res) => { |
| 91 | + expect(res).to.have.status(400); |
| 92 | + expect(res.body).to.be.an('object'); |
| 93 | + expect(res.body.message).to.eql('Validation Error!'); |
| 94 | + expect(res.body.data.ratingValue).to.eql('Rating is required'); |
| 95 | + done(err); |
| 96 | + }); |
| 97 | + }); |
| 98 | + it('should return an error if rating is greater than 5', (done) => { |
| 99 | + chai |
| 100 | + .request(app) |
| 101 | + .post(`${baseURL}/accomodation/ratings`) |
| 102 | + .send(accomodationRatings[5]) |
| 103 | + .set('authorization', validToken) |
| 104 | + .end((err, res) => { |
| 105 | + expect(res).to.have.status(400); |
| 106 | + expect(res.body).to.be.an('object'); |
| 107 | + expect(res.body.message).to.eql('Validation Error!'); |
| 108 | + expect(res.body.data.ratingValue).to.eql('Ratings should be between the number 0 to 5'); |
| 109 | + done(err); |
| 110 | + }); |
| 111 | + }); |
| 112 | + it('should return a 500 error when an error occurs on the server', (done) => { |
| 113 | + const stub = sinon.stub(Rating, 'update') |
| 114 | + .rejects(new Error('Server error, Please try again later')); |
| 115 | + chai.request(app) |
| 116 | + .post(`${baseURL}/accomodation/ratings`) |
| 117 | + .send(accomodationRatings[0]) |
| 118 | + .set('authorization', validToken) |
| 119 | + .end((err, res) => { |
| 120 | + expect(res.status).to.equal(500); |
| 121 | + stub.restore(); |
| 122 | + done(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments