From 80752a27b48099a794876e9c9ffb04d758859987 Mon Sep 17 00:00:00 2001 From: anjdydhody Date: Mon, 2 Dec 2024 22:25:51 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20=EC=A6=90=EA=B2=A8=EC=B0=BE?= =?UTF-8?q?=EA=B8=B0=20=EC=B7=A8=EC=86=8C=20API=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20#237?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/stock-bookmark.service.spec.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts index f537166..22a4a2f 100644 --- a/BE/src/stock/bookmark/stock-bookmark.service.spec.ts +++ b/BE/src/stock/bookmark/stock-bookmark.service.spec.ts @@ -1,5 +1,6 @@ import { Test } from '@nestjs/testing'; -import { BadRequestException } from '@nestjs/common'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { Column, PrimaryGeneratedColumn } from 'typeorm'; import { StockBookmarkRepository } from './stock-bookmark.repository'; import { StockDetailService } from '../detail/stock-detail.service'; import { StockBookmarkService } from './stock-bookmark.service'; @@ -14,6 +15,8 @@ describe('stock bookmark test', () => { existsBy: jest.fn(), create: jest.fn(), insert: jest.fn(), + findOneBy: jest.fn(), + remove: jest.fn(), }; const mockStockDetailService = { getInquirePrice: jest.fn() }; @@ -63,4 +66,31 @@ describe('stock bookmark test', () => { stockBookmarkService.registerBookmark(1, '005930'), ).rejects.toThrow(BadRequestException); }); + + it('존재하는 즐겨찾기를 취소할 경우, DB에서 해당 즐겨찾기가 삭제된다.', async () => { + jest.spyOn(stockBookmarkRepository, 'findOneBy').mockResolvedValue({ + id: 1, + stock_code: '005930', + user_id: 1, + }); + + const removeMock = jest.fn(); + jest + .spyOn(stockBookmarkRepository, 'remove') + .mockImplementation(removeMock); + + await stockBookmarkService.unregisterBookmark(1, '005930'); + + expect(removeMock).toHaveBeenCalled(); + }); + + it('존재하지 않는 즐겨찾기를 취소할 경우, NotFound 예외가 발생한다.', async () => { + jest + .spyOn(stockBookmarkRepository, 'findOneBy') + .mockResolvedValue(undefined); + + await expect( + stockBookmarkService.unregisterBookmark(1, '005930'), + ).rejects.toThrow(NotFoundException); + }); });