|
| 1 | +import { |
| 2 | + calculateLevenshteinDistance, |
| 3 | + getCloseMatchesByAttribute, |
| 4 | +} from '../close-matches' |
| 5 | +import {render} from './helpers/test-utils' |
| 6 | + |
| 7 | +describe('calculateLevenshteinDistance', () => { |
| 8 | + test.each([ |
| 9 | + ['', '', 0], |
| 10 | + ['hello', 'hello', 0], |
| 11 | + ['greeting', 'greeting', 0], |
| 12 | + ['react testing library', 'react testing library', 0], |
| 13 | + ['hello', 'hellow', 1], |
| 14 | + ['greetimg', 'greeting', 1], |
| 15 | + ['submit', 'sbmit', 1], |
| 16 | + ['cance', 'cancel', 1], |
| 17 | + ['doug', 'dog', 1], |
| 18 | + ['dogs and cats', 'dogs and cat', 1], |
| 19 | + ['uncool-div', '12cool-div', 2], |
| 20 | + ['dogs and cats', 'dogs, cats', 4], |
| 21 | + ['greeting', 'greetings traveler', 10], |
| 22 | + ['react testing library', '', 21], |
| 23 | + ['react testing library', 'y', 20], |
| 24 | + ['react testing library', 'ty', 19], |
| 25 | + ['react testing library', 'tary', 17], |
| 26 | + ['react testing library', 'trary', 16], |
| 27 | + ['react testing library', 'tlibrary', 13], |
| 28 | + ['react testing library', 'react testing', 8], |
| 29 | + ['library', 'testing', 7], |
| 30 | + ['react library', 'react testing', 7], |
| 31 | + [ |
| 32 | + 'The more your tests resemble the way your software is used, the more confidence they can give you.', |
| 33 | + 'The less your tests resemble the way your software is used, the less confidence they can give you.', |
| 34 | + 8, |
| 35 | + ], |
| 36 | + ])('distance between "%s" and "%s" is %i', (text1, text2, expected) => { |
| 37 | + expect(calculateLevenshteinDistance(text1, text2)).toBe(expected) |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('getCloseMatchesByAttribute', () => { |
| 42 | + test('should return all closest matches', () => { |
| 43 | + const {container} = render(` |
| 44 | + <div data-testid="The slow brown fox jumps over the lazy dog"></div> |
| 45 | + <div data-testid="The rapid brown fox jumps over the lazy dog"></div> |
| 46 | + <div data-testid="The quick black fox jumps over the lazy dog"></div> |
| 47 | + <div data-testid="The quick brown meerkat jumps over the lazy dog"></div> |
| 48 | + <div data-testid="The quick brown fox flies over the lazy dog"></div> |
| 49 | + `) |
| 50 | + expect( |
| 51 | + getCloseMatchesByAttribute( |
| 52 | + 'data-testid', |
| 53 | + container, |
| 54 | + 'The quick brown fox jumps over the lazy dog', |
| 55 | + ), |
| 56 | + ).toEqual([ |
| 57 | + 'The quick black fox jumps over the lazy dog', |
| 58 | + 'The quick brown fox flies over the lazy dog', |
| 59 | + ]) |
| 60 | + }) |
| 61 | + |
| 62 | + test('should ignore matches that are too distant', () => { |
| 63 | + const {container} = render(` |
| 64 | + <div data-testid="very-cool-div"></div> |
| 65 | + <div data-testid="too-diferent-to-match"></div> |
| 66 | + <div data-testid="not-even-close"></div> |
| 67 | + `) |
| 68 | + expect( |
| 69 | + getCloseMatchesByAttribute('data-testid', container, 'normal-div'), |
| 70 | + ).toEqual([]) |
| 71 | + }) |
| 72 | + |
| 73 | + test('should ignore duplicated matches', () => { |
| 74 | + const {container} = render(` |
| 75 | + <div data-testid="lazy dog"></div> |
| 76 | + <div data-testid="lazy dog"></div> |
| 77 | + <div data-testid="lazy dog"></div> |
| 78 | + <div data-testid="energetic dog"></div> |
| 79 | + `) |
| 80 | + expect( |
| 81 | + getCloseMatchesByAttribute('data-testid', container, 'happy dog'), |
| 82 | + ).toEqual(['lazy dog']) |
| 83 | + }) |
| 84 | +}) |
0 commit comments