Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest): 67% coverage for SearchIndex/index.ts #2313

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
])
})
})