From 17d1930684b1cd008c5991523d8e7b3a816eeace Mon Sep 17 00:00:00 2001 From: Andre Christoga Pramaditya Date: Mon, 14 Mar 2022 16:46:22 +0800 Subject: [PATCH 1/2] feat(jest): 67% coverage for SearchIndex/index.ts --- libraries/SearchIndex/index.test.ts | 93 ++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/libraries/SearchIndex/index.test.ts b/libraries/SearchIndex/index.test.ts index c9b7863f34..4a839d2237 100644 --- a/libraries/SearchIndex/index.test.ts +++ b/libraries/SearchIndex/index.test.ts @@ -1,7 +1,8 @@ import SearchIndex from './index' +global.console.warn = jest.fn() describe('', () => { - test('', async () => { + test('update', async () => { const idx = new SearchIndex({ ref: 'id', fields: ['text'], @@ -21,4 +22,94 @@ 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', + ]) + // expect(idx.search('world')?.map((r) => r.ref)).toEqual(['2', '3']) + // expect(idx.search('hello world')?.map((r) => r.ref)).toEqual([ + // '3', + // '1', + // '2', + // ]) + }) }) From 534ae61df9287d1c87ac18d34b7891664c9848a9 Mon Sep 17 00:00:00 2001 From: Andre Christoga Pramaditya Date: Tue, 15 Mar 2022 14:26:38 +0700 Subject: [PATCH 2/2] update(jest): minor fixes --- libraries/SearchIndex/index.test.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/libraries/SearchIndex/index.test.ts b/libraries/SearchIndex/index.test.ts index 4a839d2237..23d1ddf572 100644 --- a/libraries/SearchIndex/index.test.ts +++ b/libraries/SearchIndex/index.test.ts @@ -1,5 +1,6 @@ import SearchIndex from './index' -global.console.warn = jest.fn() +// global.console.warn = jest.fn() +// TODO: Add coverage for the warning being logged describe('', () => { test('update', async () => { @@ -79,10 +80,10 @@ describe('', () => { idx.update(data) const observable = jest.fn() observable.subscribe = jest.fn() - // function callback(bool) { - // return bool - // } - // idx.subscribe(observable, callback) + /* function callback(bool) { + return bool + } + idx.subscribe(observable, callback) */ const callback = jest.fn() idx.subscribe(observable, callback) expect(observable.subscribe).toHaveBeenCalled() @@ -105,11 +106,5 @@ describe('', () => { '1', '3', ]) - // expect(idx.search('world')?.map((r) => r.ref)).toEqual(['2', '3']) - // expect(idx.search('hello world')?.map((r) => r.ref)).toEqual([ - // '3', - // '1', - // '2', - // ]) }) })