forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall-words-in-trie.test.js
67 lines (52 loc) · 1.89 KB
/
all-words-in-trie.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const allWordsInTrie = require('./index');
const Trie = require('../index');
describe('Data Structure : Trie : All Words In Tree', () => {
it('Should return empty array', () => {
const trie = new Trie();
const result = allWordsInTrie(trie.root);
expect(result.length).toEqual(0);
});
it('Should return all words sorted alphabetically', () => {
const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
const trie = new Trie();
words.forEach(word => trie.insert(word));
const result = allWordsInTrie(trie.root);
const expected = ['apple', 'ball', 'bed', 'bed', 'java', 'javascript'];
expect(expected).toEqual(result);
});
it('Should retain duplicates', () => {
const words = ['bed', 'bed', 'bed'];
const trie = new Trie();
words.forEach(word => trie.insert(word));
const result = allWordsInTrie(trie.root);
expect(result.length).toBe(3);
});
it('passing an empty array of words returns an empty array', () => {
const words = [];
const trie = new Trie();
words.forEach(word => trie.insert(word));
const result = allWordsInTrie(trie.root);
expect(result).toEqual([]);
});
it('passing an empty Trie will throw an error ', () => {
const trie = new Trie();
expect(() => {
allWordsInTrie(trie);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing an empty array will throw an error ', () => {
expect(() => {
allWordsInTrie([]);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing null will throw an error ', () => {
expect(() => {
allWordsInTrie([]);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing an array not in a Trie will throw an error ', () => {
expect(() => {
allWordsInTrie(['bed', 'ball', 'apple']);
}).toThrow('Invalid argument: Root of Trie is required');
});
});