-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/frontend/src/features/addNote/lib/searchProductsByName.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { type ProductSelectOption } from '@/entities/product'; | ||
import { searchProductsByName } from './searchProductsByName'; | ||
|
||
type GivenProducts = 'cheese' | 'cheesecake' | 'chocolateCake' | 'creamCheese'; | ||
|
||
const given: Record<GivenProducts, ProductSelectOption> = { | ||
cheese: { id: 1, name: 'Cheese', defaultQuantity: 120 }, | ||
cheesecake: { id: 2, name: 'Cheesecake', defaultQuantity: 50 }, | ||
chocolateCake: { id: 3, name: 'Chocolate cake', defaultQuantity: 60 }, | ||
creamCheese: { id: 4, name: 'Cream cheese', defaultQuantity: 100 }, | ||
}; | ||
|
||
const sourceProducts = Object.values(given); | ||
|
||
test.each<[string, ProductSelectOption[]]>([ | ||
['che', [given.cheese, given.cheesecake, given.creamCheese]], | ||
['cream', [given.creamCheese]], | ||
['Cake ', [given.cheesecake, given.chocolateCake]], | ||
])('should find products by search query: "%s"', (query, foundProducts) => { | ||
expect(searchProductsByName(sourceProducts, query)).toStrictEqual(foundProducts); | ||
}); | ||
|
||
test.each<string>(['', ' ', 'ch', 'sadsfafds'])( | ||
'should not find any products by search query: "%s"', | ||
query => { | ||
expect(searchProductsByName(sourceProducts, query)).toStrictEqual<ProductSelectOption[]>([]); | ||
}, | ||
); |
18 changes: 18 additions & 0 deletions
18
src/frontend/src/features/addNote/lib/searchProductsByName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { type ProductSelectOption } from '@/entities/product'; | ||
|
||
const QUERY_LENGTH_THRESHOLD = 3; | ||
|
||
export const searchProductsByName = ( | ||
sourceProducts: ProductSelectOption[], | ||
query: string, | ||
): ProductSelectOption[] => { | ||
const queryTrimmed = query.trim(); | ||
|
||
if (queryTrimmed.length < QUERY_LENGTH_THRESHOLD) { | ||
return []; | ||
} | ||
|
||
return sourceProducts.filter(product => | ||
product.name.trim().toLowerCase().includes(queryTrimmed.toLowerCase()), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters