|
1 | 1 | import TeamsService from '../src/services/teams.services'; |
2 | 2 | import prisma from '../src/prisma/prisma'; |
3 | 3 | import * as teamsTransformer from '../src/transformers/teams.transformer'; |
4 | | -import { prismaTeam1, sharedTeam1, justiceLeague } from './test-data/teams.test-data'; |
| 4 | +import { prismaTeam1, sharedTeam1, justiceLeague, primsaTeam2 } from './test-data/teams.test-data'; |
5 | 5 | import teamQueryArgs from '../src/prisma-query-args/teams.query-args'; |
6 | | -import { batman, flash, greenlantern, superman, theVisitor, wonderwoman } from './test-data/users.test-data'; |
| 6 | +import { |
| 7 | + alfred, |
| 8 | + aquaman, |
| 9 | + batman, |
| 10 | + flash, |
| 11 | + greenlantern, |
| 12 | + superman, |
| 13 | + theVisitor, |
| 14 | + wonderwoman |
| 15 | +} from './test-data/users.test-data'; |
7 | 16 | import * as userUtils from '../src/utils/users.utils'; |
8 | 17 | import { AccessDeniedException, HttpException } from '../src/utils/errors.utils'; |
9 | 18 | import teamTransformer from '../src/transformers/teams.transformer'; |
@@ -208,4 +217,77 @@ describe('Teams', () => { |
208 | 217 | expect(res).toStrictEqual(sharedTeam1); |
209 | 218 | }); |
210 | 219 | }); |
| 220 | + |
| 221 | + describe('setTeamLeads', () => { |
| 222 | + test('setTeamLeads submitter is not the head or admin', async () => { |
| 223 | + vi.spyOn(prisma.team, 'findUnique').mockResolvedValue(prismaTeam1); |
| 224 | + vi.spyOn(prisma.user, 'findMany').mockResolvedValue([theVisitor]); |
| 225 | + vi.spyOn(prisma.team, 'findMany').mockResolvedValue([prismaTeam1, primsaTeam2, justiceLeague]); |
| 226 | + |
| 227 | + const callSetTeamLeads = async () => |
| 228 | + await TeamsService.setTeamLeads(wonderwoman, prismaTeam1.teamId, [theVisitor.userId]); |
| 229 | + |
| 230 | + const expectedException = new HttpException( |
| 231 | + 400, |
| 232 | + 'Access Denied: You must be an admin or the head to update the lead!' |
| 233 | + ); |
| 234 | + |
| 235 | + await expect(callSetTeamLeads).rejects.toThrow(expectedException); |
| 236 | + }); |
| 237 | + |
| 238 | + test('setTeamLeads lead is a member', async () => { |
| 239 | + vi.spyOn(prisma.team, 'findUnique').mockResolvedValue(prismaTeam1); |
| 240 | + vi.spyOn(prisma.user, 'findMany').mockResolvedValue([aquaman]); |
| 241 | + vi.spyOn(prisma.team, 'findMany').mockResolvedValue([prismaTeam1, primsaTeam2, justiceLeague]); |
| 242 | + |
| 243 | + const callSetTeamLeads = async () => await TeamsService.setTeamLeads(flash, sharedTeam1.teamId, [aquaman.userId]); |
| 244 | + |
| 245 | + const expectedException = new HttpException(400, 'A lead cannot be a member of the team!'); |
| 246 | + |
| 247 | + await expect(callSetTeamLeads).rejects.toThrow(expectedException); |
| 248 | + }); |
| 249 | + |
| 250 | + test('setTeamLeads lead is a head', async () => { |
| 251 | + vi.spyOn(prisma.team, 'findUnique').mockResolvedValue(justiceLeague); |
| 252 | + vi.spyOn(userUtils, 'getUsers').mockResolvedValue([batman]); |
| 253 | + vi.spyOn(prisma.team, 'findMany').mockResolvedValue([prismaTeam1, primsaTeam2, justiceLeague]); |
| 254 | + |
| 255 | + const callSetTeamLeads = async () => await TeamsService.setTeamLeads(alfred, justiceLeague.teamId, [batman.userId]); |
| 256 | + |
| 257 | + const expectedException = new HttpException(400, 'A lead cannot be the head of the team!'); |
| 258 | + |
| 259 | + await expect(callSetTeamLeads).rejects.toThrow(expectedException); |
| 260 | + }); |
| 261 | + |
| 262 | + test('setTeamLeads works', async () => { |
| 263 | + vi.spyOn(prisma.team, 'findUnique').mockResolvedValue(prismaTeam1); |
| 264 | + vi.spyOn(prisma.team, 'update').mockResolvedValue(prismaTeam1); |
| 265 | + vi.spyOn(userUtils, 'getUsers').mockResolvedValue([greenlantern, theVisitor]); |
| 266 | + vi.spyOn(prisma.team, 'findMany').mockResolvedValue([prismaTeam1, primsaTeam2, justiceLeague]); |
| 267 | + |
| 268 | + const teamId = 'id1'; |
| 269 | + const userIds = [ |
| 270 | + { |
| 271 | + userId: 5 |
| 272 | + }, |
| 273 | + { |
| 274 | + userId: 7 |
| 275 | + } |
| 276 | + ]; |
| 277 | + const res = await TeamsService.setTeamLeads(flash, sharedTeam1.teamId, [5, 7]); |
| 278 | + |
| 279 | + expect(prisma.team.findUnique).toHaveBeenCalledTimes(1); |
| 280 | + expect(prisma.team.update).toHaveBeenCalledTimes(1); |
| 281 | + expect(prisma.team.update).toHaveBeenCalledWith({ |
| 282 | + where: { teamId }, |
| 283 | + data: { |
| 284 | + leads: { |
| 285 | + set: userIds |
| 286 | + } |
| 287 | + }, |
| 288 | + ...teamQueryArgs |
| 289 | + }); |
| 290 | + expect(res).toStrictEqual(sharedTeam1); |
| 291 | + }); |
| 292 | + }); |
211 | 293 | }); |
0 commit comments