Skip to content

Commit

Permalink
session [nfc]: Refactor to use RESET_ACCOUNT_DATA
Browse files Browse the repository at this point in the history
Related: #4446
  • Loading branch information
chrisbobbe committed Dec 14, 2022
1 parent 8bd29e1 commit bce4575
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
39 changes: 15 additions & 24 deletions src/session/__tests__/sessionReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import deepFreeze from 'deep-freeze';

import {
DEAD_QUEUE,
LOGOUT,
APP_ONLINE,
REGISTER_ABORT,
APP_ORIENTATION,
Expand All @@ -19,18 +18,22 @@ import * as eg from '../../__tests__/lib/exampleData';
describe('sessionReducer', () => {
const baseState = eg.baseReduxState.session;

test('ACCOUNT_SWITCH', () => {
const state = deepFreeze({
...baseState,
loading: true,
});
const newState = sessionReducer(state, eg.action.account_switch);
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
});
describe('RESET_ACCOUNT_DATA', () => {
test('resets per-account state without touching global state', () => {
const prevState = [
// per-account
eg.action.register_complete,
{ type: DISMISS_SERVER_COMPAT_NOTICE },

test('LOGIN_SUCCESS', () => {
const newState = sessionReducer(baseState, eg.action.login_success);
expect(newState).toEqual({ ...baseState, ...initialPerAccountSessionState });
// global
{ type: GOT_PUSH_TOKEN, pushToken: '456' },
{ type: APP_ORIENTATION, orientation: 'LANDSCAPE' },
].reduce(sessionReducer, eg.baseReduxState.session);
expect(sessionReducer(prevState, eg.action.reset_account_data)).toEqual({
...prevState,
...initialPerAccountSessionState,
});
});
});

test('DEAD_QUEUE', () => {
Expand All @@ -39,18 +42,6 @@ describe('sessionReducer', () => {
expect(newState).toEqual({ ...baseState, loading: false });
});

test('LOGOUT', () => {
const state = deepFreeze({
...baseState,
loading: true,
});
const newState = sessionReducer(state, deepFreeze({ type: LOGOUT }));
expect(newState).toEqual({
...baseState,
...initialPerAccountSessionState,
});
});

test('REGISTER_COMPLETE', () => {
const state = deepFreeze({ ...baseState, loading: true });
const action = eg.mkActionRegisterComplete({ queue_id: '100' });
Expand Down
42 changes: 7 additions & 35 deletions src/session/sessionReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import type { Debug, Orientation, Action } from '../types';
import {
REHYDRATE,
DEAD_QUEUE,
LOGIN_SUCCESS,
RESET_ACCOUNT_DATA,
APP_ONLINE,
ACCOUNT_SWITCH,
REGISTER_START,
REGISTER_ABORT,
REGISTER_COMPLETE,
APP_ORIENTATION,
TOGGLE_OUTBOX_SENDING,
DEBUG_FLAG_TOGGLE,
GOT_PUSH_TOKEN,
LOGOUT,
DISMISS_SERVER_COMPAT_NOTICE,
} from '../actionConstants';

Expand Down Expand Up @@ -154,41 +152,15 @@ export default (state: SessionState = initialState, action: Action): SessionStat
eventQueueId: null,
};

case LOGIN_SUCCESS:
case RESET_ACCOUNT_DATA:
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// We're about to request a new event queue; no use hanging on to
// any old one we might have.
eventQueueId: null,
};

case LOGOUT:
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// Stop polling this event queue.
eventQueueId: null,
};

case ACCOUNT_SWITCH:
return {
...state,
loading: false,
outboxSending: false,
hasDismissedServerCompatNotice: false,

// Stop polling this event queue. (We'll request a new one soon,
// for the new account.)
// TODO(#5005): Keep polling on accounts other than the active
// account.
eventQueueId: null,
// Clear per-account session state. Importantly, stop polling on the
// account's current event queue if we had one. In the polling loop,
// after each server response, we check if we've dropped the queue
// ID from this state and break out if so.
...initialPerAccountSessionState,
};

case REHYDRATE:
Expand Down

0 comments on commit bce4575

Please sign in to comment.