Skip to content

Commit

Permalink
refactored language provider and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-wedensday committed Apr 11, 2024
1 parent 52035a5 commit 092e8dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
11 changes: 5 additions & 6 deletions app/components/atoms/LanguageProvider/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ export const initialState = fromJS({
/* eslint-disable default-case, no-param-reassign */
export const languageProviderReducer = (state = initialState, action) =>
produce(state, (/* draft */) => {
switch (action.type) {
case languageProviderTypes.CHANGE_LOCALE:
return state.set('locale', action.locale);
default:
return state;
}
const actionType = {
[languageProviderTypes.CHANGE_LOCALE]: () =>
state.set('locale', action.locale)
};
return action.type in actionType ? actionType[action.type]() : state;
});

export default languageProviderReducer;
19 changes: 8 additions & 11 deletions app/components/atoms/LanguageProvider/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React from 'react';
import 'react-native';
import { render } from '@testing-library/react-native';
import { Provider } from 'react-redux';
import T from '@atoms/T';
import createStore from 'app/rootReducer';
import { translationMessages } from 'app/i18n';
import { renderWithIntl } from '@utils/testUtils';
import { Text } from 'react-native';
import ConnectedLanguageProvider, { LanguageProvider } from '../index';

describe('<LanguageProvider /> container tests', () => {
it('should render its children', () => {
const children = <h1>Test</h1>;
const children = (
<h1>
<Text>Test</Text>
</h1>
);
const container = renderWithIntl(
<LanguageProvider messages={translationMessages} locale="en">
{children}
Expand All @@ -19,16 +22,10 @@ describe('<LanguageProvider /> container tests', () => {
expect(container.firstChild).not.toBeNull();
});
});

const setupReduxStore = () => ({ reduxStore: createStore().store });
describe('<ConnectedLanguageProvider /> container tests', () => {
let reduxStore;

beforeAll(() => {
const { store } = createStore();
reduxStore = store;
});

it('should render the default language messages', () => {
const { reduxStore } = setupReduxStore();
const { queryByText } = render(
<Provider store={reduxStore}>
<ConnectedLanguageProvider messages={translationMessages}>
Expand Down
13 changes: 5 additions & 8 deletions app/components/atoms/LanguageProvider/tests/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ import {
languageProviderTypes,
languageProviderReducer
} from '../reducer';

const setupMockedState = () => ({ mockedState: initialState });
/* eslint-disable default-case, no-param-reassign */
describe('Tests for LanguageProvider actions', () => {
let mockedState;
beforeEach(() => {
mockedState = initialState;
});

it('returns the initial state', () => {
const { mockedState } = setupMockedState();
expect(languageProviderReducer(undefined, {})).toEqual(mockedState);
});

it('changes the locale', () => {
const { mockedState } = setupMockedState();
const locale = 'de';
mockedState = mockedState.set('locale', locale);
const UpdateMockedState = mockedState.set('locale', locale);
expect(
languageProviderReducer(undefined, {
type: languageProviderTypes.CHANGE_LOCALE,
locale
})
).toEqual(mockedState);
).toEqual(UpdateMockedState);
});
});

0 comments on commit 092e8dc

Please sign in to comment.