Skip to content

Commit 717a62e

Browse files
borisyankovneerajwahi
authored andcommitted
Flatten userlist store structure. Add several unit tests for userListReducers
1 parent 4a3836e commit 717a62e

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

src/common/__tests__/__snapshots__/ZulipButton-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ exports[`test renders correctly 1`] = `
55
undefined,
66
Object {
77
"alignItems": "center",
8+
"alignSelf": "stretch",
89
"borderRadius": 5,
9-
"flex": 1,
1010
"height": 44,
1111
"justifyContent": "center",
1212
"marginBottom": 10

src/userlist/UserListContainer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class UserListContainer extends Component {
2121
}
2222

2323
const mapStateToProps = (state) => ({
24-
users: state.userlist.get('users'),
25-
presence: state.userlist.get('presence'),
24+
users: state.userlist,
2625
});
2726

2827
const mapDispatchToProps = (dispatch, ownProps) =>

src/userlist/UsersCard.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default class UsersCard extends Component {
3737
drawer: () => null,
3838
};
3939

40-
4140
constructor(props: Props) {
4241
super(props);
4342
this.state = {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fromJS } from 'immutable';
2+
import { GET_USER_RESPONSE } from '../userListActions';
3+
import userListReducers from '../userListReducers';
4+
5+
it('when unrecognized action, no previous state, returns initial state, does not throw', () => {
6+
const newState = userListReducers(undefined, {});
7+
expect(newState).toBeDefined();
8+
});
9+
10+
it('on unrecognized action, returns input state unchanged', () => {
11+
const prevState = { hello: 'world' };
12+
const newState = userListReducers(prevState, {});
13+
expect(newState).toEqual(prevState);
14+
});
15+
16+
it('on GET_USER_RESPONSE stores user data', () => {
17+
const users = [{ full_name: 'user1' }, { full_name: 'user2' }];
18+
const newState = userListReducers(fromJS([]), { type: GET_USER_RESPONSE, users });
19+
expect(newState).toEqual(users);
20+
});

src/userlist/userListActions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Auth, focusPing, getUsers } from '../api/apiClient';
22

33
export const PRESENCE_RESPONSE = 'PRESENCE_RESPONSE';
44
export const GET_USER_RESPONSE = 'GET_USER_RESPONSE';
5-
export const USER_FILTER_CHANGE = 'USER_FILTER_CHANGE';
65

76
export const sendFocusPing = (auth: Auth, hasFocus: boolean, newUserInput: boolean) =>
87
async (dispatch) => {

src/userlist/userListReducers.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import {
77
GET_USER_RESPONSE,
88
PRESENCE_RESPONSE,
9-
USER_FILTER_CHANGE,
109
} from './userListActions';
1110

1211
type Presence = {
@@ -31,10 +30,7 @@ type PresenceMap = {
3130
// presence: PresenceMap,
3231
// }
3332

34-
const initialState = fromJS({
35-
filter: '',
36-
users: [],
37-
});
33+
const initialState = fromJS([]);
3834

3935
const activityFromPresence = (presence: PresenceMap): UserStatus =>
4036
'active';
@@ -46,28 +42,24 @@ const reducer = (state = initialState, action) => {
4642
Object.keys(action.presence).forEach(x => {
4743
const status = activityFromPresence(action.presence[x]);
4844
const user = state.find(u => u.email === x);
49-
newState = state.setIn([user, { status }]);
45+
newState = state.set({ status });
5046
});
5147
return newState;
5248
}
53-
case GET_USER_RESPONSE:
54-
return state.merge({
55-
users: fromJS(action.users.map(x => ({
56-
email: x.email,
57-
fullName: x.full_name,
58-
avatarUrl: x.avatar_url,
59-
isActive: x.is_active,
60-
isAdmin: x.is_admin,
61-
isBot: x.is_bot,
62-
}))),
63-
});
49+
case GET_USER_RESPONSE: {
50+
const users = action.users.map(x => ({
51+
email: x.email,
52+
fullName: x.full_name,
53+
avatarUrl: x.avatar_url,
54+
isActive: x.is_active,
55+
isAdmin: x.is_admin,
56+
isBot: x.is_bot,
57+
}));
58+
return state.merge(fromJS(users));
59+
}
6460
case EVENT_PRESENCE:
6561
// TODO
6662
return state;
67-
case USER_FILTER_CHANGE:
68-
return state.merge({
69-
filter: action.filter,
70-
});
7163
default:
7264
return state;
7365
}

0 commit comments

Comments
 (0)