From c25c34b725304d6cad8bce3709f7c7cd1a225c8b Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Mon, 10 Jan 2022 18:17:47 -0300 Subject: [PATCH] chore: migrate createDiscussion to ts and add tests --- app/actions/createDiscussion.js | 22 ----------- app/actions/createDiscussion.ts | 38 +++++++++++++++++++ app/reducers/createDiscussion.test.ts | 33 ++++++++++++++++ ...reateDiscussion.js => createDiscussion.ts} | 12 +++++- 4 files changed, 81 insertions(+), 24 deletions(-) delete mode 100644 app/actions/createDiscussion.js create mode 100644 app/actions/createDiscussion.ts create mode 100644 app/reducers/createDiscussion.test.ts rename app/reducers/{createDiscussion.js => createDiscussion.ts} (62%) diff --git a/app/actions/createDiscussion.js b/app/actions/createDiscussion.js deleted file mode 100644 index 5b6faa851..000000000 --- a/app/actions/createDiscussion.js +++ /dev/null @@ -1,22 +0,0 @@ -import * as types from './actionsTypes'; - -export function createDiscussionRequest(data) { - return { - type: types.CREATE_DISCUSSION.REQUEST, - data - }; -} - -export function createDiscussionSuccess(data) { - return { - type: types.CREATE_DISCUSSION.SUCCESS, - data - }; -} - -export function createDiscussionFailure(err) { - return { - type: types.CREATE_DISCUSSION.FAILURE, - err - }; -} diff --git a/app/actions/createDiscussion.ts b/app/actions/createDiscussion.ts new file mode 100644 index 000000000..bf64baaa6 --- /dev/null +++ b/app/actions/createDiscussion.ts @@ -0,0 +1,38 @@ +import { Action } from 'redux'; + +import { CREATE_DISCUSSION } from './actionsTypes'; + +interface ICreateDiscussionRequest extends Action { + data: any; +} + +interface ICreateDiscussionSuccess extends Action { + data: any; +} + +interface ICreateDiscussionFailure extends Action { + err: any; +} + +export type TActionCreateDiscussion = ICreateDiscussionRequest & ICreateDiscussionSuccess & ICreateDiscussionFailure; + +export function createDiscussionRequest(data: any): ICreateDiscussionRequest { + return { + type: CREATE_DISCUSSION.REQUEST, + data + }; +} + +export function createDiscussionSuccess(data: any): ICreateDiscussionSuccess { + return { + type: CREATE_DISCUSSION.SUCCESS, + data + }; +} + +export function createDiscussionFailure(err: any): ICreateDiscussionFailure { + return { + type: CREATE_DISCUSSION.FAILURE, + err + }; +} diff --git a/app/reducers/createDiscussion.test.ts b/app/reducers/createDiscussion.test.ts new file mode 100644 index 000000000..0291cbb37 --- /dev/null +++ b/app/reducers/createDiscussion.test.ts @@ -0,0 +1,33 @@ +import { createDiscussionRequest, createDiscussionSuccess, createDiscussionFailure } from '../actions/createDiscussion'; +import { initialState } from './createDiscussion'; +import { mockedStore } from './mockedStore'; + +describe('test reducer', () => { + it('should return initial state', () => { + const { createDiscussion } = mockedStore.getState(); + expect(createDiscussion).toEqual(initialState); + }); + + it('should return correct createDiscussion state after dispatch createDiscussionRequest action', () => { + mockedStore.dispatch(createDiscussionRequest({})); + const { createDiscussion } = mockedStore.getState(); + expect(createDiscussion).toEqual({ isFetching: true, failure: false, error: {}, result: {} }); + }); + + it('should return correct createDiscussion state after dispatch createDiscussionSuccess action', () => { + mockedStore.dispatch(createDiscussionSuccess({ data: true })); + const { createDiscussion } = mockedStore.getState(); + expect(createDiscussion).toEqual({ isFetching: false, failure: false, result: { data: true }, error: {} }); + }); + + it('should return correct createDiscussion state after dispatch createDiscussionFailure action', () => { + mockedStore.dispatch(createDiscussionFailure({ err: true })); + const { createDiscussion } = mockedStore.getState(); + expect(createDiscussion).toEqual({ + isFetching: false, + failure: true, + result: { data: true }, + error: { err: true } + }); + }); +}); diff --git a/app/reducers/createDiscussion.js b/app/reducers/createDiscussion.ts similarity index 62% rename from app/reducers/createDiscussion.js rename to app/reducers/createDiscussion.ts index 04081d157..4d0481540 100644 --- a/app/reducers/createDiscussion.js +++ b/app/reducers/createDiscussion.ts @@ -1,13 +1,21 @@ +import { TApplicationActions } from '../definitions'; import { CREATE_DISCUSSION } from '../actions/actionsTypes'; -const initialState = { +export interface ICreateDiscussion { + isFetching: boolean; + failure: boolean; + result: Record; + error: Record; +} + +export const initialState: ICreateDiscussion = { isFetching: false, failure: false, result: {}, error: {} }; -export default function (state = initialState, action) { +export default function (state = initialState, action: TApplicationActions): ICreateDiscussion { switch (action.type) { case CREATE_DISCUSSION.REQUEST: return {