fix(tests): Standardize test syntax and update testing dependencies
This commit is contained in:
@@ -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();
|
||||
|
Reference in New Issue
Block a user