From dbe8221d69b845adba6430bfe5082ce8ef088fd9 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 12 Jan 2022 17:08:45 -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 e95763e29..1b9941979 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -3,11 +3,12 @@ import { TActionActiveUsers } from '../../actions/activeUsers'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; +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..bd9899d81 --- /dev/null +++ b/app/reducers/connect.test.ts @@ -0,0 +1,44 @@ +import { 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 {