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

주문 테스트 데이터 및 서비스 로직 개선 #175

Merged
merged 4 commits into from
Mar 16, 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
62 changes: 62 additions & 0 deletions server/src/fixture/orders.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Delivery from 'src/delivery/domain/delivery';
import Order from 'src/orders/domain/order';

export const orders = [
{
id: 1,
bookTitle: '어린왕자',
totalQuantity: 3,
totalPrice: 6000,
userId: 0,
delivery: {
id: 0,
address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국',
contact: '010-1234-5667',
receiver: '홍길동',
},
deliveryId: 0,
createdAt: '2024-03-14T15:59:31.520Z',
},
{
id: 2,
bookTitle: '어린왕자',
totalQuantity: 3,
totalPrice: 6000,
userId: 0,
delivery: {
id: 0,
address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국',
contact: '010-1234-5667',
receiver: '홍길동',
},
deliveryId: 0,
createdAt: '2024-03-14T15:59:31.520Z',
},
];

export const mockOrders = [
new Order({
id: 1,
bookTitle: 'Book 1',
totalQuantity: 2,
totalPrice: 20,
delivery: new Delivery({
address: '123 Street',
receiver: 'John Doe',
contact: '1234567890',
}),
}),
new Order({
id: 2,
bookTitle: 'Book 2',
totalQuantity: 1,
totalPrice: 15,
delivery: new Delivery({
address: '456 Avenue',
receiver: 'Jane Smith',
contact: '9876543210',
}),
}),
];

export const orderDetailMock = [{ author: '걍구두', bookId: 2, bookTitle: '신델렐라', price: 20000, quantity: 3 }];
41 changes: 41 additions & 0 deletions server/src/orders/application/orders-detail.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { orderDetailMock } from 'src/fixture/orders.fixture';

import { findAllWithOrderId } from '../domain/order.repository';
import { getDetailOrders } from './orders-detail.service';

jest.mock('../domain/order.repository');

describe('orderDetail Service', () => {
describe('getDetailOrders', () => {
const ORDER_ID = 1;
const NOT_FOUNT_ID = 9999;

beforeEach(() => {
(findAllWithOrderId as jest.Mock).mockReturnValue(orderDetailMock);
});
context('장바구니에 주문 목록 상품을 선택하면', () => {
it('주문 목록 상품을 반환한다.', async () => {
const order = await getDetailOrders(ORDER_ID);

expect(order).toStrictEqual([
{ author: '걍구두', bookId: 2, bookTitle: '신델렐라', price: 20000, quantity: 3 },
]);
});
});

context('장바구니에 주문 목록 상품이 존재하지 않으면', () => {
beforeEach(() => {
(findAllWithOrderId as jest.Mock).mockReturnValue([]);
});
it('error를 던져야 한다', async () => {
await expect(getDetailOrders(NOT_FOUNT_ID)).rejects.toThrow(
new HttpException('주문 된 상품을 찾을 수 없습니다.', StatusCodes.NOT_FOUND),
);
});
});
});
});
56 changes: 56 additions & 0 deletions server/src/orders/application/orders-list.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { mockOrders } from 'src/fixture/orders.fixture';

import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { findAll } from '../domain/order.repository';
import { getAllOrders } from './orders-list.service';

jest.mock('../domain/order.repository.ts');

describe('orderList Service', () => {
beforeEach(() => {
(findAll as jest.Mock).mockResolvedValue(mockOrders);
});

describe('getAllOrders', () => {
context('주문 목록을 조회하면', () => {
it('주문 목록 리스트를 반환한다.', async () => {
const orders = await getAllOrders();

expect(orders).toEqual([
{
address: '123 Street',
bookTitle: 'Book 1',
contact: '1234567890',
orderId: 1,
receiver: 'John Doe',
totalPrice: 20,
totalQuantity: 2,
},
{
address: '456 Avenue',
bookTitle: 'Book 2',
contact: '9876543210',
orderId: 2,
receiver: 'Jane Smith',
totalPrice: 15,
totalQuantity: 1,
},
]);
});
});

context('주문 목록에 존재하지 않으면', () => {
beforeEach(() => {
(findAll as jest.Mock).mockResolvedValue([]);
});
it('error를 던져야 한다', async () => {
await expect(getAllOrders()).rejects.toThrow(
new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND),
);
});
});
});
});
7 changes: 7 additions & 0 deletions server/src/orders/application/orders-list.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { findAll } from '../domain/order.repository';

export const getAllOrders = async () => {
const orders = await findAll();
if (orders.length === 0) {
throw new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND);
}

const findOrder = orders.map((item) => {
return {
Expand Down
50 changes: 50 additions & 0 deletions server/src/orders/web/orders-list.controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import app from 'src/app';
import request from 'supertest';

import { StatusCodes } from 'http-status-codes';
import HttpException from 'src/utils/httpException';

import { getAllOrders } from '../application/orders-list.service';

jest.mock('../application/orders-list.service.ts');

describe('getAllOrdersHandler Controller', () => {
describe('GET /orders', () => {
context('주문 조회에 성공하면', () => {
beforeEach(() => {
(getAllOrders as jest.Mock).mockReturnValue([
{
address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국',
bookTitle: '어린왕자',
contact: '010-1234-5667',
orderId: 1,
receiver: '홍길동',
totalPrice: 6000,
totalQuantity: 3,
},
{
address: '강원도 춘천시 동내면 대룡산길 227-314 24408 한국',
bookTitle: '어린왕자',
contact: '010-1234-5667',
orderId: 2,
receiver: '홍길동',
totalPrice: 6000,
totalQuantity: 3,
},
]);
});

it('200 상태코드와 응답 메세지를 반환한다.', async () => {
const { status, body } = await request(app).get('/orders');

Expand All @@ -30,5 +60,25 @@ describe('getAllOrdersHandler Controller', () => {
]);
});
});

context('주문 조회 요청 실패 시', () => {
beforeEach(() => {
(getAllOrders as jest.Mock).mockRejectedValue(
new HttpException('주문 목록을 찾을 수 없습니다.', StatusCodes.NOT_FOUND),
);
});

it('404 상태코드와 에러 메세지를 반환한다', async () => {
const { status, body } = await request(app).get('/orders');

expect(status).toBe(404);
expect(body).toEqual({
message: '주문 목록을 찾을 수 없습니다.',
status: 404,
success: false,
timestamp: expect.any(String),
});
});
});
});
});