Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3b97c34
chore: migrate activeUsers reducer and action to TS
dnlsilva Dec 21, 2021
34e6ece
chore: init types folder and set redux and BaseScreen interface
dnlsilva Dec 21, 2021
9d7450b
chore: remove mapDispatchToProps to use dispatch prop and clear some …
dnlsilva Dec 21, 2021
003f9fc
chore: type selectedUsers action and reducer and improvement in the c…
dnlsilva Dec 21, 2021
010676a
chore: move IUser to base types
dnlsilva Dec 21, 2021
dc83f76
chore: move state props to ISelectedUsersViewProps
dnlsilva Dec 21, 2021
f169f6e
chore: create mocketStore
dnlsilva Dec 21, 2021
69f2743
chore: remove applyAppStateMiddleware
dnlsilva Dec 21, 2021
b80c063
test: create activeUser and selectedUser tests
dnlsilva Dec 21, 2021
b5bda75
test: add more selectedUsers tests
dnlsilva Dec 22, 2021
b56f604
chore: fix action type
dnlsilva Dec 22, 2021
ff0925c
chore: move types to definition folder and fix imports
dnlsilva Dec 22, 2021
ef0d2bf
chore: remove unused const
dnlsilva Dec 22, 2021
d12b28b
chore: migrate redux tests to reducer folder and add eslint jest plugin
dnlsilva Dec 22, 2021
f0977dd
chore: exprot initial state and then import on tests
dnlsilva Dec 22, 2021
bad5643
chore: move interfaces to reducer and import on screen
dnlsilva Dec 22, 2021
928fac1
chore: set eslint-plugin-jest version to 24.7.0
dnlsilva Dec 23, 2021
c471195
chore: fix IUser import
dnlsilva Dec 23, 2021
48e4640
chore: update interfaces and types names
dnlsilva Dec 29, 2021
0cf9765
chore: update definitions
dnlsilva Dec 29, 2021
bc92a4f
chore: update IBaseScreen definitions
dnlsilva Dec 29, 2021
4fe8dab
Merge branch 'develop' into chore/poc-redux-ts
diegolmello Jan 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __mocks__/mockedStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyMiddleware, compose, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';

import reducers from '../app/reducers';

const enhancers = compose(applyMiddleware(createSagaMiddleware()));
export const mockedStore = createStore(reducers, enhancers);
16 changes: 16 additions & 0 deletions __tests__/redux/activeUsers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { setActiveUsers } from '../../app/actions/activeUsers';
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
import { IActiveUsers } from '../../app/reducers/activeUsers';
import { mockedStore } from '../../__mocks__/mockedStore';

describe('test reducer', () => {
it('should return {} as initial state', async () => {
const state = mockedStore.getState().activeUsers;
expect(state).toEqual({});
});
it('should return modified store after action', async () => {
const activeUsers: IActiveUsers = { any: { status: 'online', statusText: 'any' } };
mockedStore.dispatch(setActiveUsers(activeUsers));
const state = mockedStore.getState().activeUsers;
expect(state).toEqual({ ...activeUsers });
});
});
40 changes: 40 additions & 0 deletions __tests__/redux/selectedUsers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { addUser, reset, setLoading, removeUser } from '../../app/actions/selectedUsers';
import { mockedStore } from '../../__mocks__/mockedStore';

describe('test selectedUsers reducer', () => {
const initialState = {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
users: [],
loading: false
};

it('should return initial state', async () => {
const state = mockedStore.getState().selectedUsers;
expect(state).toEqual(initialState);
});

it('should return modified store after addUser', async () => {
const user = { _id: 'xxx', name: 'xxx', fname: 'xxx' };
mockedStore.dispatch(addUser(user));
const state = mockedStore.getState().selectedUsers.users;
expect(state).toEqual([user]);
});

it('should return empty store after remove user', async () => {
const user = { _id: 'xxx', name: 'xxx', fname: 'xxx' };
mockedStore.dispatch(removeUser(user));
const state = mockedStore.getState().selectedUsers.users;
expect(state).toEqual([]);
});

it('should return initialState after reset', async () => {
mockedStore.dispatch(reset());
const state = mockedStore.getState().selectedUsers;
expect(state).toEqual(initialState);
});

it('should return loading after call action', async () => {
mockedStore.dispatch(setLoading(true));
const state = mockedStore.getState().selectedUsers.loading;
expect(state).toEqual(true);
});
});
4 changes: 2 additions & 2 deletions app/actions/actionsTypes.js → app/actions/actionsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const REQUEST = 'REQUEST';
const SUCCESS = 'SUCCESS';
const FAILURE = 'FAILURE';
const defaultTypes = [REQUEST, SUCCESS, FAILURE];
function createRequestTypes(base, types = defaultTypes) {
const res = {};
function createRequestTypes(base = {}, types = defaultTypes): Record<any, any> {
const res: Record<any, any> = {};
types.forEach(type => (res[type] = `${base}_${type}`));
return res;
}
Expand Down
8 changes: 0 additions & 8 deletions app/actions/activeUsers.js

This file was deleted.

15 changes: 15 additions & 0 deletions app/actions/activeUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Action } from 'redux';

