-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraphql-tests.js
150 lines (141 loc) · 4.73 KB
/
graphql-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
const {GraphQLClient} = require('graphql-request');
const _ = require('lodash');
const expect = require('chai').expect;
const describe = require('mocha').describe;
const before = require('mocha').before;
const it = require('mocha').it;
const {server} = require('../server');
const url = 'http://localhost:4000/';
const uuidv4 = require('uuid/v4');
const {createFakeUser} = require('./devHelper');
const config = require('../config');
const {getCollection, updateCollection, getItemFromCollection} = require('../data/index');
const serverConfig = {serverUrl: 'http://localhost:4000/', subscriptionUrl: 'ws://localhost:4000/graphql'};
let graphQLClient;
before(() => {
graphQLClient = new GraphQLClient(serverConfig.serverUrl, {
headers: {
authorization: `${config.ACCESS_TOKEN}`,
},
});
});
after(() => {
server.stop();
});
describe('GraphQL Basic Tests', () => {
it('Can page through persons', (done) => {
let colOne = [];
let colTwo =[];
let query = `{
persons{
collection{
firstName
lastName
knowsConnection{
edges{
node{
firstName
lastName
}
}
}
}
pageInfo{
hasNextPage
endCursor
}
}
}`;
graphQLClient.request(query)
.then(data => {
console.log(data);
expect(data.persons.collection.length).to.equal(10);
colOne = data.persons.collection;
let query = `{
persons(paginationSpec: {after: "${data.persons.pageInfo.endCursor}"}){
collection{
firstName
lastName
knowsConnection{
edges{
node{
firstName
lastName
}
}
}
}
pageInfo{
hasNextPage
endCursor
}
}
}`;
graphQLClient.request(query)
.then(data =>{
expect(data.persons.collection.length).to.equal(10);
colTwo = data.persons.collection;
const rslt = _.intersection(colOne, colTwo);
expect(rslt.length).to.equal(0);
done();
});
})
});
it('Can get movies from API length equals Movies from data length', (done) => {
const movies = getCollection('movies');
const query = `query { movies{ title releaseDate } }`;
graphQLClient.request(query)
.then(data => {
console.log(data);
expect(movies.length).to.equal(data.movies.length);
done();
})
});
it('Can paginate likes', (done) => {
const query = `{
persons{
collection{
firstName
lastName
}
}
}`;
graphQLClient.request(query)
.then(data => {
expect(data.persons.collection.length).to.equal(10); // 10 is the default page size
done();
})
.catch(e =>{
console.log(e);
done(e);
})
});
it('Can add person via GraphQL', (done) => {
const {firstName, lastName, dob} = createFakeUser();
const query = `mutation{
addPerson(person: {firstName: "${firstName}", lastName: "${lastName}", dob: "${dob}"}){
id
firstName
lastName
dob
}
}`;
graphQLClient.request(query)
.then(data => {
console.log(data);
const obj = data.addPerson;
expect(obj.firstName).to.equal(firstName);
expect(obj.lastName).to.equal(lastName);
expect(obj.dob).to.equal(dob);
const persons = getCollection('persons');
const p = _.find(persons, {id:obj.id });
console.log(process.env.PERSONS)
expect(obj.id).to.equal(p.id);
done();
})
.catch(e =>{
done(e)
})
});
});