chore: migrate createDiscussion to ts and add tests

This commit is contained in:
GleidsonDaniel 2022-01-10 18:17:47 -03:00
parent f1ac7ce77c
commit c25c34b725
4 changed files with 81 additions and 24 deletions

View File

@ -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
};
}

View File

@ -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
};
}

View File

@ -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 }
});
});
});

View File

@ -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<string, string>;
error: Record<string, string>;
}
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 {