From 3cc5a7b6565e98fd8b0751bb0cb55eafc800a650 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Thu, 13 Jan 2022 18:45:14 -0300 Subject: [PATCH] chore: migrate redux module sortPreferences to typescript --- app/actions/sortPreferences.js | 15 -------- app/actions/sortPreferences.ts | 28 +++++++++++++++ app/definitions/index.ts | 1 + app/definitions/redux/index.ts | 5 +-- app/reducers/sortPreferences.test.ts | 35 +++++++++++++++++++ ...{sortPreferences.js => sortPreferences.ts} | 5 +-- 6 files changed, 70 insertions(+), 19 deletions(-) delete mode 100644 app/actions/sortPreferences.js create mode 100644 app/actions/sortPreferences.ts create mode 100644 app/reducers/sortPreferences.test.ts rename app/reducers/{sortPreferences.js => sortPreferences.ts} (72%) diff --git a/app/actions/sortPreferences.js b/app/actions/sortPreferences.js deleted file mode 100644 index e452e74c..00000000 --- a/app/actions/sortPreferences.js +++ /dev/null @@ -1,15 +0,0 @@ -import * as types from './actionsTypes'; - -export function setAllPreferences(preferences) { - return { - type: types.SORT_PREFERENCES.SET_ALL, - preferences - }; -} - -export function setPreference(preference) { - return { - type: types.SORT_PREFERENCES.SET, - preference - }; -} diff --git a/app/actions/sortPreferences.ts b/app/actions/sortPreferences.ts new file mode 100644 index 00000000..6a332861 --- /dev/null +++ b/app/actions/sortPreferences.ts @@ -0,0 +1,28 @@ +import { Action } from 'redux'; + +import { IPreferences } from '../definitions'; +import { SORT_PREFERENCES } from './actionsTypes'; + +interface ISetAllPreferences extends Action { + preferences: IPreferences; +} + +interface ISetPreference extends Action { + preference: Partial; +} + +export type TActionSortPreferences = ISetAllPreferences & ISetPreference; + +export function setAllPreferences(preferences: IPreferences): ISetAllPreferences { + return { + type: SORT_PREFERENCES.SET_ALL, + preferences + }; +} + +export function setPreference(preference: Partial): ISetPreference { + return { + type: SORT_PREFERENCES.SET, + preference + }; +} diff --git a/app/definitions/index.ts b/app/definitions/index.ts index 80eeb88c..25b7e849 100644 --- a/app/definitions/index.ts +++ b/app/definitions/index.ts @@ -8,6 +8,7 @@ export * from './INotification'; export * from './IRoom'; export * from './IServer'; export * from './ISubscription'; +export * from './IPreferences'; export interface IBaseScreen, S extends string> { navigation: StackNavigationProp; diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index e95763e2..b1ac102e 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,5 +1,6 @@ -import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; +import { TActionSelectedUsers } from '../../actions/selectedUsers'; +import { TActionSortPreferences } from '../../actions/sortPreferences'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; @@ -28,4 +29,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionSortPreferences; diff --git a/app/reducers/sortPreferences.test.ts b/app/reducers/sortPreferences.test.ts new file mode 100644 index 00000000..5de29933 --- /dev/null +++ b/app/reducers/sortPreferences.test.ts @@ -0,0 +1,35 @@ +import { IPreferences } from '../definitions'; +import { setAllPreferences, setPreference } from '../actions/sortPreferences'; +import { mockedStore } from './mockedStore'; +import { initialState } from './sortPreferences'; +import { DisplayMode, SortBy } from '../constants/constantDisplayMode'; + +describe('test sortPreferences reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().sortPreferences; + expect(state).toEqual(initialState); + }); + + it('should return correctly value after call setPreference action', () => { + const preferences: IPreferences = { + displayMode: DisplayMode.Condensed, + groupByType: true, + showAvatar: true, + showFavorites: true, + showUnread: true, + sortBy: SortBy.Activity + }; + mockedStore.dispatch(setAllPreferences(preferences)); + const state = mockedStore.getState().sortPreferences; + expect(state).toEqual(preferences); + }); + + it('should return correctly value after call setPreference action', () => { + const preference: Partial = { + displayMode: DisplayMode.Expanded + }; + mockedStore.dispatch(setPreference(preference)); + const { displayMode } = mockedStore.getState().sortPreferences; + expect(displayMode).toEqual(DisplayMode.Expanded); + }); +}); diff --git a/app/reducers/sortPreferences.js b/app/reducers/sortPreferences.ts similarity index 72% rename from app/reducers/sortPreferences.js rename to app/reducers/sortPreferences.ts index 4ad9e797..2083e8f7 100644 --- a/app/reducers/sortPreferences.js +++ b/app/reducers/sortPreferences.ts @@ -1,7 +1,8 @@ import { SORT_PREFERENCES } from '../actions/actionsTypes'; import { DisplayMode, SortBy } from '../constants/constantDisplayMode'; +import { IPreferences, TApplicationActions } from '../definitions'; -const initialState = { +export const initialState: IPreferences = { sortBy: SortBy.Activity, groupByType: false, showFavorites: false, @@ -10,7 +11,7 @@ const initialState = { displayMode: DisplayMode.Expanded }; -export default (state = initialState, action) => { +export default (state = initialState, action: TApplicationActions): IPreferences => { switch (action.type) { case SORT_PREFERENCES.SET_ALL: return {