-
Notifications
You must be signed in to change notification settings - Fork 269
/
get-unique-words.test.js
61 lines (47 loc) · 1.74 KB
/
get-unique-words.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
const getUniqueWords = require('./index');
const Trie = require('../index');
describe('Data Structure : Trie : Get unique words', () => {
it('Should returns unique words (no duplicates), sorted alphabetically', () => {
const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
const trie = new Trie();
words.forEach((word) => trie.insert(word));
const result = getUniqueWords(trie.root);
const expected = ['apple', 'ball', 'bed', 'java', 'javascript'];
expect(result).toEqual(expected);
});
it('removes duplicates', () => {
const words = ['bed', 'bed', 'bed'];
const trie = new Trie();
words.forEach((word) => trie.insert(word));
const result = getUniqueWords(trie.root);
expect(result.length).toBe(1);
});
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 = getUniqueWords(trie.root);
expect(result).toEqual([]);
});
it('passing an empty Trie will throw an error ', () => {
const trie = new Trie();
expect(() => {
getUniqueWords(trie);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing an empty array will throw an error ', () => {
expect(() => {
getUniqueWords([]);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing null will throw an error ', () => {
expect(() => {
getUniqueWords([]);
}).toThrow('Invalid argument: Root of Trie is required');
});
it('passing an array not in a Trie will throw an error ', () => {
expect(() => {
getUniqueWords(['bed', 'ball', 'apple']);
}).toThrow('Invalid argument: Root of Trie is required');
});
});