Skip to content

Instantly share code, notes, and snippets.

@ronal2do
Forked from BrianJVarley/AppStrings.js
Created February 4, 2019 11:40
Show Gist options
  • Save ronal2do/12a680942774ff6ee0bdd3cc9c8b62aa to your computer and use it in GitHub Desktop.
Save ronal2do/12a680942774ff6ee0bdd3cc9c8b62aa to your computer and use it in GitHub Desktop.
Mocking react-native-localization package in jest spec
// ES6 module syntax
import LocalizedStrings from 'react-native-localization';
let AppStrings = new LocalizedStrings({
'en-US': {
settingMenuOptionOne: 'Centimeters ({0})',
},
en: {
settingMenuOptionOne: 'Centimeters ({0})',
},
it:
{
}
});
module.exports = AppStrings;
export default class RNLocalization {
_language = 'en';
constructor(props) {
this.props = props;
this._setLanguage(this._language);
}
_setLanguage(interfaceLanguage) {
this._language = interfaceLanguage;
if (this.props[interfaceLanguage]) {
const localizedStrings = this.props[this._language];
for (const key in localizedStrings) {
if (localizedStrings.hasOwnProperty(key)) {
this[key] = localizedStrings[key];
}
}
}
}
}
import React,{Component} from 'react';
import {
Text
} from 'react-native';
import AppStrings from '../../localization/appStrings';
class SettingMenuView extends Component {
render() {
const {
selected,
onCmUnitsSelected,
onInchUnitsSelected
} = this.props;
return (
<Text>{AppStrings.settingMenuOptionOne}</Text>
);
}
}
export default SettingMenuView;
/*eslint-disable max-nested-callbacks*/
import React from 'react';
import {shallow} from 'enzyme';
import SettingMenuView from '../SettingMenuView';
import {
Text
} from 'react-native';
import mockRNLocalization from '../../../mocks/react-native-localization-mock';
jest.mock('react-native-localization', () => mockRNLocalization);
describe('<SettingMenuView />', () => {
it('should render a <Menu> with a menu title', () => {
const mockCmUnitsSelectedfn = jest.fn();
const mockInchUnitsSelectedfn = jest.fn();
const wrapper = shallow(
<SettingMenuView
selected={'CM'}
onCmUnitsSelected={mockCmUnitsSelectedfn}
onInchUnitsSelected={mockInchUnitsSelectedfn}
/>
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment