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 21, 2024
1 parent 949dc01 commit fdf3a30
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/@types/cozy-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ declare module 'cozy-client' {
}

export default class CozyClient {
plugins: any
constructor(rawOptions?: ClientOptions)
getStackClient(): StackClient
getInstanceOptions(): InstanceOptions
Expand Down
6 changes: 1 addition & 5 deletions src/dataproxy/worker/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import {
import { platformWorker } from '@/dataproxy/worker/platformWorker'
import schema from '@/doctypes'
import SearchEngine from '@/search/SearchEngine'
import {
FILES_DOCTYPE,
CONTACTS_DOCTYPE,
APPS_DOCTYPE
} from '@/search/consts'
import { FILES_DOCTYPE, CONTACTS_DOCTYPE, APPS_DOCTYPE } from '@/search/consts'

const log = Minilog('👷‍♂️ [shared-worker]')
Minilog.enable()
Expand Down
7 changes: 3 additions & 4 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 Expand Up @@ -162,7 +161,7 @@ class SearchEngine {
}

// Incremental index update
// At this point, the search index are supposed to be already up-to-date,
// At this point, the search index are supposed to be already up-to-date,
// thanks to the realtime.
// However, we check it is actually the case for safety, and update the lastSeq
const lastSeq = searchIndex.lastSeq || 0
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
}
53 changes: 53 additions & 0 deletions src/search/helpers/replication.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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()
}
;(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 fdf3a30

Please sign in to comment.