-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add simple wrapper for useIntl
- Add simple wrapper for useIntl
- Loading branch information
Showing
5 changed files
with
81 additions
and
4 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,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`useIntl useIntl used underlying useIntl 1`] = ` | ||
<div> | ||
Hello, Eric! | ||
</div> | ||
`; | ||
|
||
exports[`useIntl with phrase enabled uses wrapped useIntl 1`] = ` | ||
<div> | ||
{{__phrase_app.greeting__}} | ||
</div> | ||
`; |
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,48 @@ | ||
import * as React from 'react'; | ||
import * as Utils from '../helper/test_utils'; | ||
import { injectIntl, useIntl } from '../src'; | ||
|
||
type MessageDescriptor = { | ||
id: string; | ||
defaultMessage?: string; | ||
description?: string | object; | ||
}; | ||
|
||
let ComponentUnderTest; | ||
const messages: Record<string, MessageDescriptor> = { | ||
greeting: { | ||
id: 'app.greeting', | ||
defaultMessage: 'Hello, {name}!', | ||
description: 'Greeting to welcome the user to the app', | ||
}, | ||
}; | ||
|
||
describe('useIntl', () => { | ||
beforeEach(() => { | ||
function Component() { | ||
const { formatMessage } = useIntl(); | ||
return (<div>{formatMessage(messages.greeting, { name: 'Eric' })}</div>); | ||
} | ||
ComponentUnderTest = injectIntl(Component); | ||
}); | ||
|
||
describe('useIntl', () => { | ||
it('used underlying useIntl', () => { | ||
const component = Utils.createComponentWithIntl(<ComponentUnderTest />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('with phrase enabled', () => { | ||
beforeEach(() => { | ||
Utils.setPhraseConfig(); | ||
}); | ||
|
||
it('uses wrapped useIntl', () => { | ||
const component = Utils.createComponentWithIntl(<ComponentUnderTest />); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { initializePhraseAppEditor } from './functions' | ||
export { injectIntl, ReactIntlPhraseProps } from './injectIntl' | ||
export { FormattedMessage } from './FormattedMessage' | ||
export { initializePhraseAppEditor } from './functions'; | ||
export { injectIntl, ReactIntlPhraseProps } from './injectIntl'; | ||
export { FormattedMessage } from './FormattedMessage'; | ||
export { useIntl } from './useIntl'; |
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,15 @@ | ||
import * as React from 'react'; | ||
import { IntlShape, useIntl as ReactUseIntl } from 'react-intl'; | ||
import { escapeId, isPhraseEnabled } from './functions'; | ||
|
||
export function useIntl(): IntlShape { | ||
if (isPhraseEnabled()) { | ||
const intl = ReactUseIntl(); | ||
function wrappedFormatMessage(...args) { | ||
return escapeId(args[0]?.id || ''); | ||
} | ||
return { ...intl, formatMessage: wrappedFormatMessage }; | ||
} else { | ||
return ReactUseIntl(); | ||
} | ||
}; |