-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.spec.ts
172 lines (153 loc) · 6.66 KB
/
filter.spec.ts
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
import { expect } from 'chai'
import sinon, { SinonSpy } from 'sinon'
import { createLiveQueryFilter, Unsubscribe } from '../src/filter'
import debug, { Debugger } from 'debug'
context('Filter spec', () => {
let log: Debugger
let publish: ReturnType<typeof createLiveQueryFilter>['publish']
let subscribe: ReturnType<typeof createLiveQueryFilter>['subscribe']
before(() => {
log = debug('filter.spec')
let filter = createLiveQueryFilter()
publish = filter.publish
subscribe = filter.subscribe
})
it('should not throw error when publish to table without pre-defined subscriptions', () => {
publish([{ table: 'new_table' }])
})
it('should not throw error when publish to field without pre-defined subscriptions', () => {
publish([{ table: 'new_table', field: '*' }])
publish([{ table: 'new_table', fields: ['new_field2', 'new_field3'] }])
})
context('unsubscribe', () => {
let callback: SinonSpy
let unsubscribe: Unsubscribe
before(() => {
callback = sinon.spy()
})
it('should trigger callback after subscribe', () => {
unsubscribe = subscribe([{ table: 'user' }], callback)
expect(callback.callCount).to.equals(0)
publish([{ table: 'user' }])
expect(callback.callCount).to.equals(1)
})
it('should not trigger callback after unsubscribe', () => {
unsubscribe()
publish([{ table: 'user' }])
expect(callback.callCount).to.equals(1)
})
})
it('should trigger update to wildcard field', () => {
let callback = sinon.spy()
subscribe([{ table: 'user', field: '*' }], callback)
expect(callback.callCount).to.equals(0)
publish([{ table: 'user', field: 'field1' }])
expect(callback.callCount).to.equals(1)
})
it('should only trigger related subscriptions', () => {
let count_user = sinon.spy()
subscribe([{ table: 'user' }], count_user)
let look_up_user_by_username = sinon.spy()
subscribe(
[{ table: 'user', fields: ['username'] }],
look_up_user_by_username,
)
let select_user_1_username = sinon.spy()
subscribe(
[{ table: 'user', id: 1, fields: ['username'] }],
select_user_1_username,
)
let select_user_2_username = sinon.spy()
subscribe(
[{ table: 'user', id: 2, field: 'username' }],
select_user_2_username,
)
let select_user_2_rank = sinon.spy()
subscribe([{ table: 'user', id: 2, fields: ['rank'] }], select_user_2_rank)
let select_multiple_user = sinon.spy()
subscribe([{ table: 'user', ids: [2, 3] }], select_multiple_user)
let select_post_by_timestamp = sinon.spy()
subscribe([{ table: 'post' }], select_post_by_timestamp)
let select_post_1 = sinon.spy()
subscribe([{ table: 'post', id: 1 }], select_post_1)
expect(count_user.callCount).to.equals(0)
expect(look_up_user_by_username.callCount).to.equals(0)
expect(select_user_1_username.callCount).to.equals(0)
expect(select_user_2_username.callCount).to.equals(0)
expect(select_user_2_rank.callCount).to.equals(0)
expect(select_multiple_user.callCount).to.equals(0)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# insert user')
publish([{ table: 'user' }])
expect(count_user.callCount).to.equals(1)
expect(look_up_user_by_username.callCount).to.equals(1)
expect(select_user_1_username.callCount).to.equals(1)
expect(select_user_2_username.callCount).to.equals(1)
expect(select_user_2_rank.callCount).to.equals(1)
expect(select_multiple_user.callCount).to.equals(1)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# update user[1].username')
publish([{ table: 'user', id: 1, fields: ['username'] }])
expect(count_user.callCount).to.equals(2)
expect(look_up_user_by_username.callCount).to.equals(2)
expect(select_user_1_username.callCount).to.equals(2)
expect(select_user_2_username.callCount).to.equals(1)
expect(select_user_2_rank.callCount).to.equals(1)
expect(select_multiple_user.callCount).to.equals(1)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# update user[2].username')
publish([{ table: 'user', id: 2, fields: ['username'] }])
expect(count_user.callCount).to.equals(3)
expect(look_up_user_by_username.callCount).to.equals(3)
expect(select_user_1_username.callCount).to.equals(2)
expect(select_user_2_username.callCount).to.equals(2)
expect(select_user_2_rank.callCount).to.equals(1)
expect(select_multiple_user.callCount).to.equals(2)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# update user[2].rank')
publish([{ table: 'user', id: 2, field: 'rank' }])
expect(count_user.callCount).to.equals(4)
expect(look_up_user_by_username.callCount).to.equals(3)
expect(select_user_1_username.callCount).to.equals(2)
expect(select_user_2_username.callCount).to.equals(2)
expect(select_user_2_rank.callCount).to.equals(2)
expect(select_multiple_user.callCount).to.equals(3)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# update user[3,4]')
publish([{ table: 'user', ids: [3, 4] }])
expect(count_user.callCount).to.equals(5)
expect(look_up_user_by_username.callCount).to.equals(4)
expect(select_user_1_username.callCount).to.equals(2)
expect(select_user_2_username.callCount).to.equals(2)
expect(select_user_2_rank.callCount).to.equals(2)
expect(select_multiple_user.callCount).to.equals(4)
expect(select_post_by_timestamp.callCount).to.equals(0)
expect(select_post_1.callCount).to.equals(0)
log('# update post 1')
publish([{ table: 'post', id: 1 }])
expect(count_user.callCount).to.equals(5)
expect(look_up_user_by_username.callCount).to.equals(4)
expect(select_user_1_username.callCount).to.equals(2)
expect(select_user_2_username.callCount).to.equals(2)
expect(select_user_2_rank.callCount).to.equals(2)
expect(select_multiple_user.callCount).to.equals(4)
expect(select_post_by_timestamp.callCount).to.equals(1)
expect(select_post_1.callCount).to.equals(1)
})
it('should isolate each filter instance', () => {
let filter1 = createLiveQueryFilter()
let filter2 = createLiveQueryFilter()
let callback1 = sinon.spy()
let callback2 = sinon.spy()
filter1.subscribe([{ table: 'user' }], callback1)
filter2.subscribe([{ table: 'user' }], callback2)
filter1.publish([{ table: 'user' }])
expect(callback1.callCount).to.equals(1)
expect(callback2.callCount).to.equals(0)
})
})