Skip to content

Commit 9de87bb

Browse files
authored
feat(utils): add warn function (#3147)
* feat(utils): add warn function * test(utils): use calledWith
1 parent a68cdaf commit 9de87bb

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/lib/__tests__/utils-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ describe('utils.deprecate', () => {
876876
const actual = 6;
877877

878878
expect(actual).toBe(expectation);
879-
expect(warn).toHaveBeenCalled();
879+
expect(warn).toHaveBeenCalledWith('[InstantSearch.js]: message');
880880

881881
warn.mockReset();
882882
warn.mockRestore();
@@ -899,6 +899,38 @@ describe('utils.deprecate', () => {
899899
});
900900
});
901901

902+
describe('utils.warn', () => {
903+
it('expect to print a warn message', () => {
904+
const message = 'message';
905+
const warn = jest.spyOn(global.console, 'warn');
906+
907+
utils.warn(message);
908+
909+
expect(warn).toHaveBeenCalledWith('[InstantSearch.js]: message');
910+
911+
warn.mockReset();
912+
warn.mockRestore();
913+
utils.warn.cache = {};
914+
});
915+
916+
it('expect to print the same message only once', () => {
917+
const message = 'message';
918+
const warn = jest.spyOn(global.console, 'warn');
919+
920+
utils.warn(message);
921+
922+
expect(warn).toHaveBeenCalledTimes(1);
923+
924+
utils.warn(message);
925+
926+
expect(warn).toHaveBeenCalledTimes(1);
927+
928+
warn.mockReset();
929+
warn.mockRestore();
930+
utils.warn.cache = {};
931+
});
932+
});
933+
902934
describe('utils.parseAroundLatLngFromString', () => {
903935
it('expect to return a LatLng object from string', () => {
904936
const samples = [

src/lib/utils.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
checkRendering,
2626
isReactElement,
2727
deprecate,
28+
warn,
2829
parseAroundLatLngFromString,
2930
};
3031

@@ -401,21 +402,36 @@ function isReactElement(object) {
401402
);
402403
}
403404

405+
function logger(message) {
406+
// eslint-disable-next-line no-console
407+
console.warn(`[InstantSearch.js]: ${message.trim()}`);
408+
}
409+
404410
function deprecate(fn, message) {
405411
let hasAlreadyPrint = false;
406412

407413
return function(...args) {
408414
if (!hasAlreadyPrint) {
409415
hasAlreadyPrint = true;
410416

411-
// eslint-disable-next-line no-console
412-
console.warn(`[InstantSearch.js]: ${message}`);
417+
logger(message);
413418
}
414419

415420
return fn(...args);
416421
};
417422
}
418423

424+
warn.cache = {};
425+
function warn(message) {
426+
const hasAlreadyPrint = warn.cache[message];
427+
428+
if (!hasAlreadyPrint) {
429+
warn.cache[message] = true;
430+
431+
logger(message);
432+
}
433+
}
434+
419435
const latLngRegExp = /^(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)$/;
420436
function parseAroundLatLngFromString(value) {
421437
const pattern = value.match(latLngRegExp);

0 commit comments

Comments
 (0)