|
1 | | -import React from 'react'; |
| 1 | +import React, { Component } from 'react'; |
2 | 2 | import TestRenderer from 'react-test-renderer'; |
3 | 3 | import { Provider as MessageSourceProvider } from './MessageSourceContext'; |
4 | 4 | import * as MessageSource from './withMessages'; |
@@ -164,4 +164,42 @@ describe('withMessages', () => { |
164 | 164 | expect(levelOneComponent.children[0]).toBe('Hello World'); |
165 | 165 | expect(levelTwoComponent.children[0]).toBe('Hallo Welt'); |
166 | 166 | }); |
| 167 | + |
| 168 | + it('supports ref forwarding', () => { |
| 169 | + const NestedHOC = MessageSource.withMessages('hello')( |
| 170 | + class Nested extends Component { |
| 171 | + myMethod = () => { |
| 172 | + return 100; |
| 173 | + }; |
| 174 | + |
| 175 | + render() { |
| 176 | + const { getMessageWithNamedParams } = this.props; // eslint-disable-line react/prop-types |
| 177 | + return <React.Fragment>{getMessageWithNamedParams('hello.world')}</React.Fragment>; |
| 178 | + } |
| 179 | + }, |
| 180 | + ); |
| 181 | + |
| 182 | + // eslint-disable-next-line react/no-multi-comp |
| 183 | + class MyFwRefComponent extends Component { |
| 184 | + nestedRef = React.createRef(); |
| 185 | + |
| 186 | + render() { |
| 187 | + return <NestedHOC ref={this.nestedRef} />; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + const renderer = TestRenderer.create( |
| 192 | + <MessageSourceProvider value={translations}> |
| 193 | + <MyFwRefComponent /> |
| 194 | + </MessageSourceProvider>, |
| 195 | + ); |
| 196 | + |
| 197 | + const { root } = renderer; |
| 198 | + const fwRefCompInstance = root.findByType(MyFwRefComponent); |
| 199 | + |
| 200 | + expect(fwRefCompInstance.instance).toBeDefined(); |
| 201 | + expect(fwRefCompInstance.instance.nestedRef.current).toBeDefined(); |
| 202 | + expect(fwRefCompInstance.instance.nestedRef.current.myMethod).toBeDefined(); |
| 203 | + expect(fwRefCompInstance.instance.nestedRef.current.myMethod()).toBe(100); |
| 204 | + }); |
167 | 205 | }); |
0 commit comments