forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (145 loc) · 3.58 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as assert from 'assert';
import { readable, writable, derived, get } from '../../store.js';
describe('store', () => {
describe('writable', () => {
it('creates a writable store', () => {
const count = writable(0);
const values = [];
const unsubscribe = count.subscribe(value => {
values.push(value);
});
count.set(1);
count.update(n => n + 1);
unsubscribe();
count.set(3);
count.update(n => n + 1);
assert.deepEqual(values, [0, 1, 2]);
});
it('calls provided subscribe handler', () => {
let called = 0;
const store = writable(0, () => {
called += 1;
return () => called -= 1;
});
const unsubscribe1 = store.subscribe(() => {});
assert.equal(called, 1);
const unsubscribe2 = store.subscribe(() => {});
assert.equal(called, 1);
unsubscribe1();
assert.equal(called, 1);
unsubscribe2();
assert.equal(called, 0);
});
it('does not assume immutable data', () => {
const obj = {};
let called = 0;
const store = writable(obj);
store.subscribe(value => {
called += 1;
});
store.set(obj);
assert.equal(called, 2);
store.update(obj => obj);
assert.equal(called, 3);
});
});
describe('readable', () => {
it('creates a readable store', () => {
let running;
let tick;
const store = readable(undefined, set => {
tick = set;
running = true;
set(0);
return () => {
tick = () => {};
running = false;
};
});
assert.ok(!running);
const values = [];
const unsubscribe = store.subscribe(value => {
values.push(value);
});
assert.ok(running);
tick(1);
tick(2);
unsubscribe();
assert.ok(!running);
tick(3);
tick(4);
assert.deepEqual(values, [0, 1, 2]);
});
});
describe('derived', () => {
it('maps a single store', () => {
const a = writable(1);
const b = derived(a, n => n * 2);
const values = [];
const unsubscribe = b.subscribe(value => {
values.push(value);
});
a.set(2);
assert.deepEqual(values, [2, 4]);
unsubscribe();
a.set(3);
assert.deepEqual(values, [2, 4]);
});
it('maps multiple stores', () => {
const a = writable(2);
const b = writable(3);
const c = derived(([a, b]), ([a, b]) => a * b);
const values = [];
const unsubscribe = c.subscribe(value => {
values.push(value);
});
a.set(4);
b.set(5);
assert.deepEqual(values, [6, 12, 20]);
unsubscribe();
a.set(6);
assert.deepEqual(values, [6, 12, 20]);
});
it('passes optional set function', () => {
const number = writable(1);
const evens = derived(number, (n, set) => {
if (n % 2 === 0) set(n);
}, 0);
const values = [];
const unsubscribe = evens.subscribe(value => {
values.push(value);
});
number.set(2);
number.set(3);
number.set(4);
number.set(5);
assert.deepEqual(values, [0, 2, 4]);
unsubscribe();
number.set(6);
number.set(7);
number.set(8);
assert.deepEqual(values, [0, 2, 4]);
});
it('prevents glitches', () => {
const lastname = writable('Jekyll');
const firstname = derived(lastname, n => n === 'Jekyll' ? 'Henry' : 'Edward');
const fullname = derived([firstname, lastname], names => names.join(' '));
const values = [];
const unsubscribe = fullname.subscribe(value => {
values.push(value);
});
lastname.set('Hyde');
assert.deepEqual(values, [
'Henry Jekyll',
'Edward Hyde'
]);
unsubscribe();
});
});
describe('get', () => {
it('gets the current value of a store', () => {
const store = readable(42, () => {});
assert.equal(get(store), 42);
});
});
});