import { IActiveUsers } from '../reducers/activeUsers';
import { SET_ACTIVE_USERS } from './actionsTypes';

export interface SetActiveUsers extends Action {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
activeUsers: IActiveUsers;
}

export type IActionActiveUsers = SetActiveUsers;
Comment thread
dnlsilva marked this conversation as resolved.
Outdated

export const setActiveUsers = (activeUsers: IActiveUsers): SetActiveUsers => ({
type: SET_ACTIVE_USERS,
activeUsers
});
28 changes: 0 additions & 28 deletions app/actions/selectedUsers.js

This file was deleted.

43 changes: 43 additions & 0 deletions app/actions/selectedUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Action } from 'redux';

import { IUser } from '../definitions';
import * as types from './actionsTypes';

type User = {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
user: IUser;
};

type IAction = Action & User;
Comment thread
dnlsilva marked this conversation as resolved.
Outdated

interface SetLoading extends Action {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
loading: boolean;
}

export type IActionSelectedUsers = IAction & SetLoading;
Comment thread
dnlsilva marked this conversation as resolved.
Outdated

export function addUser(user: IUser): IAction {
return {
type: types.SELECTED_USERS.ADD_USER,
user
};
}

export function removeUser(user: IUser): IAction {
return {
type: types.SELECTED_USERS.REMOVE_USER,
user
};
}

export function reset(): Action {
return {
type: types.SELECTED_USERS.RESET
};
}

export function setLoading(loading: boolean): SetLoading {
return {
type: types.SELECTED_USERS.SET_LOADING,
loading
};
}
25 changes: 25 additions & 0 deletions app/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { StackNavigationProp } from '@react-navigation/stack';
import { Dispatch } from 'redux';

export interface BaseScreen {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
navigation: StackNavigationProp<any>;
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
dispatch: Dispatch;
theme: string;
Comment thread
dnlsilva marked this conversation as resolved.
}

export interface IUser {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
_id: string;
name: string;
fname: string;
search?: boolean;
// username is used when is from searching
username?: string;
}

type UserStatus = 'online' | 'offline';
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
export interface ActiveUser {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
status: UserStatus;
statusText?: string;
}

export * from './redux';
31 changes: 31 additions & 0 deletions app/definitions/redux/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IActionSelectedUsers } from '../../actions/selectedUsers';
import { IActionActiveUsers } from '../../actions/activeUsers';
// REDUCERS
import { IActiveUsers } from '../../reducers/activeUsers';
import { ISelectedUsers } from '../../reducers/selectedUsers';

export interface ApplicationState {
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
settings: any;
login: any;
meteor: any;
server: any;
selectedUsers: ISelectedUsers;
createChannel: any;
app: any;
room: any;
rooms: any;
sortPreferences: any;
share: any;
customEmojis: any;
activeUsers: IActiveUsers;
usersTyping: any;
inviteLinks: any;
createDiscussion: any;
inquiry: any;
enterpriseModules: any;
encryption: any;
permissions: any;
roles: any;
}

export type ApplicationActions = IActionActiveUsers & IActionSelectedUsers;
9 changes: 7 additions & 2 deletions app/reducers/activeUsers.js → app/reducers/activeUsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ActiveUser, ApplicationActions } from '../definitions';
import { SET_ACTIVE_USERS } from '../actions/actionsTypes';

const initialState = {};
export interface IActiveUsers {
[key: string]: ActiveUser;
}

const initialState: IActiveUsers = {};

export default function activeUsers(state = initialState, action) {
export default function activeUsers(state = initialState, action: ApplicationActions): IActiveUsers {
switch (action.type) {
case SET_ACTIVE_USERS:
return {
Expand Down
10 changes: 8 additions & 2 deletions app/reducers/selectedUsers.js → app/reducers/selectedUsers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { IUser, ApplicationActions } from '../definitions';
import { SELECTED_USERS } from '../actions/actionsTypes';

const initialState = {
export interface ISelectedUsers {
users: IUser[];
loading: boolean;
}

const initialState: ISelectedUsers = {
users: [],
loading: false
};

export default function (state = initialState, action) {
export default function (state = initialState, action: ApplicationActions): ISelectedUsers {
switch (action.type) {
case SELECTED_USERS.ADD_USER:
return {
Expand Down
Loading