From 092e8dcd8b66a60b19558f4895aaea0a137c3a10 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Fri, 12 Apr 2024 03:14:18 +0530 Subject: [PATCH] refactored language provider and tests --- .../atoms/LanguageProvider/reducer.js | 11 +++++------ .../LanguageProvider/tests/index.test.js | 19 ++++++++----------- .../LanguageProvider/tests/reducer.test.js | 13 +++++-------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/app/components/atoms/LanguageProvider/reducer.js b/app/components/atoms/LanguageProvider/reducer.js index 7388b34..ec846d7 100644 --- a/app/components/atoms/LanguageProvider/reducer.js +++ b/app/components/atoms/LanguageProvider/reducer.js @@ -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; diff --git a/app/components/atoms/LanguageProvider/tests/index.test.js b/app/components/atoms/LanguageProvider/tests/index.test.js index 5a943ae..fc1f33a 100644 --- a/app/components/atoms/LanguageProvider/tests/index.test.js +++ b/app/components/atoms/LanguageProvider/tests/index.test.js @@ -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(' container tests', () => { it('should render its children', () => { - const children =

Test

; + const children = ( +

+ Test +

+ ); const container = renderWithIntl( {children} @@ -19,16 +22,10 @@ describe(' container tests', () => { expect(container.firstChild).not.toBeNull(); }); }); - +const setupReduxStore = () => ({ reduxStore: createStore().store }); describe(' container tests', () => { - let reduxStore; - - beforeAll(() => { - const { store } = createStore(); - reduxStore = store; - }); - it('should render the default language messages', () => { + const { reduxStore } = setupReduxStore(); const { queryByText } = render( diff --git a/app/components/atoms/LanguageProvider/tests/reducer.test.js b/app/components/atoms/LanguageProvider/tests/reducer.test.js index cf185c1..baea145 100644 --- a/app/components/atoms/LanguageProvider/tests/reducer.test.js +++ b/app/components/atoms/LanguageProvider/tests/reducer.test.js @@ -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); }); });