chore: migrate createChannel to ts and add tests

This commit is contained in:
GleidsonDaniel 2022-01-10 17:47:54 -03:00
parent 6bc0e82fdc
commit f19787f35a
5 changed files with 89 additions and 27 deletions

View File

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

View File

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

View File

@ -1,11 +1,13 @@
import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionSelectedUsers } from '../../actions/selectedUsers';
import { TActionActiveUsers } from '../../actions/activeUsers'; import { TActionActiveUsers } from '../../actions/activeUsers';
import { TActionApp } from '../../actions/app'; import { TActionApp } from '../../actions/app';
import { TActionCreateApp } from '../../actions/createChannel';
// REDUCERS // REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers'; import { IActiveUsers } from '../../reducers/activeUsers';
import { ISelectedUsers } from '../../reducers/selectedUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers';
import { IApp } from '../../reducers/app'; import { IApp } from '../../reducers/app';
import { IConnect } from '../../reducers/connect'; import { IConnect } from '../../reducers/connect';
import { ICreateChannel } from '../../reducers/createChannel';
export interface IApplicationState { export interface IApplicationState {
settings: any; settings: any;
@ -13,7 +15,7 @@ export interface IApplicationState {
meteor: IConnect; meteor: IConnect;
server: any; server: any;
selectedUsers: ISelectedUsers; selectedUsers: ISelectedUsers;
createChannel: any; createChannel: ICreateChannel;
app: IApp; app: IApp;
room: any; room: any;
rooms: any; rooms: any;
@ -31,4 +33,4 @@ export interface IApplicationState {
roles: any; roles: any;
} }
export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionApp; export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionApp & TActionCreateApp;

View File

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

View File

@ -1,13 +1,21 @@
import { TApplicationActions } from '../definitions';
import { CREATE_CHANNEL } from '../actions/actionsTypes'; import { CREATE_CHANNEL } from '../actions/actionsTypes';
const initialState = { export interface ICreateChannel {
isFetching: boolean;
failure: boolean;
result: Record<string, string>;
error: Record<string, string>;
}
export const initialState: ICreateChannel = {
isFetching: false, isFetching: false,
failure: false, failure: false,
result: {}, result: {},
error: {} error: {}
}; };
export default function (state = initialState, action) { export default function (state = initialState, action: TApplicationActions): ICreateChannel {
switch (action.type) { switch (action.type) {
case CREATE_CHANNEL.REQUEST: case CREATE_CHANNEL.REQUEST:
return { return {