1 import { InternalEventBus, ServerTime } from '@proton/docs-shared'
2 import { RecentDocumentsService } from './RecentDocumentsService'
3 import type { DecryptedNode, DriveCompat } from '@proton/drive-store/lib'
4 import { type RecentDocumentsSnapshotData } from './types'
5 import type { DocsApi } from '../../Api/DocsApi'
6 import type { LoggerInterface } from '@proton/utils/logs'
7 import type { RecentDocumentAPIItem } from '../../Api/Types/GetRecentsResponse'
9 describe('RecentDocumentsService', () => {
10 const mockData: RecentDocumentAPIItem[] = [
11 { LinkID: 'link1', ContextShareID: 'share1', LastOpenTime: 1 } as RecentDocumentAPIItem,
13 const mockDecryptedNodes: DecryptedNode[] = [
21 parentNodeId: 'parentLink1',
22 signatureAddress: 'me@proton.ch',
26 let service: RecentDocumentsService
27 let driveCompat: DriveCompat
31 const eventBus = new InternalEventBus()
33 getNodes: jest.fn().mockResolvedValue(mockDecryptedNodes),
34 getNodePaths: jest.fn().mockResolvedValue([['location', 'link1']]),
35 getNodesAreShared: jest.fn().mockResolvedValue([false]),
36 } as unknown as DriveCompat
39 fetchRecentDocuments: jest.fn().mockResolvedValue({
40 isFailed: () => false,
41 getValue: () => ({ RecentDocuments: mockData }),
43 } as unknown as DocsApi
47 } as unknown as LoggerInterface
49 service = new RecentDocumentsService(eventBus, driveCompat, docsApi, logger)
52 describe('fetch', () => {
53 test('Load recent documents from local storage', async () => {
57 test('Load links from driveCompat for every id returned from local storage', async () => {
60 expect(driveCompat.getNodes).toHaveBeenCalled()
61 expect(driveCompat.getNodePaths).toHaveBeenCalled()
64 test('service state will be "fetching until fetch resolves"', async () => {
65 const promise = service.fetch()
67 expect(service.state).toBe('fetching')
71 expect(service.state).toBe('done')
74 test('service state will be "fetching until fetch resolves"', async () => {
75 const promise = service.fetch()
77 expect(service.state).toBe('fetching')
81 expect(service.state).toBe('done')
85 describe('getSnapshot', () => {
86 test('Will return populated recent document data when fetch completes', async () => {
87 const mockSnapshotData: RecentDocumentsSnapshotData = {
91 lastViewed: new ServerTime(1),
92 parentLinkId: 'parentLink1',
93 createdBy: 'me@proton.ch',
96 const promise = service.fetch()
98 expect(service.getSnapshot().data).toEqual([])
102 expect(service.getSnapshot().data?.[0]).toMatchObject(mockSnapshotData)