Skip to content

Commit

Permalink
fix(utils): warn only in development environment (#3367)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored Jan 15, 2019
1 parent 916b422 commit 0e33c76
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 115 deletions.
15 changes: 6 additions & 9 deletions src/connectors/breadcrumb/connectBreadcrumb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import find from 'lodash/find';
import isEqual from 'lodash/isEqual';
import { checkRendering, warn } from '../../lib/utils.js';
import { checkRendering, warning } from '../../lib/utils.js';

const usage = `Usage:
var customBreadcrumb = connectBreadcrumb(function renderFn(params, isFirstRendering) {
Expand Down Expand Up @@ -78,14 +78,11 @@ export default function connectBreadcrumb(renderFn, unmountFn) {
({ name }) => name === hierarchicalFacetName
);
if (isFacetSet) {
if (
!isEqual(isFacetSet.attributes, attributes) ||
isFacetSet.separator !== separator
) {
warn(
'Using Breadcrumb and HierarchicalMenu on the same facet with different options overrides the configuration of the HierarchicalMenu.'
);
}
warning(
isEqual(isFacetSet.attributes, attributes) &&
isFacetSet.separator === separator,
'Using Breadcrumb and HierarchicalMenu on the same facet with different options overrides the configuration of the HierarchicalMenu.'
);
return {};
}
}
Expand Down
40 changes: 24 additions & 16 deletions src/connectors/geo-search/connectGeoSearch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import noop from 'lodash/noop';
import {
checkRendering,
warn,
warning,
aroundLatLngToPosition,
insideBoundingBoxToBoundingBox,
} from '../../lib/utils';
Expand Down Expand Up @@ -138,7 +138,9 @@ const connectGeoSearch = (renderFn, unmountFn) => {
// Always trigger this message because the default value was `true`. We can't
// display the message only when the parameter is defined otherwise a user that was
// relying on the default value won't have any information about the changes.
warn(`
warning(
false,
`
The option \`enableGeolocationWithIP\` has been removed from the GeoSearch widget.
Please consider using the \`Configure\` widget instead:
Expand All @@ -150,26 +152,31 @@ search.addWidget(
You can find more information inside the migration guide:
http://community.algolia.com/instantsearch.js/migration-guide
`);
`
);

if (typeof widgetParams.position !== 'undefined') {
warn(`
warning(
typeof widgetParams.position === 'undefined',
`
The option \`position\` has been removed from the GeoSearch widget.
Please consider using the \`Configure\` widget instead:
search.addWidget(
configure({
aroundLatLng: '${widgetParams.position.lat}, ${widgetParams.position.lng}',
aroundLatLng: '${widgetParams.position &&
widgetParams.position.lat}, ${widgetParams.position &&
widgetParams.position.lng}',
})
);
You can find more information inside the migration guide:
http://community.algolia.com/instantsearch.js/migration-guide
`);
}
`
);

if (typeof widgetParams.radius !== 'undefined') {
warn(`
warning(
typeof widgetParams.radius === 'undefined',
`
The option \`radius\` has been removed from the GeoSearch widget.
Please consider using the \`Configure\` widget instead:
Expand All @@ -182,11 +189,12 @@ search.addWidget(
You can find more information inside the migration guide:
http://community.algolia.com/instantsearch.js/migration-guide
`);
}
`
);

if (typeof widgetParams.precision !== 'undefined') {
warn(`
warning(
typeof widgetParams.precision === 'undefined',
`
The option \`precision\` has been removed from the GeoSearch widget.
Please consider using the \`Configure\` widget instead:
Expand All @@ -199,8 +207,8 @@ search.addWidget(
You can find more information inside the migration guide:
http://community.algolia.com/instantsearch.js/migration-guide
`);
}
`
);

const widgetState = {
isRefineOnMapMove: enableRefineOnMapMove,
Expand Down
6 changes: 4 additions & 2 deletions src/connectors/hierarchical-menu/connectHierarchicalMenu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import find from 'lodash/find';
import isEqual from 'lodash/isEqual';
import { checkRendering, warn } from '../../lib/utils.js';
import { checkRendering, warning } from '../../lib/utils.js';

const usage = `Usage:
var customHierarchicalMenu = connectHierarchicalMenu(function renderFn(params, isFirstRendering) {
Expand Down Expand Up @@ -139,7 +139,9 @@ export default function connectHierarchicalMenu(renderFn, unmountFn) {
isFacetSet.separator === separator
)
) {
warn(
warning(
isEqual(isFacetSet.attributes, attributes) &&
isFacetSet.separator === separator,
'Using Breadcrumb and HierarchicalMenu on the same facet with different options overrides the configuration of the HierarchicalMenu.'
);
return {};
Expand Down
18 changes: 10 additions & 8 deletions src/connectors/hits-per-page/connectHitsPerPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import some from 'lodash/some';
import find from 'lodash/find';

import { checkRendering, warn } from '../../lib/utils.js';
import { checkRendering, warning } from '../../lib/utils.js';

const usage = `Usage:
var customHitsPerPage = connectHitsPerPage(function render(params, isFirstRendering) {
Expand Down Expand Up @@ -146,16 +146,18 @@ The first one will be picked, you should probably set only one default value`
);

if (!isCurrentInOptions) {
if (state.hitsPerPage === undefined) {
warn(
`\`hitsPerPage\` is not defined.
warning(
state.hitsPerPage !== undefined,
`
\`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`
);
}
Learn more: https://community.algolia.com/instantsearch.js/v2/widgets/configure.html
`
);

warn(
warning(
false,
`No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: ${
state.hitsPerPage
})`
Expand Down
36 changes: 19 additions & 17 deletions src/lib/__tests__/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,35 +936,37 @@ describe('utils.deprecate', () => {
});
});

describe('utils.warn', () => {
it('expect to print a warn message', () => {
const message = 'message';
const warn = jest.spyOn(global.console, 'warn');

utils.warn(message);
describe('utils.warning', () => {
let warn;

expect(warn).toHaveBeenCalledWith('[InstantSearch.js]: message');
beforeEach(() => {
warn = jest.spyOn(global.console, 'warn');
});

afterEach(() => {
warn.mockReset();
warn.mockRestore();
utils.warn.cache = {};
utils.warning.cache = {};
});

it('expect to print the same message only once', () => {
const message = 'message';
const warn = jest.spyOn(global.console, 'warn');
it('prints a warning message with a false condition', () => {
utils.warning(false, 'message');

utils.warn(message);
expect(warn).toHaveBeenCalledWith('[InstantSearch.js]: message');
});

expect(warn).toHaveBeenCalledTimes(1);
it('does not print a warning message with a true condition', () => {
utils.warning(true, 'message');

utils.warn(message);
expect(warn).toHaveBeenCalledTimes(0);
});

it('prints the same warning message only once', () => {
utils.warning(false, 'message');
expect(warn).toHaveBeenCalledTimes(1);

warn.mockReset();
warn.mockRestore();
utils.warn.cache = {};
utils.warning(false, 'message');
expect(warn).toHaveBeenCalledTimes(1);
});
});

Expand Down
131 changes: 74 additions & 57 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,6 @@ import mapValues from 'lodash/mapValues';
import curry from 'lodash/curry';
import hogan from 'hogan.js';

export {
capitalize,
getContainerNode,
bemHelper,
prepareTemplateProps,
renderTemplate,
isSpecialClick,
isDomElement,
getRefinements,
clearRefinements,
prefixKeys,
escapeRefinement,
unescapeRefinement,
checkRendering,
isReactElement,
deprecate,
warn,
aroundLatLngToPosition,
insideBoundingBoxToBoundingBox,
};

function capitalize(string) {
return (
string
Expand Down Expand Up @@ -387,36 +366,6 @@ function isReactElement(object) {
);
}

function log(message) {
// eslint-disable-next-line no-console
console.warn(`[InstantSearch.js]: ${message.trim()}`);
}

function deprecate(fn, message) {
let hasAlreadyPrinted = false;

return function(...args) {
if (!hasAlreadyPrinted) {
hasAlreadyPrinted = true;

log(message);
}

return fn(...args);
};
}

warn.cache = {};
function warn(message) {
const hasAlreadyPrinted = warn.cache[message];

if (!hasAlreadyPrinted) {
warn.cache[message] = true;

log(message);
}
}

const latLngRegExp = /^(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)$/;
function aroundLatLngToPosition(value) {
const pattern = value.match(latLngRegExp);
Expand All @@ -433,12 +382,6 @@ function aroundLatLngToPosition(value) {
};
}

export function getPropertyByPath(object, path) {
const parts = path.split('.');

return parts.reduce((current, key) => current && current[key], object);
}

function insideBoundingBoxArrayToBoundingBox(value) {
const [[neLat, neLng, swLat, swLng] = []] = value;

Expand Down Expand Up @@ -492,3 +435,77 @@ function insideBoundingBoxToBoundingBox(value) {

return insideBoundingBoxStringToBoundingBox(value);
}

function getPropertyByPath(object, path) {
const parts = path.split('.');

return parts.reduce((current, key) => current && current[key], object);
}

let deprecate = () => {};

/**
* Logs a warning if the condition is not met.
* This is used to log issues in development environment only.
*
* @return {undefined}
*/
let warning = () => {};

if (__DEV__) {
const warn = message => {
// eslint-disable-next-line no-console
console.warn(`[InstantSearch.js]: ${message.trim()}`);
};

deprecate = (fn, message) => {
let hasAlreadyPrinted = false;

return function(...args) {
if (!hasAlreadyPrinted) {
hasAlreadyPrinted = true;

warn(message);
}

return fn(...args);
};
};

warning = (condition, message) => {
if (condition) {
return;
}

const hasAlreadyPrinted = warning.cache[message];

if (!hasAlreadyPrinted) {
warning.cache[message] = true;

warn(message);
}
};
warning.cache = {};
}

export {
capitalize,
getContainerNode,
bemHelper,
prepareTemplateProps,
renderTemplate,
isSpecialClick,
isDomElement,
getRefinements,
clearRefinements,
aroundLatLngToPosition,
insideBoundingBoxToBoundingBox,
getPropertyByPath,
prefixKeys,
escapeRefinement,
unescapeRefinement,
checkRendering,
isReactElement,
deprecate,
warning,
};
Loading

0 comments on commit 0e33c76

Please sign in to comment.