diff --git a/libraries/SearchIndex/index.test.ts b/libraries/SearchIndex/index.test.ts index cd17a59fbd..30897a5a59 100644 --- a/libraries/SearchIndex/index.test.ts +++ b/libraries/SearchIndex/index.test.ts @@ -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', () => { @@ -393,7 +395,7 @@ describe('index.default.unsubscribe', () => { }) describe('', () => { - test('', async () => { + test('update', async () => { const idx = new SearchIndex({ ref: 'id', fields: ['text'], @@ -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', + ]) + }) })