git ssb

1+

Matt McKegg / mutant



Tree: d21f00b20866259a0cc392aac18a581d68aa23be

Files: d21f00b20866259a0cc392aac18a581d68aa23be / test / computed.js

1234 bytesRaw
1require('setimmediate')
2var test = require('tape')
3
4var computed = require('../computed')
5var resolve = require('../resolve')
6var Value = require('../value')
7
8test('computed get accessor', function(t) {
9 var value = Value(1)
10 var obs = computed(value, x => x * 100)
11 t.equal(resolve(obs), 100)
12 value.set(2)
13 t.equal(resolve(obs), 200)
14 t.end()
15})
16
17test('computed lazy watch', function(t) {
18 var value = Value(1)
19 var obs = computed(value, x => x * 100)
20 t.equal(resolve(obs), 100)
21
22 var observedValues = []
23 obs(x => observedValues.push(x))
24 value.set(2)
25 t.deepEqual(observedValues, [200])
26
27 t.end()
28})
29
30test('computed lazy watch with `get` race condition', function(t) {
31 var value = Value(1)
32 var final = Value()
33
34 var obs = computed(value, x => x * 100)
35 t.equal(resolve(obs), 100)
36
37 value(x => {
38 resolve(obs) // resolve obs inside of observer
39 })
40
41 obs(final.set)
42
43 value.set(2)
44
45 t.deepEqual(resolve(final), 200)
46 t.end()
47})
48
49test('computed inner update in non-live mode', function (t) {
50 var innerValue = Value(1)
51 var value = Value(innerValue)
52 var obs = computed(value, x => x)
53 t.deepEqual(obs(), 1)
54 setImmediate(() => {
55 innerValue.set(2)
56 t.deepEqual(obs(), 2)
57 t.end()
58 })
59})
60

Built with git-ssb-web