-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilters.test.js
91 lines (69 loc) · 2.31 KB
/
filters.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
import test from 'ava'
import sinon from 'sinon'
import {LiveData as L} from './livedata.js'
import {filter, distinct} from './filters.js'
test('given a LiveData when filtered LiveData is derived and source value does pass predicate then filtered LiveData should be initialized with it', t => {
// Given
const ld = new L(0)
// When
const filtered = filter(ld, value => value % 2 === 0)
// Then
t.is(filtered.get(), 0)
})
test('given a LiveData when filtered LiveData is derived and source value does not pass predicate then filtered LiveData should not be initialized with it', t => {
// Given
const ld = new L(1)
// When
const filtered = filter(ld, value => value % 2 === 0)
// Then
t.is(filtered.get(), undefined)
})
test('given a filtered LiveData when filtered value is set on source then filtered will not receive the value', t => {
const spy = sinon.spy()
// Given
const ld = new L(0)
const filtered = filter(ld, value => value % 2 === 0)
filtered.subscribe(value => spy(value))
// When
ld.set(1) // Should not be propagated to filtered LiveData
// Then
t.true(spy.neverCalledWith(1))
})
test('given a filtered LiveData when allowed value is set on source then filtered will receive the value', t => {
const spy = sinon.spy()
// Given
const ld = new L(0)
const filtered = filter(ld, value => value % 2 === 0)
filtered.subscribe(value => spy(value))
// When
ld.set(2) // Should be propagated to filtered LiveData
// Then
t.true(spy.calledWith(2))
})
test('given a distinct LiveData when value is set multiple times on source then sibscriber should only be called once', t => {
const spy = sinon.spy()
// Given
const ld = new L(0)
const distincted = distinct(ld)
distincted.subscribe(value => spy(value))
// When
ld.set(0)
ld.set(0)
ld.set(1)
ld.set(1)
// Then
t.true(spy.calledWith(0))
t.true(spy.calledWith(1))
t.true(spy.calledTwice)
})
test('given a distinct LiveData with custom comparator when value is then custom comparator should be evaluated', t => {
const spy = sinon.spy()
// Given
const ld = new L('Hello World')
const distincted = distinct(ld, (a, b) => a.toLowerCase() === b.toLowerCase())
distincted.subscribe(value => spy(value))
// When
ld.set('hello world')
// Then
t.true(spy.calledOnceWith('Hello World'))
})