Skip to content

Commit

Permalink
refactor(auth-mock): 사용자 mock 변수 이름 변경하고 id 생성 함수 정의한다 (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
leegwae committed Sep 14, 2023
1 parent 47110f2 commit 1db00a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
27 changes: 15 additions & 12 deletions src/mocks/data/auth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import type { Member } from '@/lib/api/auth';

export const myAccount: Omit<Member, 'password'> = {
id: -1,
email: 'dev-email@toquiz.com',
nickname: 'dev-nickname',
provider: 'LOCAL',
createdAt: new Date().toString(),
updatedAt: new Date().toString(),
};

let id = 0;
export const createMockUserId = (() => {
let id = 0;
return () => id++;
})();
export const createMockUser = (): Omit<Member, 'password'> => ({
id: id++,
email: '테스트 이메일',
id: createMockUserId(),
email: 'test@email.com',
nickname: '테스트 닉네임',
provider: 'TEST',
createdAt: new Date().toString(),
updatedAt: new Date().toString(),
});

export const mockUser: Omit<Member, 'password'> = {
id: createMockUserId(),
email: 'dev-email@toquiz.com',
nickname: 'dev-nickname',
provider: 'LOCAL',
createdAt: new Date().toString(),
updatedAt: new Date().toString(),
};
6 changes: 3 additions & 3 deletions src/mocks/data/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import type { Panel } from '@/lib/api/panel';

import { faker } from '@faker-js/faker';

import { myAccount } from './auth';
import { mockUser } from './auth';

export const createMockPanel = (): Panel => ({
sid: faker.datatype.uuid(),
title: faker.music.songName(),
description: faker.lorem.sentences().slice(0, 50),
author: {
id: myAccount.id,
nickname: myAccount.nickname,
id: mockUser.id,
nickname: mockUser.nickname,
},
createdAt: new Date().toDateString(),
updatedAt: new Date().toDateString(),
Expand Down
16 changes: 8 additions & 8 deletions src/mocks/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { rest } from 'msw';

import { apiUrl } from '@/lib/api/apiUrl';
import { API_BASE_URL } from '@/lib/apiClient';
import { myAccount } from '@/mocks/data/auth';
import { mockUser } from '@/mocks/data/auth';

export const signUp = rest.post<SignUpBody, never, SignUpResponse>(
`${API_BASE_URL}${apiUrl.auth.signup()}`,
async (req, res, ctx) => {
const { nickname }: SignUpBody = await req.json();
myAccount.nickname = nickname;
mockUser.nickname = nickname;

return res(
ctx.status(200),
Expand All @@ -40,15 +40,15 @@ export const login = rest.post<LogInBody, never, LogInResponse>(
`${API_BASE_URL}${apiUrl.auth.login()}`,
async (req, res, ctx) => {
const { email }: LogInBody = await req.json();
myAccount.email = email;
mockUser.email = email;

return res(
ctx.status(200),
ctx.cookie('refreshToken', 'dev-refreshToken', { httpOnly: true }),
ctx.json({
statusCode: 200,
result: {
...myAccount,
...mockUser,
email,
accessToken: 'dev-accessToken',
},
Expand Down Expand Up @@ -85,7 +85,7 @@ export const me = rest.get<never, never, GetMyInfoResponse | ErrorResponse>(
ctx.status(200),
ctx.json({
statusCode: 200,
result: myAccount,
result: mockUser,
}),
);
else {
Expand Down Expand Up @@ -118,7 +118,7 @@ export const refresh = rest.post<never, never, RefreshResponse | ErrorResponse>(
ctx.json({
statusCode: 200,
result: {
...myAccount,
...mockUser,
accessToken: 'dev-accessToken',
},
}),
Expand All @@ -143,14 +143,14 @@ export const updateMyInfo = rest.patch<
>(apiUrl.auth.update(), async (req, res, ctx) => {
const { nickname }: UpdateMyInfoBody = await req.json();

if (nickname) myAccount.nickname = nickname;
if (nickname) mockUser.nickname = nickname;

return res(
ctx.status(200),
ctx.json({
statusCode: 200,
result: {
...myAccount,
...mockUser,
},
}),
);
Expand Down

0 comments on commit 1db00a7

Please sign in to comment.