From 400673e2316f8183c07e98bb8b4777aae8830b86 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 14 Jan 2022 14:19:08 -0300 Subject: [PATCH] chore: migrate usersTyping to typescript --- app/actions/usersTyping.js | 21 ------------- app/actions/usersTyping.ts | 29 ++++++++++++++++++ app/definitions/redux/index.ts | 3 +- app/reducers/usersTyping.test.ts | 30 +++++++++++++++++++ .../{usersTyping.js => usersTyping.ts} | 7 +++-- 5 files changed, 66 insertions(+), 24 deletions(-) delete mode 100644 app/actions/usersTyping.js create mode 100644 app/actions/usersTyping.ts create mode 100644 app/reducers/usersTyping.test.ts rename app/reducers/{usersTyping.js => usersTyping.ts} (72%) diff --git a/app/actions/usersTyping.js b/app/actions/usersTyping.js deleted file mode 100644 index fb8f5980..00000000 --- a/app/actions/usersTyping.js +++ /dev/null @@ -1,21 +0,0 @@ -import { USERS_TYPING } from './actionsTypes'; - -export function addUserTyping(username) { - return { - type: USERS_TYPING.ADD, - username - }; -} - -export function removeUserTyping(username) { - return { - type: USERS_TYPING.REMOVE, - username - }; -} - -export function clearUserTyping() { - return { - type: USERS_TYPING.CLEAR - }; -} diff --git a/app/actions/usersTyping.ts b/app/actions/usersTyping.ts new file mode 100644 index 00000000..19077ce6 --- /dev/null +++ b/app/actions/usersTyping.ts @@ -0,0 +1,29 @@ +import { Action } from 'redux'; + +import { USERS_TYPING } from './actionsTypes'; + +export interface IUsersTypingGenericAction extends Action { + username: string; +} + +export type TActionUserTyping = IUsersTypingGenericAction & Action; + +export function addUserTyping(username: string): IUsersTypingGenericAction { + return { + type: USERS_TYPING.ADD, + username + }; +} + +export function removeUserTyping(username: string): IUsersTypingGenericAction { + return { + type: USERS_TYPING.REMOVE, + username + }; +} + +export function clearUserTyping(): Action { + return { + type: USERS_TYPING.CLEAR + }; +} diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index e95763e2..88b7169e 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -3,6 +3,7 @@ import { TActionActiveUsers } from '../../actions/activeUsers'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; +import { TActionUserTyping } from '../../actions/usersTyping'; export interface IApplicationState { settings: any; @@ -28,4 +29,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & TActionUserTyping; diff --git a/app/reducers/usersTyping.test.ts b/app/reducers/usersTyping.test.ts new file mode 100644 index 00000000..26e52788 --- /dev/null +++ b/app/reducers/usersTyping.test.ts @@ -0,0 +1,30 @@ +import { addUserTyping, removeUserTyping, clearUserTyping } from '../actions/usersTyping'; +import { mockedStore } from './mockedStore'; +import { initialState } from './usersTyping'; + +describe('test usersTyping reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().usersTyping; + expect(state).toEqual(initialState); + }); + + it('should return modified store after addUserTyping', () => { + mockedStore.dispatch(addUserTyping('diego')); + mockedStore.dispatch(addUserTyping('carlos')); + mockedStore.dispatch(addUserTyping('maria')); + const state = mockedStore.getState().usersTyping; + expect(state).toEqual(['diego', 'carlos', 'maria']); + }); + + it('should return modified store after removeUserTyping', () => { + mockedStore.dispatch(removeUserTyping('diego')); + const state = mockedStore.getState().usersTyping; + expect(state).toEqual(['carlos', 'maria']); + }); + + it('should return initial state after reset', () => { + mockedStore.dispatch(clearUserTyping()); + const state = mockedStore.getState().usersTyping; + expect(state).toEqual(initialState); + }); +}); diff --git a/app/reducers/usersTyping.js b/app/reducers/usersTyping.ts similarity index 72% rename from app/reducers/usersTyping.js rename to app/reducers/usersTyping.ts index acecab63..ecfbbb77 100644 --- a/app/reducers/usersTyping.js +++ b/app/reducers/usersTyping.ts @@ -1,8 +1,11 @@ import { USERS_TYPING } from '../actions/actionsTypes'; +import { TApplicationActions } from '../definitions'; -const initialState = []; +export type IUsersTyping = string[]; -export default function usersTyping(state = initialState, action) { +export const initialState: IUsersTyping = []; + +export default function usersTyping(state = initialState, action: TApplicationActions): IUsersTyping { switch (action.type) { case USERS_TYPING.ADD: if (state.findIndex(item => item === action.username) === -1) {