1
+ import Autocomplete from '../autocomplete-typeahead-search-trie' ;
2
+
3
+ const states = [
4
+ 'Alabama' , 'Alaska' , 'Arizona' , 'Arkansas' , 'California' , 'Colorado' , 'Connecticut' ,
5
+ 'Delaware' , 'Florida' , 'Georgia' , 'Hawaii' , 'Idaho' , 'Illinois' , 'Indiana' , 'Iowa' , 'Kansas' ,
6
+ 'Kentucky' , 'Louisiana' , 'Maine' , 'Maryland' , 'Massachusetts' , 'Michigan' , 'Minnesota' ,
7
+ 'Mississippi' , 'Missouri' , 'Montana' , 'Nebraska' , 'Nevada' , 'New Hampshire' , 'New Jersey' ,
8
+ 'New Mexico' , 'New York' , 'North Carolina' , 'North Dakota' , 'Ohio' , 'Oklahoma' , 'Oregon' ,
9
+ 'Pennsylvania' , 'Rhode Island' , 'South Carolina' , 'South Dakota' , 'Tennessee' , 'Texas' , 'Utah' ,
10
+ 'Vermont' , 'Virginia' , 'Washington' , 'West Virginia' , 'Wisconsin' , 'Wyoming'
11
+ ] ;
12
+
13
+ describe ( 'Autocomplete using Trie' , ( ) => {
14
+
15
+ let autocomplete ;
16
+
17
+ beforeAll ( ( ) => {
18
+ autocomplete = new Autocomplete ( states ) ;
19
+ } ) ;
20
+
21
+ test ( 'should build a trie' , ( ) => {
22
+ const words = [
23
+ 'apple' ,
24
+ 'cat' ,
25
+ 'car' ,
26
+ 'app' ,
27
+ ] ;
28
+ const autocomplete = new Autocomplete ( words ) ;
29
+ const result = JSON . stringify ( autocomplete . root ) ;
30
+ const expected = JSON . stringify ( {
31
+ children : {
32
+ a : {
33
+ children : {
34
+ p : {
35
+ children : {
36
+ p : {
37
+ children : {
38
+ l : {
39
+ children : {
40
+ e : {
41
+ children : { } ,
42
+ last : true ,
43
+ } ,
44
+ } ,
45
+ last : false ,
46
+ } ,
47
+ } ,
48
+ last : true ,
49
+ } ,
50
+ } ,
51
+ last : false ,
52
+ } ,
53
+ } ,
54
+ last : false ,
55
+ } ,
56
+ c : {
57
+ children : {
58
+ a : {
59
+ children : {
60
+ t : {
61
+ children : { } ,
62
+ last : true ,
63
+ } ,
64
+ r : {
65
+ children : { } ,
66
+ last : true ,
67
+ } ,
68
+ } ,
69
+ last : false ,
70
+ } ,
71
+ } ,
72
+ last : false ,
73
+ }
74
+ } ,
75
+ last : false ,
76
+ } ) ;
77
+
78
+ expect ( result ) . toEqual ( expected ) ;
79
+ } ) ;
80
+
81
+ describe ( 'Depth First Search' , ( ) => {
82
+
83
+ test ( 'should return an array' , ( ) => {
84
+ const result = autocomplete . getAutocompleteListDfs ( '' ) ;
85
+ expect ( result ) . toBeInstanceOf ( Array ) ;
86
+ } ) ;
87
+
88
+ test ( 'should return an empty array when there is no match' , ( ) => {
89
+ const result = autocomplete . getAutocompleteListDfs ( 'q' ) ;
90
+ expect ( result ) . toHaveLength ( 0 ) ;
91
+ } ) ;
92
+
93
+ test ( 'should return a list of possible values' , ( ) => {
94
+ const result = autocomplete . getAutocompleteListDfs ( 'New' ) ;
95
+ const expected = [ 'New Hampshire' , 'New Jersey' , 'New Mexico' , 'New York' ] ;
96
+ expect ( result ) . toEqual ( expected ) ;
97
+ } ) ;
98
+
99
+ test ( 'should return an array containing exact one element' , ( ) => {
100
+ const result = autocomplete . getAutocompleteListDfs ( 'New York' ) ;
101
+ const expected = [ 'New York' ] ;
102
+ expect ( result ) . toEqual ( expected ) ;
103
+ } ) ;
104
+
105
+ } ) ;
106
+
107
+ describe ( 'Breath First Search' , ( ) => {
108
+
109
+ test ( 'should return an array' , ( ) => {
110
+ const result = autocomplete . getAutocompleteListBfs ( '' ) ;
111
+ expect ( result ) . toBeInstanceOf ( Array ) ;
112
+ } ) ;
113
+
114
+ test ( 'should return an empty array when there is no match' , ( ) => {
115
+ const result = autocomplete . getAutocompleteListBfs ( 'q' ) ;
116
+ expect ( result ) . toHaveLength ( 0 ) ;
117
+ } ) ;
118
+
119
+ test ( 'should return a list of possible values' , ( ) => {
120
+ const result = autocomplete . getAutocompleteListBfs ( 'New' ) ;
121
+ const expected = [ 'New York' , 'New Jersey' , 'New Mexico' , 'New Hampshire' ] ;
122
+ expect ( result ) . toEqual ( expected ) ;
123
+ } ) ;
124
+
125
+ test ( 'should return an array containing exact one element' , ( ) => {
126
+ const result = autocomplete . getAutocompleteListBfs ( 'New York' ) ;
127
+ const expected = [ 'New York' ] ;
128
+ expect ( result ) . toEqual ( expected ) ;
129
+ } ) ;
130
+
131
+ } ) ;
132
+
133
+
134
+ } ) ;
0 commit comments