|
| 1 | +import { v4 } from 'uuid'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { ItemFactory } from '../../../../../test/factories/item.factory'; |
| 5 | +import { MemberFactory } from '../../../../../test/factories/member.factory'; |
| 6 | +import type { DBConnection } from '../../../../drizzle/db'; |
| 7 | +import { AuthorizedItemService } from '../../../authorizedItem.service'; |
| 8 | +import { ItemMembershipRepository } from '../../../itemMembership/membership.repository'; |
| 9 | +import { ItemVisibilityRepository } from '../itemVisibility/itemVisibility.repository'; |
| 10 | +import { ItemThumbnailService } from '../thumbnail/itemThumbnail.service'; |
| 11 | +import { ItemBookmarkRepository } from './itemBookmark.repository'; |
| 12 | +import { BookmarkService } from './itemBookmark.service'; |
| 13 | + |
| 14 | +const dbConnection = {} as DBConnection; |
| 15 | + |
| 16 | +describe('BookmarkService', () => { |
| 17 | + describe('getOwn', () => { |
| 18 | + it('returns packed bookmarked items with thumbnail URLs', async () => { |
| 19 | + const member = MemberFactory(); |
| 20 | + const itemWithThumbnail = ItemFactory({ settings: { hasThumbnail: true } }); |
| 21 | + const itemWithoutThumbnail = ItemFactory({ settings: { hasThumbnail: false } }); |
| 22 | + const thumbnailUrls = { |
| 23 | + [itemWithThumbnail.id]: { |
| 24 | + small: 'https://example.com/small-thumbnail', |
| 25 | + medium: 'https://example.com/medium-thumbnail', |
| 26 | + }, |
| 27 | + }; |
| 28 | + const bookmarks = [ |
| 29 | + { |
| 30 | + id: v4(), |
| 31 | + itemId: itemWithThumbnail.id, |
| 32 | + memberId: member.id, |
| 33 | + createdAt: new Date().toISOString(), |
| 34 | + item: itemWithThumbnail, |
| 35 | + }, |
| 36 | + { |
| 37 | + id: v4(), |
| 38 | + itemId: itemWithoutThumbnail.id, |
| 39 | + memberId: member.id, |
| 40 | + createdAt: new Date().toISOString(), |
| 41 | + item: itemWithoutThumbnail, |
| 42 | + }, |
| 43 | + ]; |
| 44 | + |
| 45 | + const itemBookmarkRepository = { |
| 46 | + getBookmarksForMember: vi.fn().mockResolvedValue(bookmarks), |
| 47 | + } as unknown as ItemBookmarkRepository; |
| 48 | + const itemMembershipRepository = { |
| 49 | + getForManyItems: vi.fn().mockResolvedValue({ |
| 50 | + data: { |
| 51 | + [itemWithThumbnail.id]: [{ permission: 'read' }], |
| 52 | + [itemWithoutThumbnail.id]: [{ permission: 'read' }], |
| 53 | + }, |
| 54 | + errors: [], |
| 55 | + }), |
| 56 | + } as unknown as ItemMembershipRepository; |
| 57 | + const itemVisibilityRepository = { |
| 58 | + getManyForMany: vi.fn().mockResolvedValue({ |
| 59 | + data: { |
| 60 | + [itemWithThumbnail.id]: [], |
| 61 | + [itemWithoutThumbnail.id]: [], |
| 62 | + }, |
| 63 | + errors: [], |
| 64 | + }), |
| 65 | + } as unknown as ItemVisibilityRepository; |
| 66 | + const itemThumbnailService = { |
| 67 | + getUrlsByItems: vi.fn().mockResolvedValue(thumbnailUrls), |
| 68 | + } as unknown as ItemThumbnailService; |
| 69 | + |
| 70 | + const service = new BookmarkService( |
| 71 | + {} as AuthorizedItemService, |
| 72 | + itemBookmarkRepository, |
| 73 | + itemMembershipRepository, |
| 74 | + itemVisibilityRepository, |
| 75 | + itemThumbnailService, |
| 76 | + ); |
| 77 | + |
| 78 | + const result = await service.getOwn(dbConnection, member); |
| 79 | + |
| 80 | + expect(itemThumbnailService.getUrlsByItems).toHaveBeenCalledWith([ |
| 81 | + itemWithThumbnail, |
| 82 | + itemWithoutThumbnail, |
| 83 | + ]); |
| 84 | + expect(result).toHaveLength(2); |
| 85 | + expect(result[0]).toMatchObject({ |
| 86 | + id: bookmarks[0].id, |
| 87 | + createdAt: bookmarks[0].createdAt, |
| 88 | + item: { |
| 89 | + id: itemWithThumbnail.id, |
| 90 | + thumbnails: thumbnailUrls[itemWithThumbnail.id], |
| 91 | + }, |
| 92 | + }); |
| 93 | + expect(result[1]).toMatchObject({ |
| 94 | + id: bookmarks[1].id, |
| 95 | + createdAt: bookmarks[1].createdAt, |
| 96 | + item: { |
| 97 | + id: itemWithoutThumbnail.id, |
| 98 | + }, |
| 99 | + }); |
| 100 | + expect(result[1].item.thumbnails).toBeUndefined(); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments