-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: avoid wrapping components with injectIntl #1413
Conversation
@@ -34,15 +34,15 @@ | |||
"module": "./lib/index.js", | |||
"types": "./dist/index.d.ts", | |||
"dependencies": { | |||
"@formatjs/intl-relativetimeformat": "^2.6.2", | |||
"@formatjs/intl-relativetimeformat": "^2.6.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I included those deps updates as I saw they were freshly published. Let me know if it's ok, otherwise I can revert.
return <Component dangerouslySetInnerHTML={html} />; | ||
return ( | ||
<Context.Consumer> | ||
{intl => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically everything that we need from the injectIntl
import {formatMessage as baseFormatMessage} from '../format'; | ||
import { | ||
invariantIntlContext, | ||
DEFAULT_INTL_CONFIG, | ||
createFormatters, | ||
} from '../utils'; | ||
import {PrimitiveType, FormatXMLElementFn} from 'intl-messageformat'; | ||
const shallowEquals = require('shallow-equal/objects'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just reordering a bit the imports
} = this.props; | ||
return ( | ||
<Context.Consumer> | ||
{intl => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here
} | ||
return ( | ||
<Context.Consumer> | ||
{intl => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also for this one, no need to have a HOC here
test/unit/components/date.tsx
Outdated
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Error formatting date.\nRangeError: Value invalid out of range for dateformat options property year' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added more expectations to check for the error message, as it gives a better understanding of the expected behavior.
1d31e4e
to
38f9a34
Compare
test/unit/components/provider.tsx
Outdated
import withIntl, {Provider} from '../../../src/components/injectIntl'; | ||
import withIntl from '../../../src/components/injectIntl'; | ||
|
||
jest.mock('intl-locales-supported'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary otherwise the test fails because the intl polyfill (for the tests) returns false
for some of the intl contructors as they don't support languages other than en
.
overall LGTM! Ready to merge once build if fixed. Thanks a lot for your contributions! |
38f9a34
to
2ba78f2
Compare
Awesome, glad to help! |
Hmm wait, if I run
I've been using |
I guess that might explain the problem I had with #1413 (comment) |
Ah ok, I had to |
2ba78f2
to
d48aff7
Compare
Alright, I force pushed and removed the |
@@ -112,7 +112,7 @@ | |||
"release": "standard-version", | |||
"test:all": "npm run lint && npm run format && npm run test", | |||
"test:perf": "cross-env NODE_ENV=production babel-node test/perf", | |||
"test:watch": "jest --watch", | |||
"test:watch": "cross-env NODE_ICU_DATA=./node_modules/full-icu jest --watch", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what tricked me. Now the polyfilled data is loaded also when running jest in watch mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then u prob don't need to mock nvm u rm'ed it. Thanks!intl-locales-supported
right?
Ah Node 12 fails bc it comes w/ native |
The reason why injectIntl was still being used is because it allows for more flexibility in the SCU. |
@DragonRaider5 can u provide more details? I'd love to understand that use case more. |
The only place where we currently use sCU is the Unless I'm missing some other point... |
64ef553
to
72e3af8
Compare
Fixes #1412
TL;DR: I'm not aware of a way to "forward" the types to the wrapped components when using a HOC. To keep things simple, and avoid using the HOC in the first place, we can simply use the context consumer directly.
I also refactored a bit the tests to use
console.error = jest.fn()
, which is the easiest way to mock the log.