diff --git a/src/api/bookshop/index.test.ts b/src/api/bookshop/index.test.ts new file mode 100644 index 0000000..d4bacb3 --- /dev/null +++ b/src/api/bookshop/index.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, test, vi } from 'vitest' +import { getFindParamsFilter } from './index' + +// deepseek r1 - plan reason +// antrhopic - claude + +vi.mock('helpers/config/mongodb', () => ({ + clientPromise: Promise.resolve({ + db: vi.fn(), + connect: vi.fn() + }) +})) + +describe('getFindParamsFilter', () => { + test('should return empty object for empty search', () => { + const result = getFindParamsFilter('{}') + expect(result).toEqual({}) + }) + + test('should create name filter with regex', () => { + const result = getFindParamsFilter('{"name":"Book Store"}') + expect(result).toEqual({ + name: { $regex: 'Book Store', $options: 'si' } + }) + }) + + test('should create ISO filter', () => { + const result = getFindParamsFilter('{"iso":"US"}') + expect(result).toEqual({ + iso: 'US' + }) + }) + + test('should create city filter with regex', () => { + const result = getFindParamsFilter('{"city":"New York"}') + expect(result).toEqual({ + city: { $regex: 'New York', $options: 'si' } + }) + }) + + test('should filter bookshops with website', () => { + const result = getFindParamsFilter('{"website":"with"}') + expect(result).toEqual({ + site: { $exists: true } + }) + }) + + test('should filter bookshops without website', () => { + const result = getFindParamsFilter('{"website":"without"}') + expect(result).toEqual({ + site: { $exists: false } + }) + }) + + test('should combine multiple search criteria', () => { + const result = getFindParamsFilter('{"name":"Book","city":"London","website":"with"}') + expect(result).toEqual({ + name: { $regex: 'Book', $options: 'si' }, + city: { $regex: 'London', $options: 'si' }, + site: { $exists: true } + }) + }) +}) diff --git a/src/api/bookshop/index.ts b/src/api/bookshop/index.ts index 3403d16..ba4999a 100644 --- a/src/api/bookshop/index.ts +++ b/src/api/bookshop/index.ts @@ -4,7 +4,7 @@ import type { ApiQuery } from 'types/api' import type { SearchBookshopsApi } from 'types/search' -const getFindParamsFilter = (search: string) => { +export const getFindParamsFilter = (search: string) => { const searchData = JSON.parse(search) const searchFilter: SearchBookshopsApi = {}