Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active December 10, 2021 07:26
Show Gist options
  • Save BrianJVarley/9629f1ebc8969e7cf5fc7d1ce94c49e1 to your computer and use it in GitHub Desktop.
Save BrianJVarley/9629f1ebc8969e7cf5fc7d1ce94c49e1 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