|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing'; |
2 | 2 | import { EdgeController } from './edge.controller'; |
3 | 3 | import { EdgeService } from './edge.service'; |
| 4 | +import { CreateEdgeDto } from './dtos/createEdge.dto'; |
| 5 | +import { EdgeResponseMessage } from './edge.controller'; |
| 6 | +import { EdgeNotFoundException } from '../exception/edge.exception'; |
| 7 | +import { Edge } from './edge.entity'; |
| 8 | +import { Node } from '../node/node.entity'; |
4 | 9 |
|
5 | 10 | describe('EdgeController', () => { |
6 | 11 | let controller: EdgeController; |
| 12 | + let edgeService: EdgeService; |
7 | 13 |
|
8 | 14 | beforeEach(async () => { |
9 | 15 | const module: TestingModule = await Test.createTestingModule({ |
10 | 16 | controllers: [EdgeController], |
11 | 17 | providers: [ |
12 | 18 | { |
13 | 19 | provide: EdgeService, |
14 | | - useValue: {}, |
| 20 | + useValue: { |
| 21 | + createEdge: jest.fn(), |
| 22 | + deleteEdge: jest.fn(), |
| 23 | + findEdges: jest.fn(), |
| 24 | + }, |
15 | 25 | }, |
16 | 26 | ], |
17 | 27 | }).compile(); |
18 | 28 |
|
19 | 29 | controller = module.get<EdgeController>(EdgeController); |
| 30 | + edgeService = module.get<EdgeService>(EdgeService); |
20 | 31 | }); |
21 | 32 |
|
22 | 33 | it('컨트롤러 클래스가 정상적으로 인스턴스화된다.', () => { |
23 | 34 | expect(controller).toBeDefined(); |
24 | 35 | }); |
| 36 | + |
| 37 | + describe('createEdge', () => { |
| 38 | + it('엣지가 성공적으로 만들어진다', async () => { |
| 39 | + const dto: CreateEdgeDto = { fromNode: 1, toNode: 3 }; |
| 40 | + const expectedResponse = { |
| 41 | + message: EdgeResponseMessage.EDGE_CREATED, |
| 42 | + }; |
| 43 | + |
| 44 | + jest.spyOn(edgeService, 'createEdge').mockResolvedValue(undefined); |
| 45 | + const result = await controller.createEdge(dto); |
| 46 | + |
| 47 | + expect(edgeService.createEdge).toHaveBeenCalledWith(dto); |
| 48 | + expect(result).toEqual(expectedResponse); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('deleteEdge', () => { |
| 53 | + it('id에 해당하는 엣지를 찾아 삭제한다.', async () => { |
| 54 | + const id = 2; |
| 55 | + const expectedResponse = { |
| 56 | + message: EdgeResponseMessage.EDGE_DELETED, |
| 57 | + }; |
| 58 | + |
| 59 | + const result = await controller.deleteEdge(id); |
| 60 | + |
| 61 | + expect(edgeService.deleteEdge).toHaveBeenCalledWith(id); |
| 62 | + expect(result).toEqual(expectedResponse); |
| 63 | + }); |
| 64 | + |
| 65 | + it('id에 해당하는 엣지가 존재하지 않으면 NodeNotFoundException을 throw한다.', async () => { |
| 66 | + jest |
| 67 | + .spyOn(edgeService, 'deleteEdge') |
| 68 | + .mockRejectedValue(new EdgeNotFoundException()); |
| 69 | + |
| 70 | + await expect(controller.deleteEdge(1)).rejects.toThrow( |
| 71 | + EdgeNotFoundException, |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('findEdges', () => { |
| 77 | + it('모든 엣지 목록을 반환한다.', async () => { |
| 78 | + const node3 = { |
| 79 | + id: 3, |
| 80 | + x: 0, |
| 81 | + y: 0, |
| 82 | + title: 'Node Title', |
| 83 | + page: null, |
| 84 | + outgoingEdges: [], |
| 85 | + incomingEdges: [], |
| 86 | + } as Node; |
| 87 | + const node4 = { |
| 88 | + id: 4, |
| 89 | + x: 0, |
| 90 | + y: 0, |
| 91 | + title: 'Node Title', |
| 92 | + page: null, |
| 93 | + outgoingEdges: [], |
| 94 | + incomingEdges: [], |
| 95 | + } as Node; |
| 96 | + const node5 = { |
| 97 | + id: 5, |
| 98 | + x: 0, |
| 99 | + y: 0, |
| 100 | + title: 'Node Title', |
| 101 | + page: null, |
| 102 | + outgoingEdges: [], |
| 103 | + incomingEdges: [], |
| 104 | + } as Node; |
| 105 | + |
| 106 | + const expectedEdges = [ |
| 107 | + { id: 1, fromNode: node3, toNode: node5 }, |
| 108 | + { id: 2, fromNode: node3, toNode: node4 }, |
| 109 | + ] as Edge[]; |
| 110 | + node3.outgoingEdges = []; |
| 111 | + |
| 112 | + jest.spyOn(edgeService, 'findEdges').mockResolvedValue(expectedEdges); |
| 113 | + |
| 114 | + await expect(controller.findEdges()).resolves.toEqual({ |
| 115 | + message: EdgeResponseMessage.EDGE_ALL_RETURNED, |
| 116 | + edges: expectedEdges, |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
25 | 120 | }); |
0 commit comments