-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
164 lines (148 loc) · 5.99 KB
/
test.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
import test from 'ava'
import { Emitter } from './'
import { filter, map } from 'rxjs/operators'
type Messages = {
SHOULD_OPEN_MODAL: { id: number, value: boolean }
SHOULD_CLOSE_MODAL: { id: number, value: boolean }
}
test('it should trigger subscribers', t => {
t.plan(2)
const emitter = new Emitter<Messages>()
emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => {
t.is(_.id, 123)
t.is(_.value, true)
})
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
})
test('it should fail silently if emitting before subscribers exist', t => {
t.plan(0)
const emitter = new Emitter<Messages>()
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.fail('Should not get called'))
})
test('it should support Rx methods', t => {
t.plan(1)
const emitter = new Emitter<Messages>()
emitter.on('SHOULD_OPEN_MODAL')
.pipe(
filter(_ => _.id > 100),
map(_ => _.id * 2)
)
.subscribe(_ =>
t.is(_, 202)
)
emitter.emit('SHOULD_OPEN_MODAL', { id: 99, value: true })
emitter.emit('SHOULD_OPEN_MODAL', { id: 101, value: true })
})
test('it should support multiple listeners', t => {
t.plan(2)
const emitter = new Emitter<Messages>()
emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.is(_.value, true))
emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.is(_.value, true))
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
})
test('it should dispose listeners independently', t => {
t.plan(1)
const emitter = new Emitter<Messages>()
const disposable1 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.fail())
emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.pass())
disposable1.unsubscribe()
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
})
test('it should automatically clean up unused listeners', t => {
t.plan(1)
const emitter = new Emitter<Messages>()
const disposable1 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.fail())
const disposable2 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.fail())
disposable1.unsubscribe()
disposable2.unsubscribe()
t.is(emitter['emitterState'].observables.has('SHOULD_OPEN_MODAL'), false)
})
test('it handle sequential additions and removals gracefully', t => {
t.plan(5)
const emitter = new Emitter<Messages>()
const disposable1 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.fail())
const disposable2 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.deepEqual(_, { id: 123, value: true }))
disposable1.unsubscribe()
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
disposable2.unsubscribe()
t.is(emitter['emitterState'].observables.has('SHOULD_OPEN_MODAL'), false)
const disposable3 = emitter.on('SHOULD_OPEN_MODAL').subscribe(_ => t.deepEqual(_, { id: 456, value: false }))
emitter.emit('SHOULD_OPEN_MODAL', { id: 456, value: false })
t.is(emitter['emitterState'].observables.has('SHOULD_OPEN_MODAL'), true)
disposable3.unsubscribe()
t.is(emitter['emitterState'].observables.has('SHOULD_OPEN_MODAL'), false)
})
test('#all should fire', t => {
t.plan(4)
const emitter = new Emitter<Messages>()
emitter.all().subscribe(_ => t.is(_.value, true))
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
emitter.emit('SHOULD_CLOSE_MODAL', { id: 123, value: true })
emitter.emit('SHOULD_CLOSE_MODAL', { id: 123, value: true })
})
test('#all should fire after specific listeners', t => {
t.plan(1)
const emitter = new Emitter<Messages>()
let isFirstCalled = false
emitter.on('SHOULD_OPEN_MODAL').subscribe(() => isFirstCalled = true)
emitter.all().subscribe(_ => t.is(isFirstCalled, true))
emitter.emit('SHOULD_OPEN_MODAL', { id: 123, value: true })
})
// cycle detection tests
test('it should show a console error when detecting cyclical dependencies in dev mode (1)', t => {
t.plan(1)
console.error = (e: string) =>
t.regex(e, /Cyclical dependency detected/)
const emitter = new Emitter<Messages>({isDevMode: true})
emitter.on('SHOULD_OPEN_MODAL').subscribe(() =>
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
)
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
})
test('it should show a console error when detecting cyclical dependencies in dev mode (2)', t => {
t.plan(1)
console.error = (e: string) =>
t.regex(e, /Cyclical dependency detected/)
const emitter = new Emitter<Messages>({isDevMode: true})
emitter.on('SHOULD_OPEN_MODAL').subscribe(() =>
emitter.emit('SHOULD_CLOSE_MODAL', { id: 1, value: true })
)
emitter.on('SHOULD_CLOSE_MODAL').subscribe(() =>
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
)
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
})
test('it should show a console error when detecting cyclical dependencies in dev mode (3)', t => {
t.plan(1)
console.error = (e: string) =>
t.regex(e, /Cyclical dependency detected/)
const emitter = new Emitter<Messages>({isDevMode: true})
emitter.all().subscribe(() =>
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
)
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
})
test('it should show a console error when detecting cyclical dependencies in dev mode (4)', t => {
t.plan(2)
console.error = (e: string) =>
t.regex(e, /Cyclical dependency detected/)
const emitter = new Emitter<Messages>({isDevMode: true})
emitter.all().subscribe(() =>
emitter.emit('SHOULD_CLOSE_MODAL', { id: 1, value: true })
)
emitter.on('SHOULD_CLOSE_MODAL').subscribe(() =>
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
)
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
})
test('it not should show a console error when not detecting cyclical dependencies in dev mode', t => {
console.error = t.fail
const emitter = new Emitter<Messages>({isDevMode: true})
emitter.on('SHOULD_CLOSE_MODAL').subscribe(() =>
emitter.emit('SHOULD_OPEN_MODAL', { id: 1, value: true })
)
emitter.emit('SHOULD_CLOSE_MODAL', { id: 1, value: true })
t.pass()
})