Files: 01e9e1d516a0bcf09d486425ab7cba0f919f5a1a / test / computed.js
1234 bytesRaw
1 | require('setimmediate') |
2 | var test = require('tape') |
3 | |
4 | var computed = require('../computed') |
5 | var resolve = require('../resolve') |
6 | var Value = require('../value') |
7 | |
8 | test('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 | |
17 | test('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 | |
30 | test('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 | |
49 | test('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