Skip to content

Commit

Permalink
feat: Add simple wrapper for useIntl
Browse files Browse the repository at this point in the history
- Add simple wrapper for useIntl
  • Loading branch information
Varpuspaavi authored Sep 15, 2022
2 parents 44b6e9c + c413888 commit d99896c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
13 changes: 13 additions & 0 deletions __tests__/__snapshots__/useIntl.tsx.snap
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>
`;
48 changes: 48 additions & 0 deletions __tests__/useIntl.tsx
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();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-intl-phraseapp",
"version": "3.0.2",
"version": "3.1.0",
"description": "The In-Context-Editor for react using react-intl",
"main": "dist/react-intl-phraseapp.js",
"typings": "./dist/index.d.ts",
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
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';
15 changes: 15 additions & 0 deletions src/useIntl.tsx
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();
}
};

0 comments on commit d99896c

Please sign in to comment.