diff --git a/lib/reducers/thread-activity-reducer.test.js b/lib/reducers/thread-activity-reducer.test.js index 0488872d7..03dde0c6d 100644 --- a/lib/reducers/thread-activity-reducer.test.js +++ b/lib/reducers/thread-activity-reducer.test.js @@ -1,111 +1,147 @@ // @flow import { reduceThreadActivity } from './thread-activity-reducer.js'; +import { messageStorePruneActionType } from '../actions/message-actions.js'; import { deleteKeyserverAccountActionTypes } from '../actions/user-actions.js'; import { updateThreadLastNavigatedActionType } from '../types/thread-activity-types.js'; // NOTE: These unit tests were generated by GitHub Copilot. describe('reduceThreadActivity', () => { test('updates the lastNavigatedTo time for a thread', () => { const initialState = { thread1: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, thread2: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, }; const action = { type: updateThreadLastNavigatedActionType, payload: { threadID: 'thread1', time: 1639522317443, }, }; const expectedState = { thread1: { lastNavigatedTo: 1639522317443, lastPruned: 1639522314170, }, thread2: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, }; const result = reduceThreadActivity(initialState, action); expect(result).toEqual(expectedState); }); + test('should create new thread activity entry with only lastNavigatedTo field', () => { + const initialState = {}; + const action = { + type: updateThreadLastNavigatedActionType, + payload: { + threadID: 'thread1', + time: 1639522317443, + }, + }; + const expectedState = { + thread1: { + lastNavigatedTo: 1639522317443, + }, + }; + const result = reduceThreadActivity(initialState, action); + expect(result).toEqual(expectedState); + }); + + test('should create new thread activity entry with only lastPruned field', () => { + const initialState = {}; + const action = { + type: messageStorePruneActionType, + payload: { + threadIDs: ['thread1'], + }, + }; + const expectedState = { + thread1: { + lastPruned: expect.any(Number), + }, + }; + const result = reduceThreadActivity(initialState, action); + expect(result).toEqual(expectedState); + }); + test('returns the initial state if the action type is not recognized', () => { const initialState = { thread1: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, thread2: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, }; const action = { type: 'UPDATE_REPORTS_ENABLED', payload: {}, }; const expectedState = { thread1: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, thread2: { lastNavigatedTo: 1639522314170, lastPruned: 1639522314170, }, }; const result = reduceThreadActivity(initialState, action); expect(result).toEqual(expectedState); }); test('removes threads of keyserver the user has disconnected from', () => { const keyserver1 = '100'; const keyserver2 = '200'; const keyserver3 = '300'; const threads1 = { [keyserver1 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, [keyserver1 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, }; const threads2 = { [keyserver2 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, [keyserver2 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, }; const threads3 = { [keyserver3 + '|1']: { lastNavigatedTo: 1, lastPruned: 1 }, [keyserver3 + '|2']: { lastNavigatedTo: 1, lastPruned: 1 }, }; const threads = { ...threads1, ...threads2, ...threads3 }; const action = { type: deleteKeyserverAccountActionTypes.success, payload: { currentUserInfo: { anonymous: true }, preRequestUserState: { cookiesAndSessions: {}, currentUserInfo: { id: '83810', username: 'user1', }, }, keyserverIDs: [keyserver1, keyserver2], }, loadingInfo: { fetchIndex: 1, trackMultipleRequests: false, customKeyName: undefined, }, }; expect(reduceThreadActivity(threads, action)).toEqual(threads3); }); }); diff --git a/lib/types/thread-activity-types.js b/lib/types/thread-activity-types.js index 21d95b296..b9eacb26e 100644 --- a/lib/types/thread-activity-types.js +++ b/lib/types/thread-activity-types.js @@ -1,12 +1,18 @@ // @flow export const updateThreadLastNavigatedActionType = 'UPDATE_THREAD_LAST_NAVIGATED'; -export type ThreadActivityStoreEntry = { - +lastNavigatedTo: number, // millisecond timestamp - +lastPruned: number, // millisecond timestamp -}; +export type ThreadActivityStoreEntry = + | { + +lastNavigatedTo: number, // millisecond timestamp + +lastPruned?: number, // millisecond timestamp + } + | { + +lastNavigatedTo?: number, + +lastPruned: number, + }; + export type ThreadActivityStore = { +[threadID: string]: ThreadActivityStoreEntry, };