-
Notifications
You must be signed in to change notification settings - Fork 4
/
graphql-subscription-tests.js
152 lines (136 loc) · 4.63 KB
/
graphql-subscription-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
151
152
'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 faker = require('faker');
const {createFakeUser} = require('./devHelper');
const {getCollection, updateCollection, getItemFromCollection} = require('../data/index');
const ws = require('ws');
const {WebSocketLink} = require("apollo-link-ws");
const {execute} = require("apollo-link");
const {SubscriptionClient} = require('subscriptions-transport-ws');
const gql = require('graphql-tag');
const serverConfig = {serverUrl: 'http://localhost:4000/', subscriptionUrl: 'ws://localhost:4000/graphql'};
const config = require('../config');
let link;
before(() => {
const client = new SubscriptionClient(
serverConfig.subscriptionUrl,
{
reconnect: true,
connectionParams: () => ({
authorization: config.ACCESS_TOKEN,
}),
},
ws,
);
link = new WebSocketLink(client);
});
after(() => {
link.subscriptionClient.close();
server.stop();
});
describe('GraphQL Subscription Tests', () => {
/*
This test sends a path via the mutiation, ping and
asserts the the path submitted in the mutation shows
up in event to which the test is subscribed
*/
it('Can ping and subscribe', (done) => {
let body = faker.lorem.words(3);
const operation = {
query: gql`
subscription onEventAdded{
onEventAdded{
id
name
body
createdAt
storedAt
}
}`
};
execute(link, operation).subscribe({
next: data => {
console.log(`received data: ${JSON.stringify(data, null, 2)}`);
expect(data.data.onEventAdded.body).to.equal(body);
done();
},
error: error => {
console.log(`received error ${JSON.stringify(error)}`)
},
complete: () => console.log(`complete`),
});
const query = `mutation{
ping(messageBody: "${body}"){
createdAt
body
name
id
}
}`;
const graphQLClient = new GraphQLClient(serverConfig.serverUrl, {
headers: {
authorization: `${config.ACCESS_TOKEN}`,
},
});
const data = graphQLClient.request(query)
.then(data => {
expect(data).to.be.an('object');
expect(data.ping.body).to.equal(body);
});
});
it('Can AddPerson and subscribe', (done) => {
const {firstName, lastName, dob} = createFakeUser();
const operation = {
query: gql`
subscription onPersonAdded{
onPersonAdded{
id
name
body
createdAt
storedAt
}
}`
};
execute(link, operation).subscribe({
next: data => {
console.log(`received data: ${JSON.stringify(data, null, 2)}`);
const rslt = JSON.parse(data.data.onPersonAdded.body);
expect(rslt.firstName).to.equal(firstName);
expect(rslt.lastName).to.equal(lastName);
expect(rslt.dob).to.equal(dob);
done();
},
error: error => {
console.log(`received error ${JSON.stringify(error)}`)
},
complete: () => console.log(`complete`),
});
const query = `mutation{
addPerson(person: {firstName: "${firstName}",
lastName: "${lastName}",
dob: "${dob}"}){
id
firstName
lastName
dob
}
}`;
const graphQLClient = new GraphQLClient(serverConfig.serverUrl, {
headers: {
authorization: `${config.ACCESS_TOKEN}`,
},
});
const data = graphQLClient.request(query)
.then(data => {
expect(data).to.be.an('object');
//expect(data.ping.body).to.equal(body);
});
});
});