This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrieNode.test.js
51 lines (37 loc) · 1.29 KB
/
TrieNode.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import TrieNode from '../TrieNode';
describe('TrieNode', () => {
it('should create trie node', () => {
const trieNode = new TrieNode('c', true);
expect(trieNode.character).toBe('c');
expect(trieNode.isCompleteWord).toBeTruthy();
expect(trieNode.toString()).toBe('c*');
});
it('should add child nodes', () => {
const trieNode = new TrieNode('c');
trieNode.addChild('a', true);
trieNode.addChild('o');
expect(trieNode.toString()).toBe('c:a,o');
});
it('should get child nodes', () => {
const trieNode = new TrieNode('c');
trieNode.addChild('a');
trieNode.addChild('o');
expect(trieNode.getChild('a').toString()).toBe('a');
expect(trieNode.getChild('o').toString()).toBe('o');
expect(trieNode.getChild('b')).toBeUndefined();
});
it('should check if node has specific child', () => {
const trieNode = new TrieNode('c');
trieNode.addChild('a');
trieNode.addChild('o');
expect(trieNode.hasChild('a')).toBeTruthy();
expect(trieNode.hasChild('o')).toBeTruthy();
expect(trieNode.hasChild('b')).toBeFalsy();
});
it('should suggest next children', () => {
const trieNode = new TrieNode('c');
trieNode.addChild('a');
trieNode.addChild('o');
expect(trieNode.suggestChildren()).toEqual(['a', 'o']);
});
});