-
Notifications
You must be signed in to change notification settings - Fork 2
feat: shim injectIntl rather than expecting it from frontend-base #7
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| /* Coverage for the `injectIntl` shim: frontend-base no longer ships one, so | ||
| * unlike the rest of `./i18n` this is our own implementation. */ | ||
| import { Component, createRef, forwardRef, memo } from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { IntlProvider } from '@openedx/frontend-base'; | ||
|
|
||
| import { injectIntl, WrappedComponentProps } from './i18n'; | ||
|
|
||
| const messages = { greeting: 'Hello, {name}!' }; | ||
|
|
||
| function renderWithIntl(ui: React.ReactElement) { | ||
| return render( | ||
| <IntlProvider locale="en" messages={messages}>{ui}</IntlProvider>, | ||
| ); | ||
| } | ||
|
|
||
| describe('injectIntl', () => { | ||
| it('passes an `intl` prop to a function component alongside its own props', () => { | ||
| function Greeting({ intl, name }: WrappedComponentProps & { name: string }) { | ||
| return <p>{intl.formatMessage({ id: 'greeting', description: 'Greets the user by name.' }, { name })}</p>; | ||
| } | ||
| const Wrapped = injectIntl(Greeting); | ||
|
|
||
| renderWithIntl(<Wrapped name="Ada" />); | ||
|
|
||
| expect(screen.getByText('Hello, Ada!')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('forwards refs to a class component instance', () => { | ||
| class Counter extends Component<WrappedComponentProps> { | ||
| locale() { | ||
| return this.props.intl.locale; | ||
| } | ||
|
|
||
| render() { | ||
| return <p>counter</p>; | ||
| } | ||
| } | ||
| const Wrapped = injectIntl(Counter); | ||
| const ref = createRef<Counter>(); | ||
|
|
||
| renderWithIntl(<Wrapped ref={ref} />); | ||
|
|
||
| expect(ref.current).toBeInstanceOf(Counter); | ||
| expect(ref.current?.locale()).toEqual('en'); | ||
| }); | ||
|
|
||
| it('forwards refs through a forwardRef component to the DOM node', () => { | ||
| const Input = forwardRef<HTMLInputElement, WrappedComponentProps>( | ||
| function Input({ intl }, ref) { | ||
| return <input ref={ref} aria-label={intl.locale} />; | ||
| }, | ||
| ); | ||
| const Wrapped = injectIntl(Input); | ||
| const ref = createRef<HTMLInputElement>(); | ||
|
|
||
| renderWithIntl(<Wrapped ref={ref} />); | ||
|
|
||
| expect(ref.current).toBe(screen.getByLabelText('en')); | ||
| }); | ||
|
|
||
| it('keeps a memoized component memoized', () => { | ||
| let renders = 0; | ||
| const Memoized = memo(function Memoized({ intl }: WrappedComponentProps) { | ||
| renders += 1; | ||
| return <p>{intl.locale}</p>; | ||
| }); | ||
| const Wrapped = injectIntl(Memoized); | ||
|
|
||
| const { rerender } = renderWithIntl(<Wrapped />); | ||
| rerender( | ||
| <IntlProvider locale="en" messages={messages}><Wrapped /></IntlProvider>, | ||
| ); | ||
|
|
||
| expect(renders).toEqual(1); | ||
| }); | ||
|
|
||
| it('does not attach a ref when the caller passes none', () => { | ||
| const spy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
| function Plain({ intl }: WrappedComponentProps) { | ||
| return <p>{intl.locale}</p>; | ||
| } | ||
| const Wrapped = injectIntl(Plain); | ||
|
|
||
| renderWithIntl(<Wrapped />); | ||
|
|
||
| /* React warns "Function components cannot be given refs" if we hand a ref | ||
| * to a component that can't hold one. */ | ||
| expect(spy).not.toHaveBeenCalled(); | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it('sets a displayName and exposes the wrapped component and its statics', () => { | ||
| function Labelled({ intl }: WrappedComponentProps) { | ||
| return <p>{intl.locale}</p>; | ||
| } | ||
| Labelled.displayName = 'Labelled'; | ||
| Labelled.someStatic = 'kept'; | ||
|
|
||
| const Wrapped = injectIntl(Labelled); | ||
|
|
||
| expect(Wrapped.displayName).toEqual('injectIntl(Labelled)'); | ||
| expect(Wrapped.WrappedComponent).toBe(Labelled); | ||
| expect((Wrapped as typeof Wrapped & { someStatic: string }).someStatic).toEqual('kept'); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.