|
| 1 | +/* global hotkey */ |
| 2 | + |
| 3 | +const {RadixTrie, Leaf} = hotkey |
| 4 | + |
| 5 | +describe('RadixTrie', () => { |
| 6 | + describe('insert', () => { |
| 7 | + it('adds hotkey to trie in a searchable manner', () => { |
| 8 | + const trie = new RadixTrie() |
| 9 | + const leaf = trie.insert(['ctrl+p', 'a', 'b']) |
| 10 | + |
| 11 | + assert(trie.get('ctrl+p'), 'missing `ctrl+p` in trie') |
| 12 | + assert(trie.get('ctrl+p').get('a'), 'missing `ctrl+p a` in trie') |
| 13 | + assert( |
| 14 | + trie |
| 15 | + .get('ctrl+p') |
| 16 | + .get('a') |
| 17 | + .get('b'), |
| 18 | + 'missing `ctrl+p a b` in trie' |
| 19 | + ) |
| 20 | + assert.equal( |
| 21 | + trie |
| 22 | + .get('ctrl+p') |
| 23 | + .get('a') |
| 24 | + .get('b'), |
| 25 | + leaf, |
| 26 | + 'didnt return leaf correctly' |
| 27 | + ) |
| 28 | + assert.instanceOf(leaf, Leaf, 'leaf isnt a Leaf instance') |
| 29 | + }) |
| 30 | + |
| 31 | + it('adds new hotkey to trie using existing maps', () => { |
| 32 | + const trie = new RadixTrie() |
| 33 | + const leaf = trie.insert(['ctrl+p', 'a', 'b']) |
| 34 | + const otherLeaf = trie.insert(['ctrl+p', 'a', 'c']) |
| 35 | + |
| 36 | + assert.equal( |
| 37 | + trie |
| 38 | + .get('ctrl+p') |
| 39 | + .get('a') |
| 40 | + .get('b'), |
| 41 | + leaf, |
| 42 | + 'didnt return `ctrl+p a b` end leaf correctly' |
| 43 | + ) |
| 44 | + assert.equal( |
| 45 | + trie |
| 46 | + .get('ctrl+p') |
| 47 | + .get('a') |
| 48 | + .get('c'), |
|
10000
49 | + otherLeaf, |
| 50 | + 'didnt return `ctrl+p a c` end leaf correctly' |
| 51 | + ) |
| 52 | + assert.notEqual(leaf, otherLeaf, 'leaves are same reference but shouldnt be') |
| 53 | + assert.instanceOf(leaf, Leaf, 'leaf isnt a Leaf instance') |
| 54 | + assert.instanceOf(otherLeaf, Leaf, 'otherLeaf isnt a Leaf instance') |
| 55 | + }) |
| 56 | + |
| 57 | + it('overrides leafs with new deeper insertions', () => { |
| 58 | + const trie = new RadixTrie() |
| 59 | + const otherLeaf = trie.insert(['g', 'c', 'e']) |
| 60 | + |
| 61 | + assert.instanceOf(trie.get('g').get('c'), RadixTrie, 'didnt override `g c` leaf as trie') |
| 62 | + assert.equal( |
| 63 | + trie |
| 64 | + .get('g') |
| 65 | + .get('c') |
| 66 | + .get('e'), |
| 67 | + otherLeaf, |
| 68 | + 'didnt add `g c e` leaf to trie' |
| 69 | + ) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('delete', () => { |
| 74 | + it('removes self from parents, if empty', () => { |
| 75 | + const trie = new RadixTrie() |
| 76 | + const leaf = trie.insert(['ctrl+p', 'a', 'b']) |
| 77 | + const keyATrie = trie.get('ctrl+p').get('a') |
| 78 | + const success = leaf.parent.delete(leaf) |
| 79 | + |
| 80 | + assert(success, 'delete was unsucessful') |
| 81 | + assert.isUndefined(trie.get('ctrl+p'), 'still has ctrl+p leaf') |
| 82 | + assert.isUndefined(keyATrie.get('b'), 'keyAtrie still has b key child') |
| 83 | + }) |
| 84 | + |
| 85 | + it('preserves parents with other tries', () => { |
| 86 | + const trie = new RadixTrie() |
| 87 | + trie.insert(['ctrl+p', 'a', 'b']) |
| 88 | + const otherLeaf = trie.insert(['ctrl+p', 'a', 'c']) |
| 89 | + const keyATrie = trie.get('ctrl+p').get('a') |
| 90 | + const keyCTrie = keyATrie.get('c') |
| 91 | + const success = otherLeaf.parent.delete(otherLeaf) |
| 92 | + |
| 93 | + assert(success, 'delete was unsucessful') |
| 94 | + assert.equal(keyCTrie.children.length, 0, '`c` trie still has children') |
| 95 | + }) |
| 96 | + }) |
| 97 | +}) |
0 commit comments