From f19787f35a65956cec151061791d980c3a89473e Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Mon, 10 Jan 2022 17:47:54 -0300 Subject: [PATCH] chore: migrate createChannel to ts and add tests --- app/actions/createChannel.js | 23 ---------- app/actions/createChannel.ts | 42 +++++++++++++++++++ app/definitions/redux/index.ts | 6 ++- app/reducers/createChannel.test.ts | 33 +++++++++++++++ .../{createChannel.js => createChannel.ts} | 12 +++++- 5 files changed, 89 insertions(+), 27 deletions(-) delete mode 100644 app/actions/createChannel.js create mode 100644 app/actions/createChannel.ts create mode 100644 app/reducers/createChannel.test.ts rename app/reducers/{createChannel.js => createChannel.ts} (62%) diff --git a/app/actions/createChannel.js b/app/actions/createChannel.js deleted file mode 100644 index c93b47ef4..000000000 --- a/app/actions/createChannel.js +++ /dev/null @@ -1,23 +0,0 @@ -import * as types from './actionsTypes'; - -export function createChannelRequest(data) { - return { - type: types.CREATE_CHANNEL.REQUEST, - data - }; -} - -export function createChannelSuccess(data) { - return { - type: types.CREATE_CHANNEL.SUCCESS, - data - }; -} - -export function createChannelFailure(err, isTeam) { - return { - type: types.CREATE_CHANNEL.FAILURE, - err, - isTeam - }; -} diff --git a/app/actions/createChannel.ts b/app/actions/createChannel.ts new file mode 100644 index 000000000..d98fd8202 --- /dev/null +++ b/app/actions/createChannel.ts @@ -0,0 +1,42 @@ +import { Action } from 'redux'; + +import { CREATE_CHANNEL } from './actionsTypes'; + +// TODO FIX DATA VALUE + +interface ICreateChannelRequest extends Action { + data: any; +} + +interface ICreateChannelSuccess extends Action { + data: any; +} + +interface ICreateChannelFailure extends Action { + err: any; + isTeam: boolean; +} + +export type TActionCreateApp = ICreateChannelRequest & ICreateChannelSuccess & ICreateChannelFailure; + +export function createChannelRequest(data: any): ICreateChannelRequest { + return { + type: CREATE_CHANNEL.REQUEST, + data + }; +} + +export function createChannelSuccess(data: any): ICreateChannelSuccess { + return { + type: CREATE_CHANNEL.SUCCESS, + data + }; +} + +export function createChannelFailure(err: any, isTeam: boolean): ICreateChannelFailure { + return { + type: CREATE_CHANNEL.FAILURE, + err, + isTeam + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index 212e304bb..da262bf52 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,11 +1,13 @@ import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionApp } from '../../actions/app'; +import { TActionCreateApp } from '../../actions/createChannel'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; import { IApp } from '../../reducers/app'; import { IConnect } from '../../reducers/connect'; +import { ICreateChannel } from '../../reducers/createChannel'; export interface IApplicationState { settings: any; @@ -13,7 +15,7 @@ export interface IApplicationState { meteor: IConnect; server: any; selectedUsers: ISelectedUsers; - createChannel: any; + createChannel: ICreateChannel; app: IApp; room: any; rooms: any; @@ -31,4 +33,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionApp; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionApp & TActionCreateApp; diff --git a/app/reducers/createChannel.test.ts b/app/reducers/createChannel.test.ts new file mode 100644 index 000000000..403da0dad --- /dev/null +++ b/app/reducers/createChannel.test.ts @@ -0,0 +1,33 @@ +import { createChannelRequest, createChannelSuccess, createChannelFailure } from '../actions/createChannel'; +import { initialState } from './createChannel'; +import { mockedStore } from './mockedStore'; + +describe('test reducer', () => { + it('should return initial state', () => { + const { createChannel } = mockedStore.getState(); + expect(createChannel).toEqual(initialState); + }); + + it('should return correct createChannel state after dispatch createChannelRequest action', () => { + mockedStore.dispatch(createChannelRequest({})); + const { createChannel } = mockedStore.getState(); + expect(createChannel).toEqual({ isFetching: true, failure: false, error: {}, result: {} }); + }); + + it('should return correct createChannel state after dispatch createChannelSuccess action', () => { + mockedStore.dispatch(createChannelSuccess({ data: true })); + const { createChannel } = mockedStore.getState(); + expect(createChannel).toEqual({ isFetching: false, failure: false, result: { data: true }, error: {} }); + }); + + it('should return correct createChannel state after dispatch createChannelFailure action', () => { + mockedStore.dispatch(createChannelFailure({ err: true }, true)); + const { createChannel } = mockedStore.getState(); + expect(createChannel).toEqual({ + isFetching: false, + failure: true, + result: { data: true }, + error: { err: true } + }); + }); +}); diff --git a/app/reducers/createChannel.js b/app/reducers/createChannel.ts similarity index 62% rename from app/reducers/createChannel.js rename to app/reducers/createChannel.ts index 60e13f007..737bcd1fc 100644 --- a/app/reducers/createChannel.js +++ b/app/reducers/createChannel.ts @@ -1,13 +1,21 @@ +import { TApplicationActions } from '../definitions'; import { CREATE_CHANNEL } from '../actions/actionsTypes'; -const initialState = { +export interface ICreateChannel { + isFetching: boolean; + failure: boolean; + result: Record; + error: Record; +} + +export const initialState: ICreateChannel = { isFetching: false, failure: false, result: {}, error: {} }; -export default function (state = initialState, action) { +export default function (state = initialState, action: TApplicationActions): ICreateChannel { switch (action.type) { case CREATE_CHANNEL.REQUEST: return {