1 import type { RecentDocumentsSnapshotData } from '@proton/docs-core'
2 import { filterItems, getDisplayName } from './useRecentDocuments'
3 import type { ContactEmail } from '@proton/shared/lib/interfaces/contacts'
4 import { ServerTime } from '@proton/docs-shared'
6 jest.mock('@proton/shared/lib/i18n', () => ({ dateLocale: { code: 'us' } }))
8 describe('filterItems', () => {
9 test('if filter is set to "owned-by-me" will return items that are not shared with the user', () => {
10 const items: RecentDocumentsSnapshotData[] = [
15 lastViewed: new ServerTime(1),
16 isSharedWithMe: false,
22 lastViewed: new ServerTime(2),
28 lastViewed: new ServerTime(3),
32 expect(filterItems(items, undefined, 'owned-by-me')).toEqual([items[0], items[1]])
34 test('if filter is set to "owned-by-others" will return items that are shared with the user', () => {
35 const items: RecentDocumentsSnapshotData[] = [
40 lastViewed: new ServerTime(1),
41 isSharedWithMe: false,
47 lastViewed: new ServerTime(2),
53 lastViewed: new ServerTime(3),
57 expect(filterItems(items, undefined, 'owned-by-others')).toEqual([items[2]])
59 test('if filter is not populated return all items', () => {
60 const items: RecentDocumentsSnapshotData[] = [
65 lastViewed: new ServerTime(1),
66 isSharedWithMe: false,
72 lastViewed: new ServerTime(2),
78 lastViewed: new ServerTime(3),
82 expect(filterItems(items, undefined, undefined)).toEqual(items)
84 test('filter items starting with search term', () => {
85 const items: RecentDocumentsSnapshotData[] = [
90 lastViewed: new ServerTime(1),
91 isSharedWithMe: false,
97 lastViewed: new ServerTime(2),
102 volumeId: 'volume 3',
103 lastViewed: new ServerTime(3),
104 isSharedWithMe: true,
107 expect(filterItems(items, 'name', undefined)).toEqual([items[1], items[2]])
109 test('filter items ending with search term', () => {
110 const items: RecentDocumentsSnapshotData[] = [
114 volumeId: 'volume 1',
115 lastViewed: new ServerTime(1),
116 isSharedWithMe: false,
121 volumeId: 'volume 2',
122 lastViewed: new ServerTime(2),
127 volumeId: 'volume 3',
128 lastViewed: new ServerTime(3),
129 isSharedWithMe: true,
132 expect(filterItems(items, '2', undefined)).toEqual([items[0], items[1]])
135 test('filter items with term in the middle', () => {
136 const items: RecentDocumentsSnapshotData[] = [
140 volumeId: 'volume 1',
141 lastViewed: new ServerTime(1),
142 isSharedWithMe: false,
147 volumeId: 'volume 2',
148 lastViewed: new ServerTime(2),
153 volumeId: 'volume 3',
154 lastViewed: new ServerTime(3),
155 isSharedWithMe: true,
158 expect(filterItems(items, 'am', undefined)).toEqual([items[1], items[2]])
162 describe('getDisplayName', () => {
163 test('Will return the user email if name is not populated and the document is owned by the user', () => {
164 const recentDocument = {
167 volumeId: 'volume 1',
168 lastViewed: new ServerTime(1),
169 isSharedWithMe: false,
171 expect(getDisplayName(recentDocument)).toBe('Me')
173 test('Will return the user if name is populated and the document is owned by the user', () => {
174 const recentDocument = {
177 volumeId: 'volume 1',
178 lastViewed: new ServerTime(1),
179 isSharedWithMe: false,
181 expect(getDisplayName(recentDocument)).toBe('Me')
184 test('Will return the contact display name if it is present and the document is shared with the user', () => {
185 const recentDocument = {
188 volumeId: 'volume 1',
189 lastViewed: new ServerTime(1),
190 isSharedWithMe: true,
191 createdBy: 'joe@proton.ch',
193 const contacts = [{ Name: 'Joe Bloggs', Email: 'joe@proton.ch' }] as unknown as ContactEmail[]
194 expect(getDisplayName(recentDocument, contacts)).toBe('Joe Bloggs')
196 test('Will return the contact email if there is no name and the document is shared with the user', () => {
197 const recentDocument = {
200 volumeId: 'volume 1',
201 lastViewed: new ServerTime(1),
202 isSharedWithMe: true,
203 createdBy: 'joe@proton.ch',
205 const contacts = [{ Email: 'joe@proton.ch' }] as unknown as ContactEmail[]
206 expect(getDisplayName(recentDocument, contacts)).toBe('joe@proton.ch')