Skip to content

Commit a1734e1

Browse files
committed
settings: Clear all PerAccountSettingsState on RESET_ACCOUNT_DATA
Really an instance of zulip#4446 that I forgot about in zulip#5606, oops. Related: zulip#4446
1 parent 46d453d commit a1734e1

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/settings/__tests__/settingsReducer-test.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ import {
1010
import type { InitialData } from '../../api/initialDataTypes';
1111
import type { UserSettings } from '../../api/modelTypes';
1212
import { EventTypes } from '../../api/eventTypes';
13-
import settingsReducer from '../settingsReducer';
13+
import settingsReducer, { initialPerAccountSettingsState } from '../settingsReducer';
1414
import * as eg from '../../__tests__/lib/exampleData';
1515

1616
describe('settingsReducer', () => {
1717
const baseState = eg.baseReduxState.settings;
1818

19+
describe('RESET_ACCOUNT_DATA', () => {
20+
test('resets per-account state without touching global state', () => {
21+
const prevState = [
22+
// per-account
23+
eg.mkActionRegisterComplete({
24+
user_settings: {
25+
/* $FlowIgnore[incompatible-cast] - testing modern servers, which
26+
send user_settings. */
27+
...(eg.action.register_complete.data.user_settings: $NonMaybeType<
28+
InitialData['user_settings'],
29+
>),
30+
enable_offline_push_notifications: false,
31+
enable_online_push_notifications: false,
32+
enable_stream_push_notifications: true,
33+
display_emoji_reaction_users: true,
34+
},
35+
}),
36+
37+
// global
38+
{ type: SET_GLOBAL_SETTINGS, update: { theme: 'night' } },
39+
{ type: SET_GLOBAL_SETTINGS, update: { language: 'fr' } },
40+
].reduce(settingsReducer, eg.baseReduxState.settings);
41+
expect(settingsReducer(prevState, eg.action.reset_account_data)).toEqual({
42+
...prevState,
43+
...initialPerAccountSettingsState,
44+
});
45+
});
46+
});
47+
1948
describe('REGISTER_COMPLETE', () => {
2049
test('changes value of all notification settings (legacy, without user_settings)', () => {
2150
const prevState = deepFreeze({

src/settings/settingsReducer.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* @flow strict-local */
2+
import type { GlobalSettingsState, PerAccountSettingsState } from '../reduxTypes';
23
import type { SettingsState, Action } from '../types';
34
import {
5+
RESET_ACCOUNT_DATA,
46
SET_GLOBAL_SETTINGS,
57
REGISTER_COMPLETE,
68
EVENT_UPDATE_GLOBAL_NOTIFICATIONS_SETTINGS,
@@ -9,30 +11,33 @@ import {
911
import { EventTypes } from '../api/eventTypes';
1012
import { ensureUnreachable } from '../types';
1113

12-
const initialState: SettingsState = {
13-
//
14-
// GlobalSettingsState
15-
//
16-
14+
const initialGlobalSettingsState: $Exact<GlobalSettingsState> = {
1715
language: 'en',
1816
theme: 'default',
1917
browser: 'default',
2018
experimentalFeaturesEnabled: false,
2119
markMessagesReadOnScroll: 'always',
20+
};
2221

23-
//
24-
// PerAccountSettingsState
25-
//
26-
22+
/** PRIVATE; exported only for tests. */
23+
export const initialPerAccountSettingsState: $Exact<PerAccountSettingsState> = {
2724
offlineNotification: true,
2825
onlineNotification: true,
2926
streamNotification: false,
3027
displayEmojiReactionUsers: false,
3128
};
3229

30+
const initialState: SettingsState = {
31+
...initialGlobalSettingsState,
32+
...initialPerAccountSettingsState,
33+
};
34+
3335
// eslint-disable-next-line default-param-last
3436
export default (state: SettingsState = initialState, action: Action): SettingsState => {
3537
switch (action.type) {
38+
case RESET_ACCOUNT_DATA:
39+
return { ...state, ...initialPerAccountSettingsState };
40+
3641
case REGISTER_COMPLETE: {
3742
const { data } = action;
3843

0 commit comments

Comments
 (0)