This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8050cc
commit 54d2068
Showing
10 changed files
with
170 additions
and
6 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
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,62 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import React, { Component } from 'react'; | ||
import * as R from 'ramda'; | ||
|
||
// Only want to warn once per component | ||
const hasWarned = {}; | ||
// Usage: deprecatedComponent('Pane', 'Use Tabs instead')(Component); | ||
const deprecatedComponent = ( | ||
displayName, | ||
extraMessage = 'Please consult the docs for a migration guide.', | ||
) => C => { | ||
class Deprecated extends Component { | ||
componentDidMount() { | ||
if (!hasWarned[displayName]) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`${displayName} is deprecated. ${extraMessage}`); | ||
hasWarned[displayName] = true; | ||
} | ||
} | ||
|
||
render() { | ||
return <C {...this.props} />; | ||
} | ||
} | ||
return Deprecated; | ||
}; | ||
|
||
// Only want to warn once per component and prop | ||
const hasWarnedProps = {}; | ||
|
||
/* Usage: | ||
deprecatedProps('Flag', [{ | ||
prop: 'size', | ||
message: 'size in Flag as a number is deprecated. Use sm, md or lg instead', | ||
}])(Flag) | ||
*/ | ||
const deprecatedProps = (displayName, deprecated) => C => { | ||
class Deprecated extends Component { | ||
componentDidMount() { | ||
if (!hasWarnedProps[displayName]) hasWarnedProps[displayName] = {}; | ||
R.intersection(Object.keys(this.props), deprecated.map(d => d.prop)).forEach(p => { | ||
if (!hasWarnedProps[displayName][p]) { | ||
// eslint-disable-next-line no-console | ||
console.warn((deprecated.find(d1 => d1.prop === p) || {}).message); | ||
hasWarnedProps[displayName][p] = true; | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
return <C {...this.props} />; | ||
} | ||
} | ||
return Deprecated; | ||
}; | ||
|
||
const isNotProd = process.env.NODE_ENV !== 'production'; | ||
|
||
const dc = isNotProd ? deprecatedComponent : C => C; | ||
const dp = isNotProd ? deprecatedProps : C => C; | ||
|
||
export { dc as deprecatedComponent, dp as deprecatedProps }; |
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,54 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { deprecatedProps, deprecatedComponent } from './deprecate'; | ||
|
||
let warnMock; | ||
beforeEach(() => { | ||
warnMock = jest.fn(); | ||
global.console.warn = warnMock; | ||
}); | ||
|
||
const DEFAULT_MESSAGE = 'Please consult the docs for a migration guide.'; | ||
|
||
test('deprecatedComponent', () => { | ||
const Component = () => <div />; | ||
const DC = deprecatedComponent('Flag')(Component); | ||
shallow(<DC />); | ||
expect(warnMock).toHaveBeenCalled(); | ||
expect(warnMock.mock.calls[0][0]).toMatch('Flag is deprecated.'); | ||
expect(warnMock.mock.calls[0][0]).toMatch(DEFAULT_MESSAGE); | ||
}); | ||
|
||
test('deprecatedComponent with custom message', () => { | ||
const Component = () => <div />; | ||
const msg = 'Use the new Flag component instead.'; | ||
const DC = deprecatedComponent('Flag2', msg)(Component); | ||
shallow(<DC />); | ||
expect(warnMock).toHaveBeenCalled(); | ||
expect(warnMock.mock.calls[0][0]).toMatch('Flag2 is deprecated.'); | ||
expect(warnMock.mock.calls[0][0]).toMatch(msg); | ||
}); | ||
|
||
test('deprecatedComponent should only warn once', () => { | ||
const Component = () => <div />; | ||
const DC = deprecatedComponent('Flag3')(Component); | ||
shallow(<DC />); | ||
expect(warnMock).toHaveBeenCalled(); | ||
shallow(<DC />); | ||
expect(warnMock).toHaveBeenCalledTimes(1); | ||
expect(warnMock.mock.calls[0][0]).toMatch('Flag3 is deprecated.'); | ||
}); | ||
|
||
test('deprecatedProps', () => { | ||
const Component = () => <div />; | ||
const DC = deprecatedProps('Flag', [ | ||
{ | ||
prop: 'size', | ||
message: 'size in Flag as a number is deprecated. Use sm, md or lg instead', | ||
}, | ||
])(Component); | ||
shallow(<DC size={16} />); | ||
expect(warnMock).toHaveBeenCalled(); | ||
expect(warnMock.mock.calls[0][0]).toMatch('Use sm, md or lg instead'); | ||
}); |
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,3 @@ | ||
import { deprecatedComponent, deprecatedProps } from './deprecate'; | ||
|
||
export { deprecatedComponent, deprecatedProps }; |
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
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
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
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
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
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