|
3 | 3 | createSyncingFakeFile, |
4 | 4 | computeSyncingFakeFile, |
5 | 5 | checkSyncingFakeFileObsolescence, |
6 | | - isReferencedByShareInSharingContext |
| 6 | + isReferencedByShareInSharingContext, |
| 7 | + isReceivedShare, |
| 8 | + filterOutReceivedShares |
7 | 9 | } from './syncHelpers' |
8 | 10 |
|
9 | 11 | const queryResults = [ |
@@ -201,6 +203,98 @@ describe('syncHelpers', () => { |
201 | 203 | }) |
202 | 204 | }) |
203 | 205 |
|
| 206 | + describe('isReceivedShare', () => { |
| 207 | + const sharingRoot = { |
| 208 | + _id: 'shared-folder-id', |
| 209 | + relationships: { |
| 210 | + referenced_by: { |
| 211 | + data: [{ id: 'sharing-id', type: 'io.cozy.sharings' }] |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + const ownFile = { |
| 216 | + _id: 'own-file-id', |
| 217 | + relationships: { |
| 218 | + referenced_by: { |
| 219 | + data: [{ id: 'album-id', type: 'io.cozy.photos.albums' }] |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + it('returns true when referenced by a sharing the user does not own', () => { |
| 225 | + expect(isReceivedShare(sharingRoot, () => false)).toBe(true) |
| 226 | + }) |
| 227 | + |
| 228 | + it('returns false for the owner of the sharing', () => { |
| 229 | + expect(isReceivedShare(sharingRoot, () => true)).toBe(false) |
| 230 | + }) |
| 231 | + |
| 232 | + it('returns false when not referenced by any sharing', () => { |
| 233 | + expect(isReceivedShare(ownFile, () => false)).toBe(false) |
| 234 | + }) |
| 235 | + |
| 236 | + it('returns false when there is no referenced_by relationship', () => { |
| 237 | + expect(isReceivedShare({ _id: 'x' }, () => false)).toBe(false) |
| 238 | + }) |
| 239 | + |
| 240 | + it('returns false when referenced_by.data is null', () => { |
| 241 | + const file = { |
| 242 | + _id: 'x', |
| 243 | + relationships: { referenced_by: { data: null } } |
| 244 | + } |
| 245 | + expect(isReceivedShare(file, () => false)).toBe(false) |
| 246 | + }) |
| 247 | + }) |
| 248 | + |
| 249 | + describe('filterOutReceivedShares', () => { |
| 250 | + const isOwner = id => id === 'own-shared-folder' |
| 251 | + const results = [ |
| 252 | + { |
| 253 | + data: [ |
| 254 | + { _id: 'own-folder' }, |
| 255 | + { |
| 256 | + _id: 'own-shared-folder', |
| 257 | + relationships: { |
| 258 | + referenced_by: { |
| 259 | + data: [{ id: 's1', type: 'io.cozy.sharings' }] |
| 260 | + } |
| 261 | + } |
| 262 | + }, |
| 263 | + { |
| 264 | + _id: 'received-share', |
| 265 | + relationships: { |
| 266 | + referenced_by: { |
| 267 | + data: [{ id: 's2', type: 'io.cozy.sharings' }] |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + ], |
| 272 | + hasMore: true, |
| 273 | + fetchMore: jest.fn() |
| 274 | + } |
| 275 | + ] |
| 276 | + |
| 277 | + it('drops received shares but keeps own and own-shared files', () => { |
| 278 | + const [filtered] = filterOutReceivedShares(results, isOwner) |
| 279 | + expect(filtered.data.map(f => f._id)).toEqual([ |
| 280 | + 'own-folder', |
| 281 | + 'own-shared-folder' |
| 282 | + ]) |
| 283 | + expect(filtered.count).toBe(2) |
| 284 | + }) |
| 285 | + |
| 286 | + it('preserves the other result properties', () => { |
| 287 | + const [filtered] = filterOutReceivedShares(results, isOwner) |
| 288 | + expect(filtered.hasMore).toBe(true) |
| 289 | + expect(filtered.fetchMore).toBe(results[0].fetchMore) |
| 290 | + }) |
| 291 | + |
| 292 | + it('passes through results without data untouched', () => { |
| 293 | + const noData = [{ fetchStatus: 'loading' }] |
| 294 | + expect(filterOutReceivedShares(noData, isOwner)).toEqual(noData) |
| 295 | + }) |
| 296 | + }) |
| 297 | + |
204 | 298 | describe('isReferencedByShareInSharingContext', () => { |
205 | 299 | it('should return true or false if the file is referenced or not by a share in sharing context', () => { |
206 | 300 | const referencedFile = { |
|
0 commit comments