-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add warning helper (#8021)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
59bfae0
commit 9f1287c
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { warning } from '../warning'; | ||
|
||
describe('warning', () => { | ||
test('calls console.warn() when the condition is false', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
warning(false, 'The message'); | ||
|
||
expect(spy).toHaveBeenCalledWith('Warning: The message'); | ||
spy.mockRestore(); | ||
}); | ||
|
||
test('does not call console.warn() when the condition is true', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
||
warning(true, 'The message'); | ||
|
||
expect(spy).not.toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
|
||
test('throws an error when no message is provided', () => { | ||
expect(() => { | ||
warning(true); | ||
}).toThrow(); | ||
}); | ||
|
||
test('substitutes extra arguments in the message', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
||
warning(false, '%s %s %s', 'a', 'b', 'c'); | ||
|
||
expect(spy).toHaveBeenCalledWith('Warning: a b c'); | ||
spy.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const emptyFunction = function () {}; | ||
|
||
const warning = __DEV__ | ||
? function warning(condition, format, ...args) { | ||
if (format === undefined) { | ||
throw new Error( | ||
'`warning(condition, format, ...args)` requires a warning ' + | ||
'format argument' | ||
); | ||
} | ||
if (!condition) { | ||
let index = 0; | ||
const message = format.replace(/%s/g, () => { | ||
return args[index++]; | ||
}); | ||
|
||
console.warn('Warning: ' + message); | ||
} | ||
} | ||
: emptyFunction; | ||
|
||
export { warning }; |