From 701361a68617e2d02516932bfe6de5237fc2a914 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 7 Jan 2022 17:24:24 -0300 Subject: [PATCH] chore: add tests and migrate RootEnum --- app/actions/app.ts | 6 ++-- app/definitions/redux/TRootEnum.ts | 12 ++++---- app/reducers/app.test.ts | 44 ++++++++++++++++++++++++++++++ app/reducers/app.ts | 6 ++-- 4 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 app/reducers/app.test.ts diff --git a/app/actions/app.ts b/app/actions/app.ts index ece16ecdb..5fdb312ef 100644 --- a/app/actions/app.ts +++ b/app/actions/app.ts @@ -1,6 +1,6 @@ import { Action } from 'redux'; -import { TRootEnum } from '../definitions'; +import { RootEnum } from '../definitions'; import { APP } from './actionsTypes'; export const ROOT_OUTSIDE = 'outside'; @@ -9,7 +9,7 @@ export const ROOT_LOADING = 'loading'; export const ROOT_SET_USERNAME = 'setUsername'; interface IAppStart extends Action { - root: TRootEnum; + root: RootEnum; text?: string; } @@ -19,7 +19,7 @@ interface ISetMasterDetail extends Action { export type TActionApp = IAppStart & ISetMasterDetail; -export function appStart({ root, ...args }: { root: TRootEnum }): IAppStart { +export function appStart({ root, ...args }: { root: RootEnum }): IAppStart { return { type: APP.START, root, diff --git a/app/definitions/redux/TRootEnum.ts b/app/definitions/redux/TRootEnum.ts index 0c8f801cd..8b0faca42 100644 --- a/app/definitions/redux/TRootEnum.ts +++ b/app/definitions/redux/TRootEnum.ts @@ -1,6 +1,6 @@ -export type TRootEnum = { - ROOT_OUTSIDE: 'outside'; - ROOT_INSIDE: 'inside'; - ROOT_LOADING: 'loading'; - ROOT_SET_USERNAME: 'setUsername'; -}; +export enum RootEnum { + ROOT_OUTSIDE = 'outside', + ROOT_INSIDE = 'inside', + ROOT_LOADING = 'loading', + ROOT_SET_USERNAME = 'setUsername' +} diff --git a/app/reducers/app.test.ts b/app/reducers/app.test.ts new file mode 100644 index 000000000..a4910ab0f --- /dev/null +++ b/app/reducers/app.test.ts @@ -0,0 +1,44 @@ +import { appStart, appInit, setMasterDetail } from '../actions/app'; +import { initialState } from './app'; +import { mockedStore } from './mockedStore'; +import { RootEnum } from '../definitions'; +import { APP_STATE } from '../actions/actionsTypes'; + +describe('test reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().app; + expect(state).toEqual(initialState); + }); + + it('should return root state after dispatch appStart action', () => { + mockedStore.dispatch(appStart({ root: RootEnum.ROOT_INSIDE })); + const { root } = mockedStore.getState().app; + expect(root).toEqual(RootEnum.ROOT_INSIDE); + }); + + it('should return ready state after dispatch appInit action', () => { + mockedStore.dispatch(appInit()); + const { ready } = mockedStore.getState().app; + expect(ready).toEqual(false); + }); + + it('should return ready state after dispatch setMasterDetail action', () => { + mockedStore.dispatch(setMasterDetail(false)); + const { isMasterDetail } = mockedStore.getState().app; + expect(isMasterDetail).toEqual(false); + }); + + it('should return correct state after app go to foreground', () => { + mockedStore.dispatch({ type: APP_STATE.FOREGROUND }); + const { foreground, background } = mockedStore.getState().app; + expect(foreground).toEqual(true); + expect(background).toEqual(false); + }); + + it('should return correct state after app go to background', () => { + mockedStore.dispatch({ type: APP_STATE.BACKGROUND }); + const { foreground, background } = mockedStore.getState().app; + expect(foreground).toEqual(false); + expect(background).toEqual(true); + }); +}); diff --git a/app/reducers/app.ts b/app/reducers/app.ts index 302bff8a7..5cd948973 100644 --- a/app/reducers/app.ts +++ b/app/reducers/app.ts @@ -1,9 +1,9 @@ import { TActionApp } from '../actions/app'; -import { TRootEnum } from '../definitions'; +import { RootEnum } from '../definitions'; import { APP, APP_STATE } from '../actions/actionsTypes'; export interface IApp { - root?: TRootEnum; + root?: RootEnum; isMasterDetail: boolean; text?: string; ready: boolean; @@ -11,7 +11,7 @@ export interface IApp { background: boolean; } -const initialState: IApp = { +export const initialState: IApp = { root: undefined, isMasterDetail: false, text: undefined,