Skip to content

Commit

Permalink
test: 테스트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Conut-1 committed Sep 11, 2024
1 parent 45bfa88 commit 2e568b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 78 deletions.
32 changes: 3 additions & 29 deletions nestjs-BE/server/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ describe('AuthController', () => {
provide: UsersService,
useValue: {
findUserByEmailAndProvider: jest.fn(),
createUser: jest.fn(),
getOrCreateUser: jest.fn(),
},
},
{
provide: ProfilesService,
useValue: {
createProfile: jest.fn(),
getOrCreateProfile: jest.fn(),
},
},
{
Expand All @@ -62,7 +62,7 @@ describe('AuthController', () => {
jest
.spyOn(authService, 'getKakaoAccount')
.mockResolvedValue(kakaoUserAccountMock);
jest.spyOn(usersService, 'findUserByEmailAndProvider').mockResolvedValue({
jest.spyOn(usersService, 'getOrCreateUser').mockResolvedValue({
uuid: 'user uuid',
} as User);
jest.spyOn(authService, 'login').mockResolvedValue(tokenMock);
Expand All @@ -74,32 +74,6 @@ describe('AuthController', () => {
message: 'Success',
data: tokenMock,
});
expect(usersService.createUser).not.toHaveBeenCalled();
});

it('kakaoLogin user login first time', async () => {
const requestMock = { kakaoUserId: 0 };
const kakaoUserAccountMock = { email: 'kakao email' };
const tokenMock = {
refresh_token: 'refresh token',
access_token: 'access token',
};
jest
.spyOn(authService, 'getKakaoAccount')
.mockResolvedValue(kakaoUserAccountMock);
jest
.spyOn(usersService, 'createUser')
.mockResolvedValue({ uuid: 'user uuid' } as User);
jest.spyOn(authService, 'login').mockResolvedValue(tokenMock);

const response = controller.kakaoLogin(requestMock);

await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: tokenMock,
});
expect(usersService.createUser).toHaveBeenCalled();
});

it('kakaoLogin kakao login fail', async () => {
Expand Down
32 changes: 6 additions & 26 deletions nestjs-BE/server/src/profiles/profiles.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('ProfilesService', () => {
profile: {
findUnique: jest.fn(),
findMany: jest.fn(),
create: jest.fn(),
upsert: jest.fn(),
update: jest.fn(),
},
},
Expand Down Expand Up @@ -83,38 +83,18 @@ describe('ProfilesService', () => {
await expect(profiles).resolves.toEqual([]);
});

it('createProfile created', async () => {
it('getOrCreateProfile', async () => {
const data = {
user_id: generateUuid(),
image: 'www.test.com/image',
nickname: 'test nickname',
};
const testProfile = { uuid: generateUuid(), ...data };
jest.spyOn(prisma.profile, 'create').mockResolvedValue(testProfile);
const profileMock = { uuid: generateUuid(), ...data };
jest.spyOn(prisma.profile, 'upsert').mockResolvedValue(profileMock);

const profile = profilesService.createProfile(data);
const profile = profilesService.getOrCreateProfile(data);

await expect(profile).resolves.toEqual(testProfile);
});

it("createProfile user_id doesn't exists", async () => {
const data = {
user_id: generateUuid(),
image: 'www.test.com/image',
nickname: 'test nickname',
};
jest
.spyOn(prisma.profile, 'create')
.mockRejectedValue(
new PrismaClientKnownRequestError(
'Foreign key constraint failed on the field: `user_id`',
{ code: 'P2003', clientVersion: '' },
),
);

const profile = profilesService.createProfile(data);

await expect(profile).rejects.toThrow(PrismaClientKnownRequestError);
await expect(profile).resolves.toEqual(profileMock);
});

it('updateProfile updated', async () => {
Expand Down
27 changes: 4 additions & 23 deletions nestjs-BE/server/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';
import { PrismaService } from '../prisma/prisma.service';
import generateUuid from '../utils/uuid';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';

describe('UsersService', () => {
let usersService: UsersService;
Expand All @@ -17,7 +16,7 @@ describe('UsersService', () => {
useValue: {
user: {
findUnique: jest.fn(),
create: jest.fn(),
upsert: jest.fn(),
},
},
},
Expand Down Expand Up @@ -55,37 +54,19 @@ describe('UsersService', () => {
await expect(user).resolves.toBeNull();
});

it('createUser created', async () => {
it('getOrCreateUser', async () => {
const testUser = {
uuid: generateUuid(),
email: '[email protected]',
provider: 'kakao',
};
jest.spyOn(prisma.user, 'create').mockResolvedValue(testUser);
jest.spyOn(prisma.user, 'upsert').mockResolvedValue(testUser);

const user = usersService.createUser({
const user = usersService.getOrCreateUser({
email: '[email protected]',
provider: 'kakao',
});

await expect(user).resolves.toEqual(testUser);
});

it('createUser user already exists', async () => {
jest
.spyOn(prisma.user, 'create')
.mockRejectedValue(
new PrismaClientKnownRequestError(
'Unique constraint failed on the constraint: `User_email_provider_key`',
{ code: 'P2025', clientVersion: '' },
),
);

const user = usersService.createUser({
email: '[email protected]',
provider: 'kakao',
});

await expect(user).rejects.toThrow(PrismaClientKnownRequestError);
});
});

0 comments on commit 2e568b8

Please sign in to comment.