Skip to content

Commit

Permalink
feat(jest): coverage for SearchIndex/index.ts (#2313)
Browse files Browse the repository at this point in the history
  • Loading branch information
drepram committed Mar 17, 2022
1 parent 16f5d5a commit 0c185d7
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion libraries/SearchIndex/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import SearchIndex from './index'
// global.console.warn = jest.fn()
// TODO: Add coverage for the warning being logged
import * as index from '~/libraries/SearchIndex/index'

describe('index.default.serialize', () => {
Expand Down Expand Up @@ -393,7 +395,7 @@ describe('index.default.unsubscribe', () => {
})

describe('', () => {
test('', async () => {
test('update', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
Expand All @@ -413,4 +415,88 @@ describe('', () => {
'2',
])
})
test('serialize', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
})
const data = [
{ id: '1', text: 'hello' },
{ id: '2', text: 'world' },
{ id: '3', text: 'hello world' },
]
expect(JSON.parse(idx.serialize())).toMatchObject({
schema: { fields: ['text'], ref: 'id' },
})
})
test('serialize', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
})
const data = [
{ id: '1', text: 'hello' },
{ id: '2', text: 'world' },
{ id: '3', text: 'hello world' },
]
const serializedData = idx.serialize()
expect(SearchIndex.deserialize(serializedData)).toMatchObject({
schema: { fields: ['text'], ref: 'id' },
})
})
test('unsubscribe', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
})
const data = [
{ id: '1', text: 'hello' },
{ id: '2', text: 'world' },
{ id: '3', text: 'hello world' },
]
const observable = jest.fn()
observable.unsubscribe = jest.fn()
idx.unsubscribe(observable)
expect(observable.unsubscribe).toHaveBeenCalled()
})
test.skip('subscribe', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
})
const data = [
{ id: '1', text: 'hello' },
{ id: '2', text: 'world' },
{ id: '3', text: 'hello world' },
]
idx.update(data)
const observable = jest.fn()
observable.subscribe = jest.fn()
/* function callback(bool) {
return bool
}
idx.subscribe(observable, callback) */
const callback = jest.fn()
idx.subscribe(observable, callback)
expect(observable.subscribe).toHaveBeenCalled()
expect(callback).toHaveBeenCalled()
})
test.skip('search but invalid search query', async () => {
const idx = new SearchIndex({
ref: 'id',
fields: ['text'],
})
const data = [
{ id: '1', text: 'hello' },
{ id: '2', text: 'world' },
{ id: '3', text: 'hello world' },
]
idx.update(data)

expect(console.warn).toHaveBeenCalled()
expect(idx.search('$%@*&*#&@*', true)?.map((r) => r.ref)).toEqual([
'1',
'3',
])
})
})

0 comments on commit 0c185d7

Please sign in to comment.