@@ -11,6 +11,7 @@ import {
1111 PARTICIPANT ,
1212 ATTENDEE ,
1313} from '../constants.js'
14+ import BrowserStorage from '../services/BrowserStorage.js'
1415import {
1516 makePublic ,
1617 makePrivate ,
@@ -56,6 +57,11 @@ jest.mock('../services/conversationsService', () => ({
5657
5758jest . mock ( '@nextcloud/event-bus' )
5859
60+ jest . mock ( '../services/BrowserStorage.js' , ( ) => ( {
61+ getItem : jest . fn ( ) ,
62+ setItem : jest . fn ( ) ,
63+ } ) )
64+
5965describe ( 'conversationsStore' , ( ) => {
6066 const testToken = 'XXTOKENXX'
6167 const previousLastMessage = {
@@ -196,6 +202,31 @@ describe('conversationsStore', () => {
196202 expect ( deleteConversation ) . not . toHaveBeenCalled ( )
197203 } )
198204
205+ test ( 'restores conversations cached in BrowserStorage' , ( ) => {
206+ const testConversations = [
207+ {
208+ token : 'one_token' ,
209+ attendeeId : 'attendee-id-1' ,
210+ lastActivity : Date . parse ( '2023-02-01T00:00:00.000Z' ) / 1000 ,
211+ } ,
212+ {
213+ token : 'another_token' ,
214+ attendeeId : 'attendee-id-2' ,
215+ lastActivity : Date . parse ( '2023-01-01T00:00:00.000Z' ) / 1000 ,
216+ } ,
217+ ]
218+
219+ BrowserStorage . getItem . mockReturnValueOnce (
220+ '[{"token":"one_token","attendeeId":"attendee-id-1","lastActivity":1675209600},{"token":"another_token","attendeeId":"attendee-id-2","lastActivity":1672531200}]'
221+ )
222+
223+ store . dispatch ( 'restoreConversations' )
224+
225+ expect ( BrowserStorage . getItem ) . toHaveBeenCalledWith ( 'cachedConversations' )
226+ expect ( store . getters . conversationsList ) . toHaveLength ( 2 )
227+ expect ( store . getters . conversationsList ) . toEqual ( testConversations )
228+ } )
229+
199230 test ( 'deletes conversation from server' , async ( ) => {
200231 store . dispatch ( 'addConversation' , testConversation )
201232
@@ -258,6 +289,37 @@ describe('conversationsStore', () => {
258289 expect ( store . getters . conversationsList ) . toStrictEqual ( testConversations )
259290 } )
260291
292+ test ( 'sets fetched conversations to BrowserStorage' , async ( ) => {
293+ const testConversations = [
294+ {
295+ token : 'one_token' ,
296+ attendeeId : 'attendee-id-1' ,
297+ lastActivity : Date . parse ( '2023-02-01T00:00:00.000Z' ) / 1000 ,
298+ } ,
299+ {
300+ token : 'another_token' ,
301+ attendeeId : 'attendee-id-2' ,
302+ lastActivity : Date . parse ( '2023-01-01T00:00:00.000Z' ) / 1000 ,
303+ } ,
304+ ]
305+
306+ const response = {
307+ data : {
308+ ocs : {
309+ data : testConversations ,
310+ } ,
311+ } ,
312+ }
313+
314+ fetchConversations . mockResolvedValue ( response )
315+
316+ await store . dispatch ( 'fetchConversations' , { } )
317+
318+ expect ( BrowserStorage . setItem ) . toHaveBeenCalledWith ( 'cachedConversations' ,
319+ '[{"token":"one_token","attendeeId":"attendee-id-1","lastActivity":1675209600},{"token":"another_token","attendeeId":"attendee-id-2","lastActivity":1672531200}]'
320+ )
321+ } )
322+
261323 test ( 'fetches all conversations and add new received conversations' , async ( ) => {
262324 const oldConversation = {
263325 token : 'tokenOne' ,
0 commit comments