Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

테스트 개선 #352

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nestjs-BE/server/src/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export class CreateUserDto {
export class CreateUserPrismaDto {
readonly email: string;
}
72 changes: 48 additions & 24 deletions nestjs-BE/server/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
4 changes: 2 additions & 2 deletions nestjs-BE/server/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateUserDto } from './dto/create-user.dto';
import { CreateUserPrismaDto } from './dto/create-user.dto';
import { User } from '@prisma/client';
import { v4 as uuid } from 'uuid';

@Injectable()
export class UsersService {
constructor(private prisma: PrismaService) {}

async getOrCreateUser(data: CreateUserDto): Promise<User> {
async getOrCreateUser(data: CreateUserPrismaDto): Promise<User> {
return this.prisma.$transaction(async () => {
const kakaoUser = await this.prisma.kakaoUser.findUnique({
where: { email: data.email },
Expand Down
Loading