Skip to content

Commit

Permalink
forwardRef UT
Browse files Browse the repository at this point in the history
  • Loading branch information
kwang-aa committed Mar 20, 2019
1 parent 1620db5 commit 80a96b2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 66 deletions.
131 changes: 69 additions & 62 deletions __test__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<I18nProvider> children get i18n from I18nProvider 1`] = `
<TestElement
<UNDEFINED
i18n={
Object {
"gettext": [Function],
Expand All @@ -27,31 +27,36 @@ exports[`<I18nProvider> children get i18n from I18nProvider 2`] = `
}
}
>
<Translate
<Translate(TestElement)
testProp="required"
>
<TestElement
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
<Translate
forwardedRef={null}
testProp="required"
>
<div>
Test
</div>
</TestElement>
</Translate>
<TestElement
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
testProp="required"
>
<div>
Test
</div>
</TestElement>
</Translate>
</Translate(TestElement)>
</I18nProvider>
`;
exports[`translate Component render anonymous component 1`] = `
<Translate
<Translate(Component)
i18n={
Object {
"gettext": [Function],
Expand All @@ -63,7 +68,8 @@ exports[`translate Component render anonymous component 1`] = `
}
testProp="required"
>
<Component
<Translate
forwardedRef={null}
i18n={
Object {
"gettext": [Function],
Expand All @@ -75,48 +81,35 @@ exports[`translate Component render anonymous component 1`] = `
}
testProp="required"
>
<div>
My
required
</div>
</Component>
</Translate>
<Component
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
testProp="required"
>
<div>
My
required
</div>
</Component>
</Translate>
</Translate(Component)>
`;
exports[`translate Component render translated component 1`] = `
<Translate
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
testProp="required"
>
<TestElement
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
testProp="required"
>
<div>
Test
</div>
</TestElement>
</Translate>
<ContextConsumer>
<Component />
</ContextConsumer>
`;
exports[`translate Component render translated stateless component 1`] = `
<Translate
<Translate(StatelessTest)
i18n={
Object {
"gettext": [Function],
Expand All @@ -128,7 +121,8 @@ exports[`translate Component render translated stateless component 1`] = `
}
testProp="required"
>
<StatelessTest
<Translate
forwardedRef={null}
i18n={
Object {
"gettext": [Function],
Expand All @@ -140,10 +134,23 @@ exports[`translate Component render translated stateless component 1`] = `
}
testProp="required"
>
<div>
My
required
</div>
</StatelessTest>
</Translate>
<StatelessTest
i18n={
Object {
"gettext": [Function],
"lang": "en-US",
"ngettext": [Function],
"npgettext": [Function],
"pgettext": [Function],
}
}
testProp="required"
>
<div>
My
required
</div>
</StatelessTest>
</Translate>
</Translate(StatelessTest)>
`;
13 changes: 10 additions & 3 deletions __test__/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow strict
import React from 'react';
import { mount } from 'enzyme';
import { mount, shallow } from 'enzyme';
import { Jed, translate, useI18n, I18nProvider, type I18nType } from '../src';
import I18nContext from '../src/I18nContext';
import mockI18n from '../src/mockI18n';
Expand Down Expand Up @@ -88,7 +88,9 @@ describe('<I18nProvider>', () => {
describe('translate Component', () => {
it('render translated component', () => {
const LocalizedEle = translate(TestElement);
const localizedEle = mount(<LocalizedEle i18n={mockI18n} testProp="required" />);
const localizedEle = shallow(
<LocalizedEle i18n={mockI18n} testProp="required" />
).dive();
const instEle = localizedEle.instance();
// $FlowFixMe
expect(instEle.props.i18n).toEqual(mockI18n);
Expand Down Expand Up @@ -143,10 +145,15 @@ describe('translate Component', () => {

test = () => {
const { current } = this.ref;
return current ? current.getName() : '';
// $FlowFixMe
if (current && typeof current.getName === 'function') {
return current.getName();
}
return '';
};

render() {
// $FlowFixMe
return <B ref={this.ref} />;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function translate<Com: React$ComponentType<*>>(

ForwardedComponent.displayName = `Translate(${name})`;

return (hoistStatics(Translate, WrappedComponent): any);
return (hoistStatics(ForwardedComponent, WrappedComponent): any);
}

export default translate;

0 comments on commit 80a96b2

Please sign in to comment.