fix(tests): Standardize test syntax and update testing dependencies

This commit is contained in:
2025-05-12 13:42:49 +00:00
parent 283703ec78
commit 45951d2610
8 changed files with 144 additions and 43 deletions

View File

@ -31,4 +31,4 @@ tap.test('should sort objects', async () => {
console.log(result[0].matches);
});
tap.start();
export default tap.start();

View File

@ -1,19 +1,81 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as smartfuzzy from '../ts/index.js';
tap.test('should sort objects', async () => {
class Car {
constructor(public brand: string) {}
}
class Car {
constructor(public brand: string, public model?: string, public year?: number) {}
}
const testObjectSorter = new smartfuzzy.ObjectSorter<Car>([
new Car('BMW'),
new Car('Mercedes Benz'),
new Car('Volvo'),
]);
const testCars = [
new Car('BMW', 'X5', 2022),
new Car('Mercedes Benz', 'S-Class', 2021),
new Car('Volvo', 'XC90', 2023),
new Car('Volkswagen', 'Golf', 2020),
new Car('Audi', 'A4', 2022),
];
const result = testObjectSorter.sort('Volvo', ['brand']);
console.log(result);
let objectSorter: smartfuzzy.ObjectSorter<Car>;
tap.test('should create an instance of ObjectSorter', async () => {
objectSorter = new smartfuzzy.ObjectSorter<Car>(testCars);
expect(objectSorter).toBeInstanceOf(smartfuzzy.ObjectSorter);
expect(objectSorter.objectDictionary).toEqual(testCars);
// Test empty constructor
const emptyObjectSorter = new smartfuzzy.ObjectSorter<Car>();
expect(emptyObjectSorter.objectDictionary).toEqual([]);
});
tap.start();
tap.test('should sort objects by exact brand match', async () => {
const result = objectSorter.sort('Volvo', ['brand']);
// Should return an array of results
expect(result).toBeArray();
expect(result.length).toBeGreaterThan(0);
// First result should be the Volvo
expect(result[0].item.brand).toEqual('Volvo');
// Should have expected result structure
expect(result[0]).toHaveProperty('item');
expect(result[0]).toHaveProperty('refIndex');
expect(result[0].refIndex).toBeTypeofNumber();
// Reference index should match the original array position
expect(result[0].refIndex).toEqual(2); // Volvo is at index 2
});
tap.test('should sort objects by fuzzy brand match', async () => {
// "Wolvo" should fuzzy match to "Volvo"
const result = objectSorter.sort('Wolvo', ['brand']);
expect(result.length).toBeGreaterThan(0);
expect(result[0].item.brand).toEqual('Volvo');
});
tap.test('should sort objects by multiple field search', async () => {
// Add a car with similar model name but different brand
objectSorter = new smartfuzzy.ObjectSorter<Car>([
...testCars,
new Car('Toyota', 'X5 Replica', 2020),
]);
// Search across both brand and model
const result = objectSorter.sort('BMW X5', ['brand', 'model']);
expect(result.length).toBeGreaterThan(0);
// BMW X5 should be first result
expect(result[0].item.brand).toEqual('BMW');
expect(result[0].item.model).toEqual('X5');
// Toyota X5 Replica should also be in results but lower ranked
const toyotaResult = result.find(r => r.item.brand === 'Toyota');
expect(toyotaResult).toBeDefined();
// Toyota should be ranked lower than BMW
const bmwIndex = result.findIndex(r => r.item.brand === 'BMW');
const toyotaIndex = result.findIndex(r => r.item.brand === 'Toyota');
expect(bmwIndex).toBeLessThan(toyotaIndex);
});
export default tap.start();

View File

@ -2,25 +2,66 @@ import { expect, tap } from '@push.rocks/tapbundle';
import * as smartfuzzy from '../ts/index.js';
let testSmartfuzzy: smartfuzzy.Smartfuzzy;
const testDictionary = [
'Sony',
'Deutsche Bahn',
'Apple Inc.',
"Trader Joe's",
];
tap.test('should create an instance of Smartfuzzy', async () => {
testSmartfuzzy = new smartfuzzy.Smartfuzzy([
'Sony',
'Deutsche Bahn',
'Apple Inc.',
"Trader Joe's",
]);
testSmartfuzzy = new smartfuzzy.Smartfuzzy(testDictionary);
expect(testSmartfuzzy).toBeInstanceOf(smartfuzzy.Smartfuzzy);
expect(testSmartfuzzy.dictionary).toEqual(testDictionary);
});
tap.test('should compute a score', async () => {
tap.test('should compute a score for a string against the dictionary', async () => {
const result = testSmartfuzzy.getChangeScoreForString('Apple');
console.log(result);
// Check that we got a dictionary map back
expect(result).toBeTypeOf('object');
// Check that every dictionary entry has a score
for (const word of testDictionary) {
expect(result).toHaveProperty(word);
expect(result[word]).toBeTypeofNumber();
}
// Check that 'Apple Inc.' has a lower score (better match) than other entries
expect(result['Apple Inc.']).toBeLessThan(result['Sony']);
});
tap.test('should get closest match', async () => {
tap.test('should get closest match for a string', async () => {
const result = testSmartfuzzy.getClosestMatchForString('Apple');
console.log(result);
// Should return closest match as string
expect(result).toBeTypeofString();
// Should match the expected closest entry
expect(result).toEqual('Apple Inc.');
});
tap.start();
tap.test('should add words to dictionary', async () => {
const initialLength = testSmartfuzzy.dictionary.length;
// Add a single word
testSmartfuzzy.addToDictionary('Microsoft');
expect(testSmartfuzzy.dictionary.length).toEqual(initialLength + 1);
expect(testSmartfuzzy.dictionary).toContain('Microsoft');
// Add multiple words
const additionalWords = ['Google', 'Amazon', 'Facebook'];
testSmartfuzzy.addToDictionary(additionalWords);
expect(testSmartfuzzy.dictionary.length).toEqual(initialLength + 4);
for (const word of additionalWords) {
expect(testSmartfuzzy.dictionary).toContain(word);
}
});
tap.test('should handle empty query string', async () => {
const result = testSmartfuzzy.getClosestMatchForString('');
// For empty strings, behavior should be defined (either null or a specific result)
expect(result).toBeNullOrUndefined();
});
export default tap.start();