|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { isFuzzyMatch, levenshteinDistance, normalizeString } from ".." |
| 3 | + |
| 4 | +describe('fuzzySearch', () => { |
| 5 | + describe('normalizeString', () => { |
| 6 | + test('returns correct string', () => { |
| 7 | + expect(normalizeString('île-de-France')).toBe('ile de france') |
| 8 | + }) |
| 9 | + }) |
| 10 | + |
| 11 | + describe('levenshteinDistance', () => { |
| 12 | + test('returns correct lenvenshtein distance', () => { |
| 13 | + expect(levenshteinDistance('test', 'test')).toBe(0) |
| 14 | + |
| 15 | + expect(levenshteinDistance('tests', 'test')).toBe(1) |
| 16 | + expect(levenshteinDistance('test', 'tests')).toBe(1) |
| 17 | + |
| 18 | + expect(levenshteinDistance('tset', 'test')).toBe(2) |
| 19 | + |
| 20 | + expect(levenshteinDistance('hello', 'test')).toBe(4) |
| 21 | + |
| 22 | + expect(levenshteinDistance('', 'test')).toBe(4) |
| 23 | + expect(levenshteinDistance('test', '0')).toBe(4) |
| 24 | + }) |
| 25 | + }) |
| 26 | + describe('fuzzySearch', () => { |
| 27 | + test('with default distance (1)', () => { |
| 28 | + expect(isFuzzyMatch('test', 'test')).toBeTruthy() |
| 29 | + expect(isFuzzyMatch('tests', 'test')).toBeFalsy() |
| 30 | + expect(isFuzzyMatch('test', 'tests')).toBeTruthy() |
| 31 | + expect(isFuzzyMatch('tset', 'test')).toBeFalsy() |
| 32 | + expect(isFuzzyMatch('hello', 'test')).toBeFalsy() |
| 33 | + expect(isFuzzyMatch('', 'test')).toBeTruthy() |
| 34 | + }) |
| 35 | + |
| 36 | + test('with distance = 0 (exact match)', () => { |
| 37 | + expect(isFuzzyMatch('test', 'test', 0)).toBeTruthy() |
| 38 | + expect(isFuzzyMatch('tests', 'test', 0)).toBeFalsy() |
| 39 | + expect(isFuzzyMatch('test', 'tests', 0)).toBeTruthy() |
| 40 | + expect(isFuzzyMatch('tset', 'test', 0)).toBeFalsy() |
| 41 | + expect(isFuzzyMatch('hello', 'test', 0)).toBeFalsy() |
| 42 | + expect(isFuzzyMatch('', 'test')).toBeTruthy() |
| 43 | + }) |
| 44 | + |
| 45 | + test('with distance = 2 (swap tolerant)', () => { |
| 46 | + expect(isFuzzyMatch('test', 'test', 2)).toBeTruthy() |
| 47 | + expect(isFuzzyMatch('tests', 'test', 2)).toBeFalsy() |
| 48 | + expect(isFuzzyMatch('test', 'tests', 2)).toBeTruthy() |
| 49 | + expect(isFuzzyMatch('tset', 'test', 2)).toBeTruthy() |
| 50 | + expect(isFuzzyMatch('hello', 'test', 2)).toBeFalsy() |
| 51 | + expect(isFuzzyMatch('', 'test')).toBeTruthy() |
| 52 | + }) |
| 53 | + }) |
| 54 | +}) |
0 commit comments