Skip to content

Commit

Permalink
Generate: container & component with Lang
Browse files Browse the repository at this point in the history
  • Loading branch information
keremcubuk committed Nov 14, 2019
1 parent 9eea6b0 commit 74cd418
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
12 changes: 7 additions & 5 deletions app/containers/Welcome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import React from 'react';
import { View, Text } from 'react-native';
import PropTypes from 'prop-types';
import I18n from 'localization';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { compose } from 'redux';
Expand All @@ -23,7 +24,7 @@ export function Welcome() {

return (
<View>
<Text>Hello Welcome</Text>
<Text>{I18n.t('firstMessage.hello')} Welcome Container!</Text>
</View>
);
}
Expand All @@ -42,8 +43,9 @@ function mapDispatchToProps(dispatch) {
};
}

const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withConnect = connect(
mapStateToProps,
mapDispatchToProps,
);

export default compose(
withConnect,
)(Welcome);
export default compose(withConnect)(Welcome);
5 changes: 4 additions & 1 deletion app/containers/Welcome/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const selectWelcomeDomain = state => state.welcome || initialState;
*/

const makeSelectWelcome = () =>
createSelector(selectWelcomeDomain, substate => substate);
createSelector(
selectWelcomeDomain,
substate => substate,
);

export default makeSelectWelcome;
export { selectWelcomeDomain };
4 changes: 3 additions & 1 deletion app/containers/Welcome/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Tests for Welcome
*
* @see https://github.com/react-boilerplate/react-boilerplate/tree/master/docs/testing
* @see https://github.com/keremcubuk/react-native-boilerplate/tree/master/docs/testing
*
*/

Expand All @@ -16,6 +16,7 @@ describe('<Welcome />', () => {
it('Expect to not log errors in console', () => {
const spy = jest.spyOn(global.console, 'error');
const dispatch = jest.fn();

render(<Welcome dispatch={dispatch} />);
expect(spy).not.toHaveBeenCalled();
});
Expand All @@ -33,6 +34,7 @@ describe('<Welcome />', () => {
const {
container: { firstChild },
} = render(<Welcome />);

expect(firstChild).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions app/localization/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const en = {
localization: {
deviceLanguage: 'en-US',
},
component: {
welcome: 'Welcome',
firstMessage: {
hello: 'Hello from',
},
};

Expand Down
4 changes: 2 additions & 2 deletions app/localization/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const fr = {
localization: {
deviceLanguage: 'fr',
},
component: {
welcome: 'Bienvenue',
firstMessage: {
hello: 'Bonjour de',
},
};

Expand Down
2 changes: 1 addition & 1 deletion internals/generators/component/index.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function {{ properCase name }}() {
return (
<View>
{{#if wantMessages}}
<Text>{I18n.t('component.welcome')} {{ properCase name }} Component!</Text>
<Text>{I18n.t('firstMessage.hello')} {{ properCase name }} Component!</Text>
{{else}}
<Text>{{ properCase name }} Component!</Text>
{{/if}}
Expand Down
6 changes: 6 additions & 0 deletions internals/generators/container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module.exports = {
default: true,
message: 'Do you want sagas for asynchronous flows? (e.g. fetching data)',
},
{
type: 'confirm',
name: 'wantMessages',
default: true,
message: 'Do you want i18n messages (i.e. will this component use text)?',
},
],
actions: data => {
// Generate index.js and index.test.js
Expand Down
9 changes: 8 additions & 1 deletion internals/generators/container/index.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import React from 'react';
import { View, Text } from 'react-native';
import PropTypes from 'prop-types';
{{#if wantMessages}}
import I18n from 'localization';
{{/if}}
import { connect } from 'react-redux';
{{#if wantActionsAndReducer}}
import { createStructuredSelector } from 'reselect';
Expand Down Expand Up @@ -35,7 +38,11 @@ export function {{ properCase name }}() {

return (
<View>
<Text>{{ properCase name }} Container!</Text>
{{#if wantMessages}}
<Text>{I18n.t('firstMessage.hello')} {{ properCase name }} Container!</Text>
{{else}}
<Text>{{ properCase name }} Container!</Text>
{{/if}}
</View>
);
}
Expand Down
29 changes: 4 additions & 25 deletions internals/generators/container/test.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,22 @@
*
* Tests for {{ properCase name }}
*
* @see https://github.com/react-boilerplate/react-boilerplate/tree/master/docs/testing
* @see https://github.com/keremcubuk/react-native-boilerplate/tree/master/docs/testing
*
*/

import React from 'react';
import { render } from 'react-testing-library';
{{#if wantMessages}}
import { IntlProvider } from 'react-intl';
{{/if}}
// import 'jest-dom/extend-expect'; // add some helpful assertions

import { {{ properCase name }} } from '../index';
{{#if wantMessages}}
import { DEFAULT_LOCALE } from '../../../i18n';
{{/if}}

describe('<{{ properCase name }} />', () => {
it('Expect to not log errors in console', () => {
const spy = jest.spyOn(global.console, 'error');
const dispatch = jest.fn();
{{#if wantMessages}}
render(
<IntlProvider locale={DEFAULT_LOCALE}>
<{{ properCase name }} dispatch={dispatch} />
</IntlProvider>,
);
{{else}}

render(<{{ properCase name }} dispatch={dispatch} />);
{{/if}}
expect(spy).not.toHaveBeenCalled();
});

Expand All @@ -44,19 +31,11 @@ describe('<{{ properCase name }} />', () => {
* @see {@link https://jestjs.io/docs/en/api#testskipname-fn}
*/
it.skip('Should render and match the snapshot', () => {
{{#if wantMessages}}
const {
container: { firstChild },
} = render(
<IntlProvider locale={DEFAULT_LOCALE}>
<{{ properCase name }} />
</IntlProvider>,
);
{{else}}

const {
container: { firstChild },
} = render(<{{ properCase name }} />);
{{/if}}

expect(firstChild).toMatchSnapshot();
});
});

0 comments on commit 74cd418

Please sign in to comment.