-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from boostcampwm2023/BE-feature/users
ํ ์คํธ ๊ฐ์
- Loading branch information
Showing
3 changed files
with
51 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export class CreateUserDto { | ||
export class CreateUserPrismaDto { | ||
readonly email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,47 +2,71 @@ import { Test, TestingModule } from '@nestjs/testing'; | |
import { UsersService } from './users.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { KakaoUser, User } from '@prisma/client'; | ||
|
||
describe('UsersService', () => { | ||
let usersService: UsersService; | ||
let prisma: PrismaService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService, PrismaService], | ||
providers: [ | ||
UsersService, | ||
{ | ||
provide: PrismaService, | ||
useValue: { | ||
$transaction: jest.fn(), | ||
user: { create: jest.fn(), findUnique: jest.fn() }, | ||
kakaoUser: { create: jest.fn(), findUnique: jest.fn() }, | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
usersService = module.get<UsersService>(UsersService); | ||
prisma = module.get<PrismaService>(PrismaService); | ||
|
||
await prisma.kakaoUser.deleteMany({}); | ||
await prisma.user.deleteMany({}); | ||
}); | ||
|
||
it('getOrCreateUser', async () => { | ||
const testUserUuid = uuid(); | ||
const testEmail = '[email protected]'; | ||
const testUser = { uuid: testUserUuid }; | ||
await prisma.user.create({ | ||
data: { uuid: testUserUuid }, | ||
}); | ||
await prisma.kakaoUser.create({ | ||
data: { | ||
email: testEmail, | ||
userUuid: testUserUuid, | ||
}, | ||
}); | ||
|
||
const user = usersService.getOrCreateUser({ email: testEmail }); | ||
it('getOrCreateUser user exist', async () => { | ||
const testUser: User = { uuid: uuid() }; | ||
const testKakaoUser: KakaoUser = { | ||
id: 0, | ||
email: '[email protected]', | ||
userUuid: testUser.uuid, | ||
}; | ||
|
||
jest | ||
.spyOn(prisma, '$transaction') | ||
.mockImplementation(async (callback) => callback(prisma)); | ||
jest.spyOn(prisma.kakaoUser, 'findUnique').mockResolvedValue(testKakaoUser); | ||
jest.spyOn(prisma.user, 'findUnique').mockResolvedValue(testUser); | ||
|
||
const user = usersService.getOrCreateUser({ email: testKakaoUser.email }); | ||
|
||
await expect(user).resolves.toEqual(testUser); | ||
expect(prisma.user.create).not.toHaveBeenCalled(); | ||
expect(prisma.user.findUnique).toHaveBeenCalled(); | ||
}); | ||
|
||
it("getOrCreateUser user doesn't exist", async () => { | ||
const testEmail = '[email protected]'; | ||
const newUser: User = { uuid: uuid() }; | ||
const newKakaoUser: KakaoUser = { | ||
id: 0, | ||
email: '[email protected]', | ||
userUuid: newUser.uuid, | ||
}; | ||
|
||
jest | ||
.spyOn(prisma, '$transaction') | ||
.mockImplementation(async (callback) => callback(prisma)); | ||
jest.spyOn(prisma.kakaoUser, 'findUnique').mockResolvedValue(null); | ||
jest.spyOn(prisma.kakaoUser, 'create').mockResolvedValue(newKakaoUser); | ||
jest.spyOn(prisma.user, 'create').mockResolvedValue(newUser); | ||
|
||
const user = usersService.getOrCreateUser({ email: newKakaoUser.email }); | ||
|
||
const user = await usersService.getOrCreateUser({ email: testEmail }); | ||
expect(user.uuid).toMatch( | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, | ||
); | ||
await expect(user).resolves.toEqual(newUser); | ||
expect(prisma.user.create).toHaveBeenCalled(); | ||
expect(prisma.user.findUnique).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters