Skip to content

Commit

Permalink
feat: Add test with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ldoppea authored and paultranvan committed Oct 18, 2024
1 parent 4e975be commit 328c8ef
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import FlexSearch from 'flexsearch'
// @ts-ignore
import { encode as encode_balance } from 'flexsearch/dist/module/lang/latin/balance'

import CozyClient, { Q } from 'cozy-client'
import Minilog from 'cozy-minilog'
Expand All @@ -16,6 +14,7 @@ import {
REPLICATION_DEBOUNCE
} from '@/search/consts'
import { getPouchLink } from '@/search/helpers/client'
import { getSearchEncoder } from '@/search/helpers/getSearchEncoder'
import { normalizeSearchResult } from '@/search/helpers/normalizeSearchResult'
import { startReplicationWithDebounce } from '@/search/helpers/replication'
import {
Expand Down Expand Up @@ -124,7 +123,7 @@ class SearchEngine {

const flexsearchIndex = new FlexSearch.Document<CozyDoc, true>({
tokenize: 'forward',
encode: encode_balance as FlexSearch.Encoders,
encode: getSearchEncoder(),
minlength: 2,
document: {
id: '_id',
Expand Down
7 changes: 7 additions & 0 deletions src/search/helpers/getSearchEncoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import FlexSearch from 'flexsearch'
// @ts-ignore
import { encode as encode_balance } from 'flexsearch/dist/module/lang/latin/balance'

export const getSearchEncoder = (): FlexSearch.Encoders => {
return encode_balance as FlexSearch.Encoders
}
56 changes: 56 additions & 0 deletions src/search/helpers/replication.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import CozyClient from 'cozy-client'

import { getPouchLink } from '@/search/helpers/client'

import { startReplicationWithDebounce } from './replication'

jest.mock('cozy-client')
jest.mock('@/search/helpers/client', () => ({
getPouchLink: jest.fn()
}))

describe('startReplicationWithDebounce', () => {
let client: CozyClient
let pouchLink: any

beforeEach(() => {
client = new CozyClient()
pouchLink = {
startReplication: jest.fn()
}
log = {
debug: jest.fn()
}
;(getPouchLink as jest.Mock).mockReturnValue(pouchLink)
jest.useFakeTimers()
})

afterEach(() => {
jest.clearAllMocks()
jest.clearAllTimers()
})

it('should start replication after the specified interval', () => {
const interval = 1000
const replicate = startReplicationWithDebounce(client, interval)

replicate()
expect(pouchLink.startReplication).not.toHaveBeenCalled()
jest.advanceTimersByTime(interval)
expect(pouchLink.startReplication).toHaveBeenCalledTimes(1)
})

it('should debounce replication calls within the interval', () => {
const interval = 1000
const replicate = startReplicationWithDebounce(client, interval)

replicate()
jest.advanceTimersByTime(interval / 2)
expect(pouchLink.startReplication).not.toHaveBeenCalled()
replicate()
replicate()

jest.advanceTimersByTime(interval)
expect(pouchLink.startReplication).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 328c8ef

Please sign in to comment.