|
| 1 | +import { LiveblocksAPI, RoomCollaborators, RoomStorage } from '../clients/liveblocks.server' |
| 2 | +import { prisma } from '../db.server' |
| 3 | +import { |
| 4 | + createTestProject, |
| 5 | + createTestProjectCollaborator, |
| 6 | + createTestUser, |
| 7 | + truncateTables, |
| 8 | +} from '../test-util' |
| 9 | +import { getCollaborators, updateCollaborators } from './projectCollaborators.server' |
| 10 | + |
| 11 | +describe('projectCollaborators model', () => { |
| 12 | + afterEach(async () => { |
| 13 | + // cleanup |
| 14 | + await truncateTables([ |
| 15 | + prisma.projectCollaborator, |
| 16 | + prisma.projectID, |
| 17 | + prisma.project, |
| 18 | + prisma.userDetails, |
| 19 | + ]) |
| 20 | + }) |
| 21 | + |
| 22 | + describe('getCollaborators', () => { |
| 23 | + beforeEach(async () => { |
| 24 | + await createTestUser(prisma, { id: 'bob' }) |
| 25 | + await createTestUser(prisma, { id: 'alice' }) |
| 26 | + await createTestUser(prisma, { id: 'wendy' }) |
| 27 | + await createTestProject(prisma, { id: 'one', ownerId: 'bob' }) |
| 28 | + await createTestProject(prisma, { id: 'two', ownerId: 'bob' }) |
| 29 | + await createTestProject(prisma, { id: 'three', ownerId: 'alice' }) |
| 30 | + await createTestProject(prisma, { id: 'four', ownerId: 'bob' }) |
| 31 | + await createTestProject(prisma, { id: 'five', ownerId: 'bob' }) |
| 32 | + await createTestProjectCollaborator(prisma, { projectId: 'one', userId: 'bob' }) |
| 33 | + await createTestProjectCollaborator(prisma, { projectId: 'two', userId: 'bob' }) |
| 34 | + await createTestProjectCollaborator(prisma, { projectId: 'two', userId: 'wendy' }) |
| 35 | + await createTestProjectCollaborator(prisma, { projectId: 'three', userId: 'alice' }) |
| 36 | + await createTestProjectCollaborator(prisma, { projectId: 'three', userId: 'bob' }) |
| 37 | + await createTestProjectCollaborator(prisma, { projectId: 'five', userId: 'alice' }) |
| 38 | + await createTestProjectCollaborator(prisma, { projectId: 'five', userId: 'wendy' }) |
| 39 | + }) |
| 40 | + it('returns an empty object if no ids are passed', async () => { |
| 41 | + const got = await getCollaborators({ ids: [], userId: 'bob' }) |
| 42 | + expect(got).toEqual({}) |
| 43 | + }) |
| 44 | + it("returns an empty object if ids don't match the given user id", async () => { |
| 45 | + let got = await getCollaborators({ ids: ['one', 'two'], userId: 'alice' }) |
| 46 | + expect(got).toEqual({}) |
| 47 | + got = await getCollaborators({ ids: ['one', 'two'], userId: 'NOBODY' }) |
| 48 | + expect(got).toEqual({}) |
| 49 | + }) |
| 50 | + it('returns the collaborator details by project id', async () => { |
| 51 | + const ids = ['one', 'two', 'four', 'five'] |
| 52 | + const got = await getCollaborators({ ids: ids, userId: 'bob' }) |
| 53 | + expect(Object.keys(got)).toEqual(ids) |
| 54 | + expect(got['one'].map((c) => c.id)).toEqual(['bob']) |
| 55 | + expect(got['two'].map((c) => c.id)).toEqual(['bob', 'wendy']) |
| 56 | + expect(got['four'].map((c) => c.id)).toEqual([]) |
| 57 | + expect(got['five'].map((c) => c.id)).toEqual(['alice', 'wendy']) |
| 58 | + }) |
| 59 | + it('ignores mismatching projects', async () => { |
| 60 | + const ids = ['one', 'two', 'three'] |
| 61 | + const got = await getCollaborators({ ids: ids, userId: 'bob' }) |
| 62 | + expect(Object.keys(got)).toEqual(['one', 'two']) |
| 63 | + expect(got['one'].map((c) => c.id)).toEqual(['bob']) |
| 64 | + expect(got['two'].map((c) => c.id)).toEqual(['bob', 'wendy']) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('updateCollaborators', () => { |
| 69 | + beforeEach(async () => { |
| 70 | + await createTestUser(prisma, { id: 'bob' }) |
| 71 | + await createTestUser(prisma, { id: 'alice' }) |
| 72 | + await createTestUser(prisma, { id: 'wendy' }) |
| 73 | + await createTestProject(prisma, { id: 'one', ownerId: 'bob' }) |
| 74 | + await createTestProject(prisma, { id: 'two', ownerId: 'bob' }) |
| 75 | + await createTestProject(prisma, { id: 'three', ownerId: 'alice' }) |
| 76 | + await createTestProject(prisma, { id: 'four', ownerId: 'bob' }) |
| 77 | + await createTestProject(prisma, { id: 'five', ownerId: 'bob' }) |
| 78 | + await createTestProject(prisma, { id: 'six', ownerId: 'bob' }) |
| 79 | + await createTestProjectCollaborator(prisma, { projectId: 'one', userId: 'bob' }) |
| 80 | + await createTestProjectCollaborator(prisma, { projectId: 'two', userId: 'bob' }) |
| 81 | + await createTestProjectCollaborator(prisma, { projectId: 'two', userId: 'wendy' }) |
| 82 | + await createTestProjectCollaborator(prisma, { projectId: 'three', userId: 'alice' }) |
| 83 | + await createTestProjectCollaborator(prisma, { projectId: 'three', userId: 'bob' }) |
| 84 | + await createTestProjectCollaborator(prisma, { projectId: 'five', userId: 'alice' }) |
| 85 | + await createTestProjectCollaborator(prisma, { projectId: 'five', userId: 'wendy' }) |
| 86 | + }) |
| 87 | + |
| 88 | + const mockGetRoomStorage = (collabs: RoomCollaborators) => async (): Promise<RoomStorage> => { |
| 89 | + return { |
| 90 | + data: { |
| 91 | + collaborators: collabs, |
| 92 | + }, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + describe('when the project has collaborators', () => { |
| 97 | + describe('when the room storage has existing users', () => { |
| 98 | + it('updates the collaborators with the data from the room storage', async () => { |
| 99 | + LiveblocksAPI.getRoomStorage = mockGetRoomStorage({ |
| 100 | + data: { |
| 101 | + alice: { data: { id: 'alice', name: 'Alice Alisson', avatar: 'alice.png' } }, |
| 102 | + }, |
| 103 | + }) |
| 104 | + await updateCollaborators({ id: 'one' }) |
| 105 | + |
| 106 | + const got = await prisma.projectCollaborator.findMany({ where: { project_id: 'one' } }) |
| 107 | + expect(got.map((c) => c.user_id)).toEqual(['bob', 'alice']) |
| 108 | + }) |
| 109 | + }) |
| 110 | + describe('when the room storage has duplicate users', () => { |
| 111 | + it('only adds the missing ones', async () => { |
| 112 | + LiveblocksAPI.getRoomStorage = mockGetRoomStorage({ |
| 113 | + data: { |
| 114 | + alice: { data: { id: 'alice', name: 'Alice Alisson', avatar: 'alice.png' } }, |
| 115 | + bob: { data: { id: 'bob', name: 'Bob Bobson', avatar: 'bob.png' } }, |
| 116 | + }, |
| 117 | + }) |
| 118 | + await updateCollaborators({ id: 'one' }) |
| 119 | + |
| 120 | + const got = await prisma.projectCollaborator.findMany({ |
| 121 | + where: { project_id: 'one' }, |
| 122 | + }) |
| 123 | + expect(got.map((c) => c.user_id)).toEqual(['bob', 'alice']) |
| 124 | + }) |
| 125 | + }) |
| 126 | + describe('when the room storage has non-existing users', () => { |
| 127 | + it('updates the collaborators with only the existing users', async () => { |
| 128 | + LiveblocksAPI.getRoomStorage = mockGetRoomStorage({ |
| 129 | + data: { |
| 130 | + alice: { |
| 131 | + data: { |
| 132 | + id: 'alice', |
| 133 | + name: 'Alice Alisson', |
| 134 | + avatar: 'alice.png', |
| 135 | + }, |
| 136 | + }, |
| 137 | + johndoe: { |
| 138 | + data: { |
| 139 | + id: 'johndoe', |
| 140 | + name: 'John Doe', |
| 141 | + avatar: 'johndoe.png', |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }) |
| 146 | + await updateCollaborators({ id: 'one' }) |
| 147 | + |
| 148 | + const got = await prisma.projectCollaborator.findMany({ where: { project_id: 'one' } }) |
| 149 | + expect(got.map((c) => c.user_id)).toEqual(['bob', 'alice']) |
| 150 | + }) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe('when the project has no collaborators', () => { |
| 155 | + it('adds the collaborators with the data from the room storage', async () => { |
| 156 | + LiveblocksAPI.getRoomStorage = mockGetRoomStorage({ |
| 157 | + data: { |
| 158 | + alice: { data: { id: 'alice', name: 'Alice Alisson', avatar: 'alice.png' } }, |
| 159 | + johndoe: { data: { id: 'johndoe', name: 'John Doe', avatar: 'johndoe.png' } }, |
| 160 | + bob: { data: { id: 'bob', name: 'Bob Bobson', avatar: 'bob.png' } }, |
| 161 | + }, |
| 162 | + }) |
| 163 | + await updateCollaborators({ id: 'six' }) |
| 164 | + |
| 165 | + const got = await prisma.projectCollaborator.findMany({ where: { project_id: 'six' } }) |
| 166 | + expect(got.map((c) => c.user_id)).toEqual(['bob', 'alice']) |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + describe('when the project does not exist', () => { |
| 171 | + it('errors', async () => { |
| 172 | + LiveblocksAPI.getRoomStorage = mockGetRoomStorage({ |
| 173 | + data: { |
| 174 | + alice: { data: { id: 'alice', name: 'Alice Alisson', avatar: 'alice.png' } }, |
| 175 | + }, |
| 176 | + }) |
| 177 | + const fn = async () => updateCollaborators({ id: 'unknown' }) |
| 178 | + await expect(fn).rejects.toThrow() |
| 179 | + }) |
| 180 | + }) |
| 181 | + }) |
| 182 | +}) |
0 commit comments