Skip to content

Commit

Permalink
UsersReducers
Browse files Browse the repository at this point in the history
Fixes #zulip#3397
Part of ...???
  • Loading branch information
borisyankov committed Mar 15, 2019
1 parent f176acf commit ec06280
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 5 deletions.
135 changes: 134 additions & 1 deletion src/users/__tests__/usersReducers-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import deepFreeze from 'deep-freeze';

import { REALM_INIT, EVENT_USER_ADD, ACCOUNT_SWITCH } from '../../actionConstants';
import {
REALM_INIT,
EVENT_USER_ADD,
ACCOUNT_SWITCH,
EVENT_USER_UPDATE,
} from '../../actionConstants';
import usersReducers from '../usersReducers';

describe('usersReducers', () => {
Expand Down Expand Up @@ -96,6 +101,134 @@ describe('usersReducers', () => {
});
});

describe('EVENT_USER_UPDATE', () => {
test('updating non existing user does not mutate state', () => {
const initialState = deepFreeze([
{
user_id: 1,
full_name: 'Some Guy',
},
]);
const action = deepFreeze({
type: EVENT_USER_UPDATE,
person: {
user_id: 2,
full_name: 'New Name',
},
});

const actualState = usersReducers(initialState, action);

expect(actualState).toEqual(initialState);
});

test('updating name', () => {
const initialState = deepFreeze([
{
user_id: 1,
full_name: 'Some Guy',
},
]);
const action = deepFreeze({
type: EVENT_USER_UPDATE,
person: {
user_id: 1,
full_name: 'New Name',
},
});
const expectedState = [
{
user_id: 1,
full_name: 'New Name',
},
];

const actualState = usersReducers(initialState, action);

expect(actualState).toEqual(expectedState);
});

test('updating avatar', () => {
const initialState = deepFreeze([
{
user_id: 1,
full_name: 'Some Guy',
avatar_url: 'https://example.com/avatar.png',
},
]);
const action = deepFreeze({
type: EVENT_USER_UPDATE,
person: {
user_id: 1,
avatar_source: 'G',
avatar_url: 'https://example.com/new-avatar.png',
avatar_url_medium: 'https://example.com/new-avatar-medium.png',
},
});
const expectedState = [
{
user_id: 1,
full_name: 'Some Guy',
avatar_url: 'https://example.com/new-avatar.png',
},
];

const actualState = usersReducers(initialState, action);

expect(actualState).toEqual(expectedState);
});

test('updating custom profile field', () => {
const initialState = deepFreeze([
{
user_id: 1,
full_name: 'Some Guy',
profile_data: {
1: {
value: 'CA',
rendered_value: null,
},
2: {
value: 'Blue',
rendered_value: null,
},
},
},
]);
const action = deepFreeze({
type: EVENT_USER_UPDATE,
person: {
user_id: 1,
custom_profile_field: {
id: 2,
value: 'Red',
rendered_value: null,
},
},
});
const expectedState = [
{
user_id: 1,
full_name: 'Some Guy',
profile_data: {
1: {
value: 'CA',
rendered_value: null,
},
2: {
value: 'Red',
rendered_value: null,
},
},
},
];

const actualState = usersReducers(initialState, action);

expect(actualState).toEqual(expectedState);
});
});

describe('ACCOUNT_SWITCH', () => {
test('resets state to initial state', () => {
const initialState = deepFreeze([
Expand Down
24 changes: 20 additions & 4 deletions src/users/usersReducers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow strict-local */
import type { UsersState, Action } from '../types';
import isEqual from 'lodash.isequal';

import type { User, UsersState, Action } from '../types';
import {
LOGOUT,
LOGIN_SUCCESS,
Expand All @@ -10,6 +12,7 @@ import {
EVENT_USER_UPDATE,
} from '../actionConstants';
import { NULL_ARRAY } from '../nullObjects';
import { updateUser } from './userHelpers';

const initialState: UsersState = NULL_ARRAY;

Expand All @@ -29,9 +32,22 @@ export default (state: UsersState = initialState, action: Action): UsersState =>
case EVENT_USER_REMOVE:
return state; // TODO

case EVENT_USER_UPDATE:
console.log('UPDATE!!!', action);
return state; // TODO
case EVENT_USER_UPDATE: {
const userIndex = state.findIndex(x => x.user_id === action.person.user_id);
const oldUser: User = state[userIndex];

if (userIndex === -1) {
return state;
}

const updatedUser = updateUser(oldUser, action.person);

if (isEqual(updatedUser, oldUser)) {
return state;
}

return [...state.slice(0, userIndex), updatedUser, ...state.slice(userIndex + 1)];
}

default:
return state;
Expand Down

0 comments on commit ec06280

Please sign in to comment.