Skip to content

Commit 168f743

Browse files
committed
restructure propTypes api
1 parent 595748b commit 168f743

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
export { Provider } from './lib/MessageSourceContext';
55
export { FetchingProvider } from './lib/FetchingProvider';
66
export { useMessageSource } from './lib/useMessageSource';
7-
export { withMessages, propTypes } from './lib/withMessages';
7+
export { withMessages } from './lib/withMessages';
8+
export { propTypes } from './lib/propTypes';

src/lib/propTypes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import PropTypes from 'prop-types';
2+
3+
/**
4+
* Example usage:
5+
*
6+
* Exported just for convenience, in case you want to run propType checks on your component.
7+
* Note: some bundlers might remove these definitions during build time.
8+
*/
9+
export const propTypes = {
10+
getMessage: PropTypes.func,
11+
getMessageWithNamedParams: PropTypes.func,
12+
};

src/lib/useMessageSource.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import TestRenderer from 'react-test-renderer';
33
import { useMessageSource } from './useMessageSource';
44
import { Provider as MessageSourceProvider } from './MessageSourceContext';
5+
import { propTypes as MessageSourceApi } from './propTypes';
56

67
describe('useMessageSource', () => {
78
const translations = {
@@ -10,6 +11,23 @@ describe('useMessageSource', () => {
1011
'greeting.named': 'Hello {name}',
1112
};
1213

14+
it('exposes the correct api', () => {
15+
function AssertApi() {
16+
const hooksApi = useMessageSource();
17+
Object.keys(MessageSourceApi).forEach(api => {
18+
expect(hooksApi[api]).toBeDefined();
19+
});
20+
21+
return null;
22+
}
23+
24+
TestRenderer.create(
25+
<MessageSourceProvider value={translations}>
26+
<AssertApi />
27+
</MessageSourceProvider>,
28+
);
29+
});
30+
1331
it('retrieves the correct translated value with named parameters', () => {
1432
function Nested() {
1533
const { getMessageWithNamedParams } = useMessageSource();

src/lib/withMessages.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32
import invariant from 'invariant';
43
import hoistNonReactStatics from 'hoist-non-react-statics';
54
import { useMessageSource } from './useMessageSource';
@@ -59,14 +58,3 @@ function internalWithMessages(keyPrefixOrComponent) {
5958
* 4. compose(MessageSource.withMessages)(Component)
6059
*/
6160
export const withMessages = internalWithMessages;
62-
63-
/**
64-
* Example usage:
65-
*
66-
* Exported just for convenience, in case you want to run propType checks on your component.
67-
* Note: some bundlers might remove these definitions during build time.
68-
*/
69-
export const propTypes = {
70-
getMessage: PropTypes.func,
71-
getMessageWithNamedParams: PropTypes.func,
72-
};

src/lib/withMessages.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
22
import TestRenderer from 'react-test-renderer';
33
import { Provider as MessageSourceProvider } from './MessageSourceContext';
4-
import * as MessageSource from './messageSource';
5-
6-
/* eslint-disable react/prop-types */
4+
import * as MessageSource from './withMessages';
5+
import { propTypes as MessageSourceApi } from './propTypes';
76

87
describe('withMessages', () => {
98
const translations = {
@@ -20,8 +19,9 @@ describe('withMessages', () => {
2019
const { root } = renderer;
2120

2221
const captorInstance = root.findByType(PropsCaptor);
23-
expect(captorInstance.props.getMessage).toBeDefined();
24-
expect(captorInstance.props.getMessageWithNamedParams).toBeDefined();
22+
Object.keys(MessageSourceApi).forEach(api => {
23+
expect(captorInstance.props[api]).toBeDefined();
24+
});
2525
});
2626

2727
it('retrieves the correct translated value with named parameters', () => {
@@ -108,7 +108,7 @@ describe('withMessages', () => {
108108

109109
it('[curried] retrieves the correct translated value in mixed mode', () => {
110110
function Nested(props) {
111-
const { getMessage } = props;
111+
const { getMessage } = props; // eslint-disable-line react/prop-types
112112
return (
113113
<React.Fragment>
114114
{getMessage('world')}
@@ -164,9 +164,4 @@ describe('withMessages', () => {
164164
expect(levelOneComponent.children[0]).toBe('Hello World');
165165
expect(levelTwoComponent.children[0]).toBe('Hallo Welt');
166166
});
167-
168-
it('propTypes are exported', () => {
169-
// eslint-disable-next-line react/forbid-foreign-prop-types
170-
expect(MessageSource.propTypes).toBeDefined();
171-
});
172167
});

0 commit comments

Comments
 (0)