From 1db676f424e3806c82aabae939c476f463a81810 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Mon, 10 Jan 2022 15:34:07 -0300 Subject: [PATCH] chore: migrate connect to ts and add tests --- app/actions/connect.js | 20 ----------- app/actions/connect.ts | 21 ++++++++++++ app/definitions/redux/index.ts | 3 +- app/reducers/connect.test.ts | 44 +++++++++++++++++++++++++ app/reducers/{connect.js => connect.ts} | 11 +++++-- 5 files changed, 76 insertions(+), 23 deletions(-) delete mode 100644 app/actions/connect.js create mode 100644 app/actions/connect.ts create mode 100644 app/reducers/connect.test.ts rename app/reducers/{connect.js => connect.ts} (63%) diff --git a/app/actions/connect.js b/app/actions/connect.js deleted file mode 100644 index 57f46c71b..000000000 --- a/app/actions/connect.js +++ /dev/null @@ -1,20 +0,0 @@ -import * as types from './actionsTypes'; - -export function connectRequest() { - return { - type: types.METEOR.REQUEST - }; -} - -export function connectSuccess() { - return { - type: types.METEOR.SUCCESS - }; -} - -export function disconnect(err) { - return { - type: types.METEOR.DISCONNECT, - err - }; -} diff --git a/app/actions/connect.ts b/app/actions/connect.ts new file mode 100644 index 000000000..be566253b --- /dev/null +++ b/app/actions/connect.ts @@ -0,0 +1,21 @@ +import { Action } from 'redux'; + +import * as types from './actionsTypes'; + +export function connectRequest(): Action { + return { + type: types.METEOR.REQUEST + }; +} + +export function connectSuccess(): Action { + return { + type: types.METEOR.SUCCESS + }; +} + +export function disconnect(): Action { + return { + type: types.METEOR.DISCONNECT + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 59eb7c921..212e304bb 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -5,11 +5,12 @@ import { TActionApp } from '../../actions/app'; import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; import { IApp } from '../../reducers/app'; +import { IConnect } from '../../reducers/connect'; export interface IApplicationState { settings: any; login: any; - meteor: any; + meteor: IConnect; server: any; selectedUsers: ISelectedUsers; createChannel: any; diff --git a/app/reducers/connect.test.ts b/app/reducers/connect.test.ts new file mode 100644 index 000000000..349305af8 --- /dev/null +++ b/app/reducers/connect.test.ts @@ -0,0 +1,44 @@ +import { appStart, appInit, setMasterDetail } from '../actions/app'; +import { initialState } from './connect'; +import { mockedStore } from './mockedStore'; +import { RootEnum } from '../definitions'; +import { APP_STATE } from '../actions/actionsTypes'; + +describe('test reducer', () => { + it('should return initial state', () => { + const { meteor } = mockedStore.getState(); + expect(meteor).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/connect.js b/app/reducers/connect.ts similarity index 63% rename from app/reducers/connect.js rename to app/reducers/connect.ts index a8ef2bea1..beffc58bd 100644 --- a/app/reducers/connect.js +++ b/app/reducers/connect.ts @@ -1,11 +1,18 @@ +import { Action } from 'redux'; + import { METEOR } from '../actions/actionsTypes'; -const initialState = { +export interface IConnect { + connecting: boolean; + connected: boolean; +} + +export const initialState: IConnect = { connecting: false, connected: false }; -export default function connect(state = initialState, action) { +export default function connect(state = initialState, action: Action): IConnect { switch (action.type) { case METEOR.REQUEST: return {