Files: dc1e0540e6cdbe6af6955e06be52bb58d0e33115 / test / array.js
2170 bytesRaw
1 | require('setimmediate') |
2 | var test = require('tape') |
3 | |
4 | var MutantArray = require('../array') |
5 | var MutantValue = require('../value') |
6 | |
7 | test('Observable returns contents when called ', function(t) { |
8 | var array = MutantArray(['cat']) |
9 | var actual = array() |
10 | t.deepEqual(actual, ['cat']) |
11 | t.end() |
12 | }) |
13 | |
14 | test('Observable calls subscribe with latest when updated with set', function(t) { |
15 | var expected = ['dog'] |
16 | var array = MutantArray(['cat']) |
17 | array(actual => { |
18 | t.deepEqual(actual, expected) |
19 | t.end() |
20 | }) |
21 | array.set(expected) |
22 | }) |
23 | |
24 | test('Observable calls multiple subscribers with latest when updated with set', function(t) { |
25 | t.plan(2) |
26 | var expected = ['dog'] |
27 | var array = MutantArray(['cat']) |
28 | array(actual => { |
29 | t.deepEqual(actual, expected) |
30 | }) |
31 | array(actual => { |
32 | t.deepEqual(actual, expected) |
33 | }) |
34 | array.set(expected) |
35 | }) |
36 | |
37 | test('#push inserts a new element and calls subscriber', function(t) { |
38 | var expected = ['cat','dog'] |
39 | var array = MutantArray(['cat']) |
40 | array(actual => { |
41 | t.deepEqual(actual, expected) |
42 | t.end() |
43 | }) |
44 | array.push('dog') |
45 | }) |
46 | |
47 | test('#insert inserts element at index and calls subscriber', function(t) { |
48 | var expected = ['dog', 'cat'] |
49 | var array = MutantArray(['cat']) |
50 | array(actual => { |
51 | t.deepEqual(actual, expected) |
52 | t.end() |
53 | }) |
54 | array.insert('dog', 0) |
55 | }) |
56 | |
57 | test('#delete removes basic element and calls subscriber', function(t) { |
58 | var expected = ['dog'] |
59 | var array = MutantArray(['cat', 'dog']) |
60 | array(actual => { |
61 | t.deepEqual(actual, expected) |
62 | t.end() |
63 | }) |
64 | array.delete('cat') |
65 | }) |
66 | |
67 | test('#delete when deleting an observable in an array, remaining observables still call their subscribers', function(t) { |
68 | t.plan(3) |
69 | var cat = MutantValue('cat') |
70 | cat(t.ok) |
71 | |
72 | var dog = MutantValue('dog') |
73 | dog(t.fail) |
74 | |
75 | var array = MutantArray([cat, dog]) |
76 | array(t.ok) |
77 | array.delete(dog) //array will emit |
78 | cat.set('Kool kat') //array and cat will emit. |
79 | }) |
80 | |
81 | test('Array will call subscriber when an observable in the array is updated', function(t) { |
82 | var obs = MutantValue(1) |
83 | var array = MutantArray([obs]) |
84 | array(arr => { |
85 | actual = arr[0] |
86 | t.equal(actual, 2) |
87 | t.end() |
88 | }) |
89 | obs.set(2) |
90 | }) |
91 |
Built with git-ssb-web