diff --git a/src/connectors/breadcrumb/connectBreadcrumb.js b/src/connectors/breadcrumb/connectBreadcrumb.js index eea3850e15..6b869397a9 100644 --- a/src/connectors/breadcrumb/connectBreadcrumb.js +++ b/src/connectors/breadcrumb/connectBreadcrumb.js @@ -1,6 +1,6 @@ import find from 'lodash/find'; import isEqual from 'lodash/isEqual'; -import { checkRendering } from '../../lib/utils.js'; +import { checkRendering, warn } from '../../lib/utils.js'; const usage = `Usage: var customBreadcrumb = connectBreadcrumb(function renderFn(params, isFirstRendering) { @@ -82,9 +82,8 @@ export default function connectBreadcrumb(renderFn, unmountFn) { !isEqual(isFacetSet.attributes, attributes) || isFacetSet.separator !== separator ) { - // eslint-disable-next-line no-console - console.warn( - 'Using Breadcrumb & HierarchicalMenu on the same facet with different options. Adding that one will override the configuration of the HierarchicalMenu. Check your options.' + warn( + 'Using Breadcrumb and HierarchicalMenu on the same facet with different options overrides the configuration of the HierarchicalMenu.' ); } return {}; diff --git a/src/connectors/hierarchical-menu/connectHierarchicalMenu.js b/src/connectors/hierarchical-menu/connectHierarchicalMenu.js index b5e3582c14..f59a4e503c 100644 --- a/src/connectors/hierarchical-menu/connectHierarchicalMenu.js +++ b/src/connectors/hierarchical-menu/connectHierarchicalMenu.js @@ -1,7 +1,7 @@ import find from 'lodash/find'; import isEqual from 'lodash/isEqual'; -import { checkRendering } from '../../lib/utils.js'; +import { checkRendering, warn } from '../../lib/utils.js'; const usage = `Usage: var customHierarchicalMenu = connectHierarchicalMenu(function renderFn(params, isFirstRendering) { @@ -131,9 +131,8 @@ export default function connectHierarchicalMenu(renderFn, unmountFn) { isFacetSet.separator === separator ) ) { - // eslint-disable-next-line no-console - console.warn( - 'using Breadcrumb & HierarchicalMenu on the same facet with different options' + warn( + 'Using Breadcrumb and HierarchicalMenu on the same facet with different options overrides the configuration of the HierarchicalMenu.' ); return {}; } diff --git a/src/connectors/hits-per-page/connectHitsPerPage.js b/src/connectors/hits-per-page/connectHitsPerPage.js index 641b501e15..ade2f82417 100644 --- a/src/connectors/hits-per-page/connectHitsPerPage.js +++ b/src/connectors/hits-per-page/connectHitsPerPage.js @@ -1,7 +1,7 @@ import some from 'lodash/some'; import find from 'lodash/find'; -import { checkRendering } from '../../lib/utils.js'; +import { checkRendering, warn } from '../../lib/utils.js'; const usage = `Usage: var customHitsPerPage = connectHitsPerPage(function render(params, isFirstRendering) { @@ -145,20 +145,20 @@ The first one will be picked, you should probably set only one default value` if (!isCurrentInOptions) { if (state.hitsPerPage === undefined) { - if (window.console) { - window.console.warn( - `[Warning][hitsPerPage] hitsPerPage not defined. - You should probably set the value \`hitsPerPage\` - using the searchParameters attribute of the instantsearch constructor.` - ); - } - } else if (window.console) { - window.console.warn( - `[Warning][hitsPerPage] No item in \`items\` - with \`value: hitsPerPage\` (hitsPerPage: ${state.hitsPerPage})` + warn( + `\`hitsPerPage\` is not defined. +The option \`hitsPerPage\` needs to be set using the \`configure\` widget. + +Learn more: https://community.algolia.com/instantsearch.js/v2/widgets/configure.html` ); } + warn( + `No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: ${ + state.hitsPerPage + })` + ); + items = [{ value: '', label: '' }, ...items]; } diff --git a/src/lib/utils.js b/src/lib/utils.js index d92bba4ab9..5f39259db4 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -407,19 +407,19 @@ function isReactElement(object) { ); } -function logger(message) { +function log(message) { // eslint-disable-next-line no-console console.warn(`[InstantSearch.js]: ${message.trim()}`); } function deprecate(fn, message) { - let hasAlreadyPrint = false; + let hasAlreadyPrinted = false; return function(...args) { - if (!hasAlreadyPrint) { - hasAlreadyPrint = true; + if (!hasAlreadyPrinted) { + hasAlreadyPrinted = true; - logger(message); + log(message); } return fn(...args); @@ -428,12 +428,12 @@ function deprecate(fn, message) { warn.cache = {}; function warn(message) { - const hasAlreadyPrint = warn.cache[message]; + const hasAlreadyPrinted = warn.cache[message]; - if (!hasAlreadyPrint) { + if (!hasAlreadyPrinted) { warn.cache[message] = true; - logger(message); + log(message); } } 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 9d2d680a99..fe20deb566 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 @@ -115,9 +115,8 @@ describe('hitsPerPage()', () => { 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]).toEqual( - `[Warning][hitsPerPage] No item in \`items\` - with \`value: hitsPerPage\` (hitsPerPage: 20)` + expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot( + `"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: 20)"` ); }); @@ -125,9 +124,8 @@ describe('hitsPerPage()', () => { helper.state.hitsPerPage = -1; widget.init({ state: helper.state, helper }); expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once'); - expect(consoleWarn.mock.calls[0][0]).toEqual( - `[Warning][hitsPerPage] No item in \`items\` - with \`value: hitsPerPage\` (hitsPerPage: -1)` + expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot( + `"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: -1)"` ); }); diff --git a/src/widgets/pagination/__tests__/pagination-test.js b/src/widgets/pagination/__tests__/pagination-test.js index f41a108b01..91d9775f95 100644 --- a/src/widgets/pagination/__tests__/pagination-test.js +++ b/src/widgets/pagination/__tests__/pagination-test.js @@ -89,7 +89,9 @@ describe('pagination()', () => { widget = pagination({ container }); widget.init({ helper }); widget.render({ results, helper, state: { page: 0 } }); - const { props: { setCurrentPage } } = ReactDOM.render.mock.calls[0][0]; + const { + props: { setCurrentPage }, + } = ReactDOM.render.mock.calls[0][0]; setCurrentPage(2); expect(scrollIntoView).toHaveBeenCalledTimes(1); });