From 4249eef6539cd2f5b9e05852f4e56e36a5a25853 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 14 Jan 2022 18:00:04 -0300 Subject: [PATCH] chore: migrate settings to typescript --- app/actions/settings.js | 21 -------------- app/actions/settings.ts | 34 +++++++++++++++++++++++ app/reducers/settings.test.ts | 31 +++++++++++++++++++++ app/reducers/{settings.js => settings.ts} | 8 ++++-- 4 files changed, 71 insertions(+), 23 deletions(-) delete mode 100644 app/actions/settings.js create mode 100644 app/actions/settings.ts create mode 100644 app/reducers/settings.test.ts rename app/reducers/{settings.js => settings.ts} (57%) diff --git a/app/actions/settings.js b/app/actions/settings.js deleted file mode 100644 index 6fae375bc..000000000 --- a/app/actions/settings.js +++ /dev/null @@ -1,21 +0,0 @@ -import { SETTINGS } from './actionsTypes'; - -export function addSettings(settings) { - return { - type: SETTINGS.ADD, - payload: settings - }; -} - -export function updateSettings(id, value) { - return { - type: SETTINGS.UPDATE, - payload: { id, value } - }; -} - -export function clearSettings() { - return { - type: SETTINGS.CLEAR - }; -} diff --git a/app/actions/settings.ts b/app/actions/settings.ts new file mode 100644 index 000000000..6f28570b2 --- /dev/null +++ b/app/actions/settings.ts @@ -0,0 +1,34 @@ +import { Action } from 'redux'; + +import { ISettings } from '../reducers/settings'; +import { SETTINGS } from './actionsTypes'; + +interface IAddSettings extends Action { + payload: ISettings; +} + +interface IUpdateSettings extends Action { + payload: { id: string; value: string }; +} + +export type IActionSettings = IAddSettings & IUpdateSettings; + +export function addSettings(settings: ISettings): IAddSettings { + return { + type: SETTINGS.ADD, + payload: settings + }; +} + +export function updateSettings(id: string, value: string): IUpdateSettings { + return { + type: SETTINGS.UPDATE, + payload: { id, value } + }; +} + +export function clearSettings(): Action { + return { + type: SETTINGS.CLEAR + }; +} diff --git a/app/reducers/settings.test.ts b/app/reducers/settings.test.ts new file mode 100644 index 000000000..da93ce3cc --- /dev/null +++ b/app/reducers/settings.test.ts @@ -0,0 +1,31 @@ +import { addSettings, clearSettings, updateSettings } from '../actions/settings'; +import { mockedStore } from './mockedStore'; +import { initialState } from './settings'; + +describe('test settings reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().settings; + expect(state).toEqual(initialState); + }); + + const settings = { API_Use_REST_For_DDP_Calls: true, FileUpload_MaxFileSize: 600857600, Jitsi_URL_Room_Prefix: 'RocketChat' }; + + it('should return modified store after call addSettings action', () => { + mockedStore.dispatch(addSettings(settings)); + const state = mockedStore.getState().settings; + expect(state).toEqual(settings); + }); + + it('should return correctly settings after call updateSettings action', () => { + const id = 'Jitsi_URL_Room_Prefix'; + mockedStore.dispatch(updateSettings(id, 'ChatRocket')); + const state = mockedStore.getState().settings; + expect(state[id]).toEqual('ChatRocket'); + }); + + it('should return initial state after clearSettings', () => { + mockedStore.dispatch(clearSettings()); + const state = mockedStore.getState().settings; + expect(state).toEqual({}); + }); +}); diff --git a/app/reducers/settings.js b/app/reducers/settings.ts similarity index 57% rename from app/reducers/settings.js rename to app/reducers/settings.ts index 6e9ab5005..bcaad3019 100644 --- a/app/reducers/settings.js +++ b/app/reducers/settings.ts @@ -1,8 +1,12 @@ +import { IActionSettings } from '../actions/settings'; import { SETTINGS } from '../actions/actionsTypes'; -const initialState = {}; +// TODO UPDATE SETTINGS TYPE +export type ISettings = Record; -export default (state = initialState, action) => { +export const initialState: ISettings = {}; + +export default (state = initialState, action: IActionSettings): ISettings => { switch (action.type) { case SETTINGS.ADD: return {