forked from gakimball/tree-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
36 lines (26 loc) · 829 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const expect = require('chai').expect;
const findById = require('.');
describe('findById()', () => {
const tree = [
{ id: 1 },
{ id: 2, children: [{ id: 3 }]},
{ id: 4, children: [{ id: 5, children: [{ id: 6}] }]},
]
it('creates a search function', () => {
const fn = findById('children');
expect(fn(tree, 'id', 1)).to.eql({ id: 1 });
});
it('works on nested elements', () => {
const fn = findById('children');
expect(fn(tree, 'id', 6)).to.eql({ id: 6 });
});
it('creates a search function with a pre-defined key', () => {
const fn = findById('children', 'id');
expect(fn(tree, 1)).to.eql({ id: 1 });
});
it('returns undefined if nothing is found', () => {
const fn = findById('children');
expect(fn(tree, 'id', 7)).to.be.undefined;
});
});