From 6451dce02f3891d8aed08b76f09d8fb82d702d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Thu, 26 Sep 2019 08:49:45 -0700 Subject: [PATCH] fix(instantsearch): return instance in widgets methods (#4143) --- src/lib/InstantSearch.ts | 10 ++++-- src/lib/__tests__/InstantSearch-test.js | 46 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/lib/InstantSearch.ts b/src/lib/InstantSearch.ts index 06c1e552e7..1cca688d60 100644 --- a/src/lib/InstantSearch.ts +++ b/src/lib/InstantSearch.ts @@ -257,7 +257,8 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend false, 'addWidget will still be supported in 4.x releases, but not further. It is replaced by `addWidgets([widget])`' ); - this.addWidgets([widget]); + + return this.addWidgets([widget]); } /** @@ -289,6 +290,8 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend } this.mainIndex.addWidgets(widgets); + + return this; } /** @@ -303,7 +306,8 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend false, 'removeWidget will still be supported in 4.x releases, but not further. It is replaced by `removeWidgets([widget])`' ); - this.removeWidgets([widget]); + + return this.removeWidgets([widget]); } /** @@ -328,6 +332,8 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend } this.mainIndex.removeWidgets(widgets); + + return this; } /** diff --git a/src/lib/__tests__/InstantSearch-test.js b/src/lib/__tests__/InstantSearch-test.js index 463c35ed81..4b901c930a 100644 --- a/src/lib/__tests__/InstantSearch-test.js +++ b/src/lib/__tests__/InstantSearch-test.js @@ -271,6 +271,26 @@ describe('addWidget(s)', () => { expect(search.mainIndex.getWidgets()).toHaveLength(1); }); + + it('returns the search instance when calling `addWidget`', () => { + const searchClient = createSearchClient(); + const search = new InstantSearch({ + indexName: 'indexName', + searchClient, + }); + + expect(search.addWidget(createWidget())).toBe(search); + }); + + it('returns the search instance when calling `addWidgets`', () => { + const searchClient = createSearchClient(); + const search = new InstantSearch({ + indexName: 'indexName', + searchClient, + }); + + expect(search.addWidgets([createWidget()])).toBe(search); + }); }); describe('removeWidget(s)', () => { @@ -311,6 +331,32 @@ describe('removeWidget(s)', () => { expect(search.mainIndex.getWidgets()).toHaveLength(0); }); + + it('returns the search instance when calling `removeWidget`', () => { + const searchClient = createSearchClient(); + const search = new InstantSearch({ + indexName: 'indexName', + searchClient, + }); + + const widget = createWidget(); + search.addWidget(widget); + + expect(search.removeWidget(widget)).toBe(search); + }); + + it('returns the search instance when calling `removeWidgets`', () => { + const searchClient = createSearchClient(); + const search = new InstantSearch({ + indexName: 'indexName', + searchClient, + }); + + const widget = createWidget(); + search.addWidgets([widget]); + + expect(search.removeWidgets([widget])).toBe(search); + }); }); describe('start', () => {