|  | 
|  | 1 | +const Trie = require('./trie'); | 
|  | 2 | + | 
|  | 3 | +describe('Trie', () => { | 
|  | 4 | +  let trie; | 
|  | 5 | + | 
|  | 6 | +  beforeEach(() => { | 
|  | 7 | +    trie = new Trie(); | 
|  | 8 | +  }); | 
|  | 9 | + | 
|  | 10 | +  describe('construtor', () => { | 
|  | 11 | +    it('should initialize trie', () => { | 
|  | 12 | +      expect(trie).toBeDefined(); | 
|  | 13 | +    }); | 
|  | 14 | + | 
|  | 15 | +    it('should set default value to undefined', () => { | 
|  | 16 | +      expect(trie.val).toEqual(undefined); | 
|  | 17 | +    }); | 
|  | 18 | + | 
|  | 19 | +    it('should initialization value', () => { | 
|  | 20 | +      trie = new Trie(1); | 
|  | 21 | +      expect(trie.val).toEqual(1); | 
|  | 22 | +    }); | 
|  | 23 | + | 
|  | 24 | +    it('should initialize children as empty map', () => { | 
|  | 25 | +      expect(trie.children).toEqual({}); | 
|  | 26 | +    }); | 
|  | 27 | + | 
|  | 28 | +    it('should not be a word by default', () => { | 
|  | 29 | +      expect(trie.isWord).toEqual(false); | 
|  | 30 | +    }); | 
|  | 31 | +  }); | 
|  | 32 | + | 
|  | 33 | +  describe('insert', () => { | 
|  | 34 | +    it('should insert a word', () => { | 
|  | 35 | +      trie.insert('ab'); | 
|  | 36 | +      expect(trie.children.a).toBeDefined(); | 
|  | 37 | +      expect(trie.children.a.children.b).toBeDefined(); | 
|  | 38 | +      expect(trie.children.a.isWord).toEqual(false); | 
|  | 39 | +      expect(trie.children.a.children.b.isWord).toEqual(true); | 
|  | 40 | +    }); | 
|  | 41 | + | 
|  | 42 | +    it('should insert multiple words with the same root', () => { | 
|  | 43 | +      trie.insert('a'); | 
|  | 44 | +      trie.insert('ab'); | 
|  | 45 | +      expect(trie.children.a.isWord).toEqual(true); | 
|  | 46 | +      expect(trie.children.a.children.b.isWord).toEqual(true); | 
|  | 47 | +    }); | 
|  | 48 | +  }); | 
|  | 49 | + | 
|  | 50 | +  describe('search & startsWith', () => { | 
|  | 51 | +    beforeEach(() => { | 
|  | 52 | +      trie.insert('dog'); | 
|  | 53 | +      trie.insert('dogs'); | 
|  | 54 | +      trie.insert('door'); | 
|  | 55 | +    }); | 
|  | 56 | + | 
|  | 57 | +    it('should search for words', () => { | 
|  | 58 | +      expect(trie.search('dog')).toEqual(true); | 
|  | 59 | +    }); | 
|  | 60 | + | 
|  | 61 | +    it('should not match incomplete words by default', () => { | 
|  | 62 | +      expect(trie.search('do')).toEqual(false); | 
|  | 63 | +    }); | 
|  | 64 | + | 
|  | 65 | +    it('should match partial words if partial is set', () => { | 
|  | 66 | +      expect(trie.search('do', { | 
|  | 67 | +        partial: true, | 
|  | 68 | +      })).toEqual(true); | 
|  | 69 | +      expect(trie.startsWith('do')).toEqual(true); | 
|  | 70 | +    }); | 
|  | 71 | + | 
|  | 72 | +    it('should not match non existing words', () => { | 
|  | 73 | +      expect(trie.search('doors')).toEqual(false); | 
|  | 74 | +    }); | 
|  | 75 | + | 
|  | 76 | +    it('should not match non existing words with partials', () => { | 
|  | 77 | +      expect(trie.search('doors', { | 
|  | 78 | +        partial: true, | 
|  | 79 | +      })).toEqual(false); | 
|  | 80 | +      expect(trie.startsWith('doors')).toEqual(false); | 
|  | 81 | +    }); | 
|  | 82 | +  }); | 
|  | 83 | + | 
|  | 84 | +  describe('when multiple words are inserted', () => { | 
|  | 85 | +    beforeEach(() => { | 
|  | 86 | +      trie.insert('dog'); | 
|  | 87 | +      trie.insert('dogs'); | 
|  | 88 | +      trie.insert('door'); | 
|  | 89 | +      trie.insert('day'); | 
|  | 90 | +      trie.insert('cat'); | 
|  | 91 | +    }); | 
|  | 92 | + | 
|  | 93 | +    describe('getAllWords', () => { | 
|  | 94 | +      it('should get all words', () => { | 
|  | 95 | +        const words = trie.getAllWords(); | 
|  | 96 | +        expect(words.length).toEqual(5); | 
|  | 97 | +        expect(words).toEqual(['dog', 'dogs', 'door', 'day', 'cat']); | 
|  | 98 | +      }); | 
|  | 99 | + | 
|  | 100 | +      it('should use prefix', () => { | 
|  | 101 | +        const words = trie.getAllWords("Adrian's "); | 
|  | 102 | +        expect(words.length).toEqual(5); | 
|  | 103 | +        expect(words).toEqual([ | 
|  | 104 | +          "Adrian's dog", | 
|  | 105 | +          "Adrian's dogs", | 
|  | 106 | +          "Adrian's door", | 
|  | 107 | +          "Adrian's day", | 
|  | 108 | +          "Adrian's cat", | 
|  | 109 | +        ]); | 
|  | 110 | +      }); | 
|  | 111 | +    }); | 
|  | 112 | + | 
|  | 113 | +    describe('autocomplete', () => { | 
|  | 114 | +      it('should return all words if not prefix is given', () => { | 
|  | 115 | +        const words = trie.autocomplete(); | 
|  | 116 | +        expect(words.length).toBe(5); | 
|  | 117 | +        expect(words).toEqual(['dog', 'dogs', 'door', 'day', 'cat']); | 
|  | 118 | +      }); | 
|  | 119 | + | 
|  | 120 | +      it('should auto complete words given a prefix', () => { | 
|  | 121 | +        const words = trie.autocomplete('do'); | 
|  | 122 | +        expect(words.length).toBe(3); | 
|  | 123 | +        expect(words).toEqual(['dog', 'dogs', 'door']); | 
|  | 124 | +      }); | 
|  | 125 | + | 
|  | 126 | +      it('should handle non-existing words prefixes', () => { | 
|  | 127 | +        const words = trie.autocomplete('co'); | 
|  | 128 | +        expect(words.length).toBe(0); | 
|  | 129 | +        expect(words).toEqual([]); | 
|  | 130 | +      }); | 
|  | 131 | +    }); | 
|  | 132 | +  }); | 
|  | 133 | +}); | 
0 commit comments