From a61a0942dccf7010862095abfeb67df0dd2bfb3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Sun, 3 Mar 2019 19:37:13 +0100 Subject: [PATCH] test: use toWarnDev Jest matcher --- .../__tests__/connectBreadcrumb-test.js | 9 +++---- .../__tests__/connectHierarchicalMenu-test.js | 9 +++---- .../__tests__/hits-per-page-test.js | 27 ++++++++----------- src/widgets/panel/__tests__/panel-test.js | 15 ++++------- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js b/src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js index 4560c7f98d1..d47d22a09ad 100644 --- a/src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js +++ b/src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js @@ -60,16 +60,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/breadcrumb/ } // when there is an identical hierarchicalFacets into current configuration - { - const spy = jest.spyOn(global.console, 'warn'); + expect(() => { const config = widget.getConfiguration({ hierarchicalFacets: [{ name: 'category' }], }); + expect(config).toEqual({}); - expect(spy).toHaveBeenCalled(); - spy.mockReset(); - spy.mockRestore(); - } + }).toWarnDev(); // when there is already a different hierarchicalFacets into current configuration { diff --git a/src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js b/src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js index 366d176ac17..f16a4b80708 100644 --- a/src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js +++ b/src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js @@ -72,16 +72,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica } // when there is an identical hierarchicalFacets into current configuration - { - const spy = jest.spyOn(global.console, 'warn'); + expect(() => { const config = widget.getConfiguration({ hierarchicalFacets: [{ name: 'category' }], }); + expect(config).toEqual({}); - expect(spy).toHaveBeenCalled(); - spy.mockReset(); - spy.mockRestore(); - } + }).toWarnDev(); // when there is already a different hierarchicalFacets into current configuration { diff --git a/src/widgets/hits-per-page/__tests__/hits-per-page-test.js b/src/widgets/hits-per-page/__tests__/hits-per-page-test.js index 872414cb9f3..25ed1e37c52 100644 --- a/src/widgets/hits-per-page/__tests__/hits-per-page-test.js +++ b/src/widgets/hits-per-page/__tests__/hits-per-page-test.js @@ -28,12 +28,9 @@ describe('hitsPerPage()', () => { let widget; let helper; let results; - let consoleWarn; let state; beforeEach(() => { - consoleWarn = jest.spyOn(window.console, 'warn'); - container = document.createElement('div'); items = [ { value: 10, label: '10 results' }, @@ -117,22 +114,24 @@ describe('hitsPerPage()', () => { expect(helper.search).toHaveBeenCalledTimes(1, 'search called once'); }); - it('should throw if there is no name attribute in a passed object', () => { + it('should warn without name attribute in a passed item', () => { items.length = 0; items.push({ label: 'Label without a value' }); - widget.init({ state: helper.state, helper }); - expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once'); - expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot( - `"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: 20)"` + + expect(() => { + widget.init({ state: helper.state, helper }); + }).toWarnDev( + '[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: 20)' ); }); it('must include the current hitsPerPage at initialization time', () => { helper.state.hitsPerPage = -1; - widget.init({ state: helper.state, helper }); - expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once'); - expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot( - `"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: -1)"` + + expect(() => { + widget.init({ state: helper.state, helper }); + }).toWarnDev( + '[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: -1)' ); }); @@ -142,8 +141,4 @@ describe('hitsPerPage()', () => { widget.init({ state: helper.state, helper }); }).not.toThrow(/No item in `items`/); }); - - afterEach(() => { - consoleWarn.mockRestore(); - }); }); diff --git a/src/widgets/panel/__tests__/panel-test.js b/src/widgets/panel/__tests__/panel-test.js index ac482507388..41765d22628 100644 --- a/src/widgets/panel/__tests__/panel-test.js +++ b/src/widgets/panel/__tests__/panel-test.js @@ -22,18 +22,13 @@ describe('Usage', () => { }); test('with `hidden` as boolean warns', () => { - const warn = jest.spyOn(global.console, 'warn'); - warn.mockImplementation(() => {}); - - panel({ - hidden: true, - }); - - expect(warn).toHaveBeenCalledWith( + expect(() => { + panel({ + hidden: true, + }); + }).toWarnDev( '[InstantSearch.js]: The `hidden` option in the "panel" widget expects a function returning a boolean (received "boolean" type).' ); - - warn.mockRestore(); }); test('with a widget without `container` throws', () => {