Code Duplication    Length = 162-167 lines in 2 locations

tests/mutations/beer.mutations.test.js 1 location

@@ 1-167 (lines=167) @@
1
var chai     = require('chai');
2
var chaiHttp = require('chai-http');
3
var should   = chai.should();
4
5
var server      = require('../../index');
6
var Beer        = require('../../models/beer.model');
7
var Brewery     = require('../../models/brewery.model');
8
var BeerData    = require('../../beers.json');
9
var BreweryData = require('../../breweries.json');
10
11
chai.use(chaiHttp);
12
13
describe('GraphQL Beer (mutations)', function() {
14
15
  beforeEach(function(done){
16
    BreweryData.forEach(function (aBeer) {
17
      var newBrewery = new Brewery(aBeer);
18
      newBrewery.save();
19
    });
20
    BeerData.forEach(function (aBeer) {
21
      var newBeer = new Beer(aBeer);
22
      newBeer.save();
23
    });
24
    done();
25
  });
26
27
  afterEach(function(done){
28
    Beer.collection.drop();
29
    Brewery.collection.drop();
30
    done();
31
  });
32
33
  it('should add a new beer', function(done) {
34
35
    var newBrewery = new Brewery({
36
      _id: 'test_brewery',
37
      name: 'Test Brewery',
38
      location: 'Beerland'
39
    });
40
41
    newBrewery.save();
42
43
    var graphqlQuery = `
44
      mutation {
45
        addBeer(data: {name: "Beer Test Triple", brewery: "test_brewery", alcohol: 8.5})
46
      }
47
    `;
48
49
    chai.request(server)
50
      .post('/graphql')
51
      .send({query: graphqlQuery})
52
      .end(function(err, res){
53
        res.should.have.status(200);
54
        res.should.be.json;
55
        res.body.data.addBeer.should.be.true;
56
      });
57
58
    // find this new beer
59
    var graphqlQuery = `
60
    {
61
      beers(brewery: "test_brewery") {
62
        name
63
        brewery {
64
          name
65
        }
66
        alcohol
67
      }
68
    }`;
69
70
    chai.request(server)
71
      .post('/graphql')
72
      .send({query: graphqlQuery})
73
      .end(function(err, res){
74
        res.should.have.status(200);
75
        res.body['data']['beers'][0]['name'].should.equal('Beer Test Triple');
76
        res.body['data']['beers'][0]['brewery'].name.should.equal('Test Brewery');
77
        res.body['data']['beers'][0]['alcohol'].should.equal(8.5);
78
        done();
79
      });
80
  });
81
82
  it('should delete a SINGLE beer', function(done) {
83
    var newBeer = new Beer({
84
      name: 'Custom Beer',
85
      brewery: 'Custom Brewery',
86
      alcohol: 9.5,
87
      description: 'hummmmmmm'
88
    });
89
90
    newBeer.save(function(err, data) {
91
92
      var graphqlQuery = `
93
      mutation {
94
        removeBeer(_id: "${data._id}") {
95
          name
96
        }
97
      }`;
98
99
      chai.request(server)
100
        .post('/graphql')
101
        .send({query: graphqlQuery})
102
        .end(function(err, res){
103
          res.should.have.status(200);
104
          res.should.be.json;
105
          res.body.should.be.an('object');
106
          res.body.data.removeBeer.name.should.equal('Custom Beer');
107
          done();
108
        });
109
110
      // try to retrieve the deleted beer
111
      var graphqlQuery = `
112
      {
113
        beer(id: "${data._id}") {
114
          name
115
        }
116
      }`;
117
118
      chai.request(server)
119
        .post('/graphql')
120
        .send({query: graphqlQuery})
121
        .end(function(err, res){
122
          res.should.have.status(200);
123
          res.should.be.json;
124
          res.body.should.be.an('object');
125
          res.body.data.beer.should.be.null;
126
          done();
127
        });
128
    });
129
  });
130
131
  it('should delete all beers', function(done) {
132
133
    var graphqlQuery = `
134
      mutation {
135
        removeAllBeers
136
      }
137
    `;
138
139
    chai.request(server)
140
      .post('/graphql')
141
      .send({query: graphqlQuery})
142
      .end(function(err, res){
143
        res.should.have.status(200);
144
        res.should.be.json;
145
        res.body.data.removeAllBeers.should.be.true;
146
      });
147
148
    // retrieve all beers
149
    var graphqlQuery = `
150
    {
151
      beers {
152
        name
153
      }
154
    }`;
155
156
    chai.request(server)
157
      .post('/graphql')
158
      .send({query: graphqlQuery})
159
      .end(function(err, res){
160
        res.should.have.status(200);
161
        res.body.data.beers.should.be.an('array');
162
        res.body.data.beers.should.have.lengthOf(0);
163
        done();
164
      });
165
  });
166
167
});
168

tests/queries/beer.queries.test.js 1 location

@@ 1-162 (lines=162) @@
1
var chai     = require('chai');
2
var chaiHttp = require('chai-http');
3
var should   = chai.should();
4
5
var server      = require('../../index');
6
var Beer        = require('../../models/beer.model');
7
var Brewery     = require('../../models/brewery.model');
8
var BeerData    = require('../../beers.json');
9
var BreweryData = require('../../breweries.json');
10
11
chai.use(chaiHttp);
12
13
describe('GraphQL Beer (queries)', function() {
14
15
  beforeEach(function(done){
16
    BreweryData.forEach(function (aBeer) {
17
      var newBrewery = new Brewery(aBeer);
18
      newBrewery.save();
19
    });
20
    BeerData.forEach(function (aBeer) {
21
      var newBeer = new Beer(aBeer);
22
      newBeer.save();
23
    });
24
    done();
25
  });
26
27
  afterEach(function(done){
28
    Beer.collection.drop();
29
    Brewery.collection.drop();
30
    done();
31
  });
32
33
  it('should display all the beers', function(done) {
34
    var graphqlQuery = `
35
    {
36
      beers {
37
        name
38
        alcohol
39
        description
40
      }
41
    }`;
42
43
    chai.request(server)
44
      .post('/graphql')
45
      .send({query: graphqlQuery})
46
      .end(function(err, res){
47
        res.should.have.status(200);
48
        res.should.be.a.json;
49
        res.body['data']['beers'].should.be.an('array');
50
        res.body['data']['beers'][0].should.have.property('name');
51
        res.body['data']['beers'][0].should.have.property('alcohol');
52
        done();
53
      });
54
  });
55
56
  it('should display only beers from Duvel Brewery', function(done) {
57
    var graphqlQuery = `
58
    {
59
      beers(brewery: "duvel") {
60
        name
61
        alcohol
62
        brewery {
63
          name
64
        }
65
      }
66
    }`;
67
68
    chai.request(server)
69
      .post('/graphql')
70
      .send({query: graphqlQuery})
71
      .end(function(err, res){
72
        res.should.have.status(200);
73
        res.should.be.a.json;
74
        for (var beer of res.body['data']['beers']) {
75
          beer.brewery.name.should.equal('Duvel Moortgat');
76
        }
77
        done();
78
      });
79
  });
80
81
  it('should display beers ordered by alcohol', function(done) {
82
    var graphqlQuery = `
83
    {
84
      beers(orderBy: ALCOHOL) {
85
        name
86
        alcohol
87
      }
88
    }`;
89
90
    chai.request(server)
91
      .post('/graphql')
92
      .send({query: graphqlQuery})
93
      .end(function(err, res){
94
        res.should.have.status(200);
95
        res.should.be.a.json;
96
        var previousAlcohol = 1;
97
        for (var beer of res.body['data']['beers']) {
98
          beer.alcohol.should.be.at.least(previousAlcohol);
99
          previousAlcohol = beer.alcohol;
100
        }
101
        done();
102
      });
103
  });
104
105
  it('should display only beers from Duvel Brewery and ordered by alcohol', function(done) {
106
    var graphqlQuery = `
107
    {
108
      beers(brewery: "Duvel", orderBy: ALCOHOL) {
109
        name
110
        brewery {
111
          name
112
        }
113
        alcohol
114
      }
115
    }`;
116
117
    chai.request(server)
118
      .post('/graphql')
119
      .send({query: graphqlQuery})
120
      .end(function(err, res){
121
        res.should.have.status(200);
122
        res.should.be.a.json;
123
        var previousAlcohol = 1;
124
        for (var beer of res.body['data']['beers']) {
125
          beer.brewery.name.should.equal('Duvel Moortgat');
126
          beer.alcohol.should.be.at.least(previousAlcohol);
127
          previousAlcohol = beer.alcohol;
128
        }
129
        done();
130
      });
131
  });
132
133
  it('should display a SINGLE beer', function(done) {
134
    var newBeer = new Beer({
135
      name: 'Custom Beer',
136
      brewery: 'Custom Brewery',
137
      alcohol: 9.5,
138
      description: 'miam miam'
139
    });
140
141
    newBeer.save(function(err, data) {
142
143
      var graphqlQuery = `
144
      {
145
        beer(id: "${data._id}") {
146
          name
147
        }
148
      }`;
149
150
      chai.request(server)
151
        .post('/graphql')
152
        .send({query: graphqlQuery})
153
        .end(function(err, res){
154
          res.should.have.status(200);
155
          res.should.be.a.json;
156
          res.body.should.be.an('object');
157
          res.body['data']['beer']['name'].should.equal('Custom Beer');
158
          done();
159
        });
160
    });
161
  });
162
});
163