/
testRealtime.js
141 lines (124 loc) · 3.97 KB
/
testRealtime.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
import { assert } from 'chai'
import { createClient } from '../../src'
// on()
// subscribe()
// unsubscribe()
// removeSubscription()
// getSubscriptions()
describe('test subscribing to an insert', function() {
const supabase = createClient('http://localhost:8000', 'examplekey')
afterEach(function() {
const subscriptions = supabase.getSubscriptions()
for (const sub of subscriptions) {
supabase.removeSubscription(sub)
}
})
// test adding and removing a subscription
it('can add and remove a subscription', function() {
const subscription = supabase
.from('*')
.on('*', null)
.subscribe()
assert(subscription.uuid === supabase.getSubscriptions()[0].uuid, "uuid's not equal")
supabase.removeSubscription(subscription)
assert(supabase.getSubscriptions().length === 0, 'subscriptions not empty')
})
// test double wildcard, all events, all tables
it('from(*).on(*).subscribe()', function(done) {
const callbackAction = function(record) {
assert(record.new.message === 'hello, mocha', 'inserted message is incorrect')
done()
}
const subscription = supabase
.from('*')
.on('*', callbackAction)
.subscribe()
subscription.channel.socket.conn.addEventListener('open', function(event) {
supabase
.from('messages')
.insert([{ message: 'hello, mocha', user_id: 1, channel_id: 1 }])
.then()
.catch(console.error)
})
}).timeout(5000)
// test events on specific table
it('from("messages").on("*").subscribe()', function(done) {
const callbackAction = function(record) {
assert(record.new.message === 'hello, mocha fans', 'inserted message is incorrect')
done()
}
const subscription = supabase
.from('messages')
.on('*', callbackAction)
.subscribe()
subscription.channel.socket.conn.addEventListener('open', function(event) {
supabase
.from('messages')
.insert([{ message: 'hello, mocha fans', user_id: 1, channel_id: 1 }])
.then()
.catch(console.error)
})
}).timeout(5000)
// test delete message
it('from("messages").on("DELETE").subscribe()', function(done) {
const callbackAction = function(record) {
assert(record.old.id === 1, 'deleted message does not have correct id')
done()
}
const subscription = supabase
.from('messages')
.on('DELETE', callbackAction)
.subscribe()
subscription.channel.socket.conn.addEventListener('open', function(event) {
supabase
.from('messages')
.match({ id: 1 })
.delete()
.then()
.catch(console.error)
})
}).timeout(5000)
// test update message
it('from(messages).on(UPDATE).subscribe()', function(done) {
const callbackAction = function(record) {
assert(
record.new.message === 'updated message yo',
'updated message does not have correct text'
)
done()
}
const subscription = supabase
.from('messages')
.on('UPDATE', callbackAction)
.subscribe()
subscription.channel.socket.conn.addEventListener('open', function(event) {
supabase
.from('messages')
.match({ id: 1 })
.update({ message: 'updated message yo' })
.then()
.catch(console.error)
})
}).timeout(5000)
// test on INSERT
it('from("*").on("INSERT").subscribe()', function(done) {
const callbackAction = function(record) {
assert(record.new.message === 'hello, mocha fans, Y2K', 'inserted message is incorrect')
done()
}
const subscription = supabase
.from('*')
.on('INSERT', callbackAction)
.subscribe()
subscription.channel.socket.conn.addEventListener('open', function(event) {
supabase
.from('messages')
.insert([{ message: 'hello, mocha fans, Y2K', user_id: 1, channel_id: 1 }])
.then()
.catch(console.error)
})
}).timeout(5000)
})
after(function() {
setTimeout(() => process.exit(0), 5000)
})