forked from originlabs/graphql-postgres-subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres-pubsub.test.js
201 lines (170 loc) · 6.57 KB
/
postgres-pubsub.test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Adapted from https://github.com/apollographql/graphql-subscriptions/blob/master/src/test/tests.ts
const { isAsyncIterable } = require("iterall");
const { PostgresPubSub } = require("./postgres-pubsub");
describe("PostgresPubSub", () => {
test("PostgresPubSub can subscribe when instantiated without a client", async function(done) {
const ps = new PostgresPubSub();
await ps.connect();
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).resolves.toBe(true);
}).catch(done);
});
test("PostgresPubSub can subscribe and is called when events happen", async function(done) {
const ps = new PostgresPubSub();
await ps.connect();
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).resolves.toBe(true);
}).catch(done);
});
test("PostgresPubSub can subscribe when instantiated with connection options but without a client", async function(done) {
const ps = new PostgresPubSub({
connectionString: process.env.DATABASE_URL
});
await ps.connect();
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).resolves.toBe(true);
}).catch(done);
});
test("PostgresPubSub can unsubscribe", async function(done) {
const ps = new PostgresPubSub();
await ps.connect()
ps.subscribe("a", payload => {
expect(false).toBe(true); // Should not reach this point
}).then(subId => {
ps.unsubscribe(subId);
const succeed = ps.publish("a", "test");
expect(succeed).resolves.toBe(true); // True because publish success is not
// indicated by trigger having subscriptions
done(); // works because pubsub is synchronous
}).catch(done);
});
test("Should emit error when payload exceeds Postgres 8000 character limit", async () => {
const ps = new PostgresPubSub();
await ps.connect();
await ps.subscribe("a", () => {});
await expect(
ps.publish("a", "a".repeat(9000))
).rejects.toThrow('payload string too long');
});
test("AsyncIterator should expose valid asyncIterator for a specific event", () => {
const eventName = "test";
const ps = new PostgresPubSub({ topics: [eventName]});
const iterator = ps.asyncIterator(eventName);
expect(iterator).not.toBeUndefined();
expect(isAsyncIterable(iterator)).toBe(true);
});
test("AsyncIterator should trigger event on asyncIterator when published", async (done) => {
const eventName = "test";
const ps = new PostgresPubSub({ topics: [eventName]});
await ps.connect()
const iterator = ps.asyncIterator(eventName);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).not.toBeUndefined();
expect(result.done).not.toBeUndefined();
done();
}).catch(done);
ps.publish(eventName, { test: true });
});
test("AsyncIterator should not trigger event on asyncIterator when publishing other event", async (done) => {
const eventName = "test2";
const ps = new PostgresPubSub({ topics: [eventName]});
await ps.connect();
const iterator = ps.asyncIterator("test");
const spy = jest.fn();
iterator.next().then(spy);
ps.publish(eventName, { test: true });
expect(spy).not.toHaveBeenCalled();
done();
});
test("AsyncIterator should register to multiple events", async (done) => {
const eventName = "test2";
const ps = new PostgresPubSub({ topics: ['test', 'test2']});
await ps.connect();
const iterator = ps.asyncIterator(["test", "test2"]);
const spy = jest.fn();
iterator.next().then(() => {
spy();
expect(spy).toHaveBeenCalled();
done();
}).catch(done);
ps.publish(eventName, { test: true });
});
test("AsyncIterator transforms messages using commonMessageHandler", async (done) => {
const eventName = "test";
const commonMessageHandler = message => ({ transformed: message });
const ps = new PostgresPubSub({ commonMessageHandler, topics: [eventName] });
await ps.connect();
const iterator = ps.asyncIterator(eventName);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).toEqual({ transformed: { test: true } });
expect(result.done).toBe(false);
done();
}).catch(done);
ps.publish(eventName, { test: true });
});
test("PostgresPubSub transforms messages using commonMessageHandler", async function(done) {
const commonMessageHandler = message => ({ transformed: message });
const ps = new PostgresPubSub({ commonMessageHandler });
await ps.connect();
ps.subscribe("transform", payload => {
expect(payload).toEqual({ transformed: { test: true } });
done();
}).then(() => {
const succeed = ps.publish("transform", { test: true });
expect(succeed).resolves.toBe(true);
}).catch(done);
});
// This test does not clean up after it ends. It breaks the test that follows after it.
// It won't break any tests if it's the last. https://imgflip.com/i/2lmlgm
// TODO: Fix it properly
test("AsyncIterator should not trigger event on asyncIterator already returned", async done => {
const eventName = "test";
const ps = new PostgresPubSub({ topics: [eventName]});
await ps.connect();
const iterator = ps.asyncIterator(eventName);
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).not.toBeUndefined();
expect(result.done).toBe(false);
}).catch(done);
ps.publish(eventName, { test: true });
await delay(0);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).toBeUndefined();
expect(result.done).toBe(true);
done();
}).catch(done);
await delay(0);
iterator.return();
ps.publish(eventName, { test: true });
});
test("AsyncIteratorPromised should work without having topics on constructor", async (done) => {
const eventName = "test2";
const ps = new PostgresPubSub();
await ps.connect();
const iterator = await ps.asyncIteratorPromised(["test", "test2"]);
const spy = jest.fn();
iterator.next().then(() => {
spy();
expect(spy).toHaveBeenCalled();
done();
}).catch(done);
ps.publish(eventName, { test: true });
});
});