Files: 70bdc654bff8ac23d9006bcbee7a5fa93d8abf62 / test / array.js
2172 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('Array will call subscriber when an observable in the array is updated', function(t) { |
38 | var obs = MutantValue(1) |
39 | var array = MutantArray([obs]) |
40 | array(arr => { |
41 | actual = arr[0] |
42 | t.equal(actual, 2) |
43 | t.end() |
44 | }) |
45 | obs.set(2) |
46 | }) |
47 | |
48 | test('#push inserts a new element and calls subscriber', function(t) { |
49 | var expected = ['cat','dog'] |
50 | var array = MutantArray(['cat']) |
51 | array(actual => { |
52 | t.deepEqual(actual, expected) |
53 | t.end() |
54 | }) |
55 | array.push('dog') |
56 | }) |
57 | |
58 | test('#insert inserts element at index and calls subscriber', function(t) { |
59 | var expected = ['dog', 'cat'] |
60 | var array = MutantArray(['cat']) |
61 | array(actual => { |
62 | t.deepEqual(actual, expected) |
63 | t.end() |
64 | }) |
65 | array.insert('dog', 0) |
66 | }) |
67 | |
68 | |
69 | test('#delete removes basic element and calls subscriber', function(t) { |
70 | var expected = ['dog'] |
71 | var array = MutantArray(['cat', 'dog']) |
72 | array(actual => { |
73 | t.deepEqual(actual, expected) |
74 | t.end() |
75 | }) |
76 | array.delete('cat') |
77 | }) |
78 | |
79 | test('#delete when deleting an observable in an array, remaining observables still call their subscribers', function(t) { |
80 | t.plan(3) |
81 | var cat = MutantValue('cat') |
82 | cat(t.ok) |
83 | |
84 | var dog = MutantValue('dog') |
85 | dog(t.fail) |
86 | |
87 | var array = MutantArray([cat, dog]) |
88 | array(t.ok) |
89 | array.delete(dog) //array will emit |
90 | cat.set('Kool kat') //array and cat will emit. |
91 | }) |
92 | |
93 |
Built with git-ssb-web