-
Notifications
You must be signed in to change notification settings - Fork 0
/
can-stream-bacon.test.js
184 lines (122 loc) · 3.33 KB
/
can-stream-bacon.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
174
175
176
177
178
179
180
181
182
183
184
import test from 'ava';
import compute from 'can-compute';
import canStreamX from '.';
import DefineList from 'can-define/list/list';
import Bacon from 'baconjs';
const canStream = canStreamX({
streamConstructor: Bacon.fromBinder,
on: 'onValue',
off: false
});
test('Compute changes can be streamed', (t) => {
const c = compute(0);
const stream = canStream.toStream(c);
let computeVal;
stream.onValue((newVal) => {
computeVal = newVal;
});
t.is(computeVal, 0);
c(1);
t.is(computeVal, 1);
c(2);
t.is(computeVal, 2);
c(3);
t.is(computeVal, 3);
t.pass();
});
test('Compute streams do not bind to the compute unless activated', (t) => {
const c = compute(0);
const stream = canStream.toStream(c);
t.is(c.computeInstance.__bindEvents, undefined);
stream.onValue(() => {});
t.is(c.computeInstance.__bindEvents._lifecycleBindings, 1);
});
test('Compute stream values can be piped into a compute', (t) => {
let expected = 0;
const c1 = compute(0);
const c2 = compute(0);
const resultCompute = canStream.toStream(c1).merge(canStream.toStream(c2));
resultCompute.onValue((val) => {
t.is(val, expected);
});
expected = 1;
c1(1);
expected = 2;
c2(2);
expected = 3;
c1(3);
});
test('Computed streams fire change events', (t) => {
let expected = 0;
const c1 = compute(expected);
const c2 = compute(expected);
const resultCompute = canStream.toStream(c1).merge(canStream.toStream(c2));
resultCompute.onValue((val) => {
t.is(expected, val);
});
expected = 1;
c1(expected);
expected = 2;
c2(expected);
expected = 3;
c1(expected);
});
test('Create a stream from a compute with shorthand method: toStream', (t) => {
let expected = 0;
const c1 = compute(0);
const resultCompute = canStream.toStream(c1);
resultCompute.onValue((val) => {
t.is(val, expected);
});
expected = 1;
c1(1);
});
test('toCompute(streamMaker) can-define-stream#17', (t) => {
const c = compute('a');
const letterStream = canStream.toStream(c);
const streamedCompute = canStream.toCompute(setStream => setStream.merge(letterStream));
streamedCompute.on('change', (ev, newVal) => {
});
t.is(streamedCompute(), 'a');
c(1);
t.is(streamedCompute(), 1);
c('b');
t.is(streamedCompute(), 'b');
});
test('setting test', (t) => {
const c = canStream.toCompute(setStream => setStream);
c(5);
// listen to the compute for it to have a value
c.on('change', () => {});
// immediate value
t.is(c(), 5);
});
test('Stream on DefineList', (t) => {
let expectedLength;
const people = new DefineList([
{ first: 'Justin', last: 'Meyer' },
{ first: 'Paula', last: 'Strozak' },
]);
const stream = canStream.toStream(people, '.length');
expectedLength = 2;
stream.onValue((newLength) => {
t.is(newLength, expectedLength);
});
expectedLength = 3;
people.push({
first: 'Obaid',
last: 'Ahmed',
});
expectedLength = 2;
people.pop();
});
test('Test if streams are memory safe', function(t) {
const c = canStream.toCompute(setStream => setStream);
const handler = (ev, newVal, oldVal) => {
console.log('newVal', newVal); //->output: obaid
};
c.on('change', handler);
t.is(c.computeInstance.__bindEvents._lifecycleBindings, 1);
c.off('change', handler);
t.is(c.computeInstance.__bindEvents._lifecycleBindings, 0);
});