48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { searchRecipes } from '../public/js/recipe-search.js';
|
|
|
|
function recipe(id, overrides = {}) {
|
|
return {
|
|
_id: id,
|
|
title: '기본 레시피',
|
|
summary: null,
|
|
tags: [],
|
|
ingredientGroups: [{ name: '재료', items: [] }],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test('재료 정확 일치부터 부분 일치, 제목, 태그, 요약 순으로 검색한다', () => {
|
|
const recipes = [
|
|
recipe('summary', { summary: '감자를 맛있게 먹는 방법' }),
|
|
recipe('tag', { tags: ['감자'] }),
|
|
recipe('title', { title: '감자 수프' }),
|
|
recipe('partial', {
|
|
ingredientGroups: [{ name: '재료', items: [{ name: '알감자' }] }],
|
|
}),
|
|
recipe('exact', {
|
|
ingredientGroups: [{ name: '재료', items: [{ name: '감자' }, { name: '감자전분' }] }],
|
|
}),
|
|
];
|
|
|
|
const matches = searchRecipes(recipes, ' 감자 ');
|
|
|
|
assert.deepEqual(matches.map(({ recipe: item }) => item._id), [
|
|
'exact',
|
|
'partial',
|
|
'title',
|
|
'tag',
|
|
'summary',
|
|
]);
|
|
assert.deepEqual(matches[0].matchedIngredients, ['감자', '감자전분']);
|
|
});
|
|
|
|
test('검색어가 없으면 기존 레시피 순서를 유지한다', () => {
|
|
const recipes = [recipe('new'), recipe('old')];
|
|
assert.deepEqual(
|
|
searchRecipes(recipes, '').map(({ recipe: item }) => item._id),
|
|
['new', 'old'],
|
|
);
|
|
});
|