git ssb

1+

Matt McKegg / mutant



Tree: dc1e0540e6cdbe6af6955e06be52bb58d0e33115

Files: dc1e0540e6cdbe6af6955e06be52bb58d0e33115 / test / array.js

2170 bytesRaw
1require('setimmediate')
2var test = require('tape')
3
4var MutantArray = require('../array')
5var MutantValue = require('../value')
6
7test('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
14test('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
24test('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
37test('#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
47test('#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
57test('#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
67test('#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
81test('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