Skip to content

Commit

Permalink
Added extra unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel committed Feb 23, 2025
1 parent 6c8760a commit b2f3636
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/api/bookshop/index.test.ts
Original file line number Diff line number Diff line change
@@ -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 }
})
})
})
2 changes: 1 addition & 1 deletion src/api/bookshop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down

0 comments on commit b2f3636

Please sign in to comment.