-
Notifications
You must be signed in to change notification settings - Fork 2
/
livedata.test.js
173 lines (131 loc) · 3.95 KB
/
livedata.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
import test from 'ava'
import sinon from 'sinon'
import {LiveData as L, MediatorLiveData as M} from './livedata.js'
test('given a LiveData with initial value then get will return it', t => {
// Given
const ld = new L(true)
// Then
t.true(ld.get())
})
test('given a LiveData when not subscribed to then it is not active', t => {
// Given
const ld = new L()
// Then
t.false(ld.isActive())
})
test('given a LiveData when setting another value get will return it', t => {
// Given
const ld = new L(true)
// When
ld.set(false)
// Then
t.false(ld.get())
})
test('given a LiveData when transitioning to another value get will return it', t => {
// Given
const ld = new L(true)
// When
ld.transition(state => !state)
// Then
t.false(ld.get())
})
test('given a LiveData when no initialValue supplied then the first subscribe will not trigger the observer', t => {
const spy = sinon.spy()
// Given
const ld = new L()
// When
ld.subscribe(value => spy(value))
// Then
t.true(spy.notCalled)
})
test('given a LiveData when subscribed to the observer is immediatly called with the initial value', t => {
const spy = sinon.spy()
// Given
const ld = new L(true)
// When
ld.subscribe((newValue, oldValue) => {
spy(newValue, oldValue)
})
// Then
t.true(spy.calledOnceWith(true))
})
test('given a LiveData subscription when changing the value the observer ist called with the new and old value', t => {
const spy = sinon.spy()
// Given
const ld = new L(true)
ld.subscribe((newValue, oldValue) => {
spy(newValue, oldValue)
})
spy.resetHistory() // Ignore the invokation when subscription happend
// When
ld.set(false)
// Then
t.true(spy.calledOnceWith(false, true))
})
test('given an onActive callback when multiple subscriber onActive will only be invoked once', t => {
const onActive = sinon.spy()
const sub = sinon.spy()
// Given
const ld = new L(true, onActive)
// When
ld.subscribe(sub)
ld.subscribe(sub)
ld.subscribe(sub)
// Then
t.is(onActive.callCount, 1)
t.true(sub.calledThrice)
t.true(onActive.calledBefore(sub))
})
test('given a MediatorLiveData when adding a source then the subscription will be called', t => {
const spy = sinon.spy()
// Given
const liveData = new L(true)
const mediatorLiveData = new M()
// When
mediatorLiveData.addSource(liveData, value => mediatorLiveData.set(value))
mediatorLiveData.subscribe(value => spy(value))
liveData.set(false)
// Then
t.true(spy.calledWith(true))
})
test('given a MediatorLiveData when removing a source then unsubscribe on the source should happen', t => {
const spy = sinon.spy()
const onActive = sinon.spy()
const onInactive = sinon.spy()
// Given
const liveData = new L(true, onActive, onInactive)
const mediatorLiveData = new M()
const removeSource = mediatorLiveData.addSource(liveData, value => mediatorLiveData.set(value))
mediatorLiveData.subscribe(value => spy(value))
// When
removeSource()
// Then
t.true(onActive.calledOnce)
t.true(onInactive.calledOnce)
})
test('given a MediatorLiveData when unsubscribing from it all sources should be unsubscribed', t => {
const spy = sinon.spy()
const onActive = sinon.spy()
const onInactive = sinon.spy()
// Given
const liveData = new L(true, onActive, onInactive)
const mediatorLiveData = new M()
mediatorLiveData.addSource(liveData, value => mediatorLiveData.set(value))
const unsub = mediatorLiveData.subscribe(value => spy(value))
// When
unsub()
// Then
t.true(onActive.calledOnce)
t.true(onInactive.calledOnce)
})
test('given a MediatorLiveData which is active when a source is added it should be immediatly be subscribed to', t => {
const spy = sinon.spy()
// Given
const liveData = new L(true)
const mediatorLiveData = new M()
mediatorLiveData.subscribe(value => spy(value))
// Then
mediatorLiveData.addSource(liveData, value => mediatorLiveData.set(value))
// Then
t.is(spy.callCount, 1)
})