Issues (37)

tests/mutations/beer.mutations.test.js (11 issues)

1 View Code Duplication
var chai     = require('chai');
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
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;
0 ignored issues
show
The result of the property access to res.should.be.json is not used.
Loading history...
55
        res.body.data.addBeer.should.be.true;
0 ignored issues
show
The result of the property access to res.body.data.addBeer.should.be.true is not used.
Loading history...
56
      });
57
58
    // find this new beer
59
    var graphqlQuery = `
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable graphqlQuery already seems to be declared on line 43. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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;
0 ignored issues
show
The result of the property access to res.should.be.json is not used.
Loading history...
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 = `
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable graphqlQuery already seems to be declared on line 92. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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;
0 ignored issues
show
The result of the property access to res.should.be.json is not used.
Loading history...
124
          res.body.should.be.an('object');
125
          res.body.data.beer.should.be.null;
0 ignored issues
show
The result of the property access to res.body.data.beer.should.be.null is not used.
Loading history...
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;
0 ignored issues
show
The result of the property access to res.should.be.json is not used.
Loading history...
145
        res.body.data.removeAllBeers.should.be.true;
0 ignored issues
show
The result of the property access to res.body.data.removeAllBeers.should.be.true is not used.
Loading history...
146
      });
147
148
    // retrieve all beers
149
    var graphqlQuery = `
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable graphqlQuery already seems to be declared on line 133. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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