UNPKG

1.43 kBJavaScriptView Raw
1var test = require('tape');
2var KeyTree = require('../key-tree-store');
3
4
5test('`add` should store objects', function (t) {
6 var tree = new KeyTree();
7 var one = {id: 'one'};
8 var two = {id: 'two'};
9 var three = {id: 'three'};
10 var four = {id: 'four'};
11
12 tree.add('first', one);
13 tree.add('first.second', two);
14 tree.add('first.second', three);
15 tree.add('first.second.third', four);
16
17 t.equal(Object.keys(tree.storage).length, 3);
18
19 t.equal(tree.storage['first.second'].length, 2, 'should be two for `first.second` key');
20 t.equal(tree.get().length, 4, 'should return all');
21 t.equal(tree.get('first').length, 4, 'should be 4 that match');
22 t.equal(tree.get('first.second').length, 3, 'should be 3 that match');
23 t.equal(tree.get('first.second.third').length, 1, 'should be 1 that match');
24
25 t.equal(tree.get('second.third').length, 0, 'keypaths should start at the start');
26
27 t.equal(tree.get('first.seco').length, 0, 'keypaths must be the full path, or end in a . to match');
28
29 t.deepEqual(tree.getGrouped('first.second'), {
30 'first.second': [two, three],
31 'first.second.third': [four]
32 });
33
34 t.deepEqual(tree.getGrouped(), {
35 'first': [one],
36 'first.second': [two, three],
37 'first.second.third': [four]
38 });
39
40 tree.remove(two);
41 t.equal(tree.get('first').length, 3, 'should be 3 that match after removal');
42
43
44 t.end();
45});