@@ -13,4 +13,49 @@ describe('Data Structure : Trie : All Words In Tree', () => {
13
13
const expected = [ 'apple' , 'ball' , 'bed' , 'bed' , 'java' , 'javascript' ] ;
14
14
expect ( expected ) . toEqual ( result ) ;
15
15
} ) ;
16
+
17
+ it ( 'Should retain duplicates' , ( ) => {
18
+ const words = [ 'bed' , 'bed' , 'bed' ] ;
19
+ const trie = new Trie ( ) ;
20
+
21
+ words . forEach ( word => trie . insert ( word ) ) ;
22
+
23
+ const result = allWordsInTrie ( trie . root ) ;
24
+ expect ( result . length ) . toBe ( 3 ) ;
25
+ } ) ;
26
+
27
+ it ( 'passing an empty array of words returns an empty array' , ( ) => {
28
+ const words = [ ] ;
29
+ const trie = new Trie ( ) ;
30
+
31
+ words . forEach ( word => trie . insert ( word ) ) ;
32
+
33
+ const result = allWordsInTrie ( trie . root ) ;
34
+ expect ( result ) . toEqual ( [ ] ) ;
35
+ } ) ;
36
+
37
+ it ( 'passing an empty Trie will throw an error ' , ( ) => {
38
+ const trie = new Trie ( ) ;
39
+ expect ( ( ) => {
40
+ allWordsInTrie ( trie ) ;
41
+ } ) . toThrow ( 'Cannot read property \'0\' of undefined' ) ;
42
+ } ) ;
43
+
44
+ it ( 'passing an empty array will throw an error ' , ( ) => {
45
+ expect ( ( ) => {
46
+ allWordsInTrie ( [ ] ) ;
47
+ } ) . toThrow ( 'Cannot read property \'0\' of undefined' ) ;
48
+ } ) ;
49
+
50
+ it ( 'passing null will throw an error ' , ( ) => {
51
+ expect ( ( ) => {
52
+ allWordsInTrie ( [ ] ) ;
53
+ } ) . toThrow ( 'Cannot read property \'0\' of undefined' ) ;
54
+ } ) ;
55
+
56
+ it ( 'passing an array not in a Trie will throw an error ' , ( ) => {
57
+ expect ( ( ) => {
58
+ allWordsInTrie ( [ 'bed' , 'ball' , 'apple' ] ) ;
59
+ } ) . toThrow ( 'Cannot read property \'0\' of undefined' ) ;
60
+ } ) ;
16
61
} ) ;
0 commit comments