|
| 1 | +import { prismaMock } from './singleton'; |
| 2 | +import templateService from "../src/service/templateService"; |
| 3 | +import templateRepository from '../src/repository/templateRepository'; |
| 4 | +import { userService } from '../src/service'; |
| 5 | + |
| 6 | + |
| 7 | +// test('should read all templates ', async () => { |
| 8 | + |
| 9 | +// const newUser = { |
| 10 | +// name: 'Rich', |
| 11 | +// email: 'hello@prisma.io', |
| 12 | +// kakaoId: 2020200, |
| 13 | +// appleId: "apple_id", |
| 14 | +// ageRange: "20~29", |
| 15 | +// gender: "male", |
| 16 | +// refreshToken: "refresh_token", |
| 17 | +// deviceToken: "device_token", |
| 18 | +// } |
| 19 | + |
| 20 | +// const createdNewUser = { |
| 21 | +// id:1, |
| 22 | +// ...newUser, |
| 23 | +// used_template:[1,2], |
| 24 | +// created_at: new Date(), |
| 25 | +// updated_at: new Date(), |
| 26 | +// kakao_id: BigInt(2020200), |
| 27 | +// apple_id: "apple_id", |
| 28 | +// age_range: "20~29", |
| 29 | +// gender: "male" |
| 30 | +// } |
| 31 | + |
| 32 | +// // mock으로 가짜 유저 생성 |
| 33 | +// prismaMock.user.create.mockResolvedValue(createdNewUser); |
| 34 | + |
| 35 | + |
| 36 | +// const templates = [ |
| 37 | +// { |
| 38 | +// id: 1, |
| 39 | +// info: "template_1_info", |
| 40 | +// title: "template_1_title", |
| 41 | +// shortInfo: "emplate_1_shortInfo", |
| 42 | +// questions: ["질문1", "질문2","질문3"], |
| 43 | +// guideline: "template_1_guideline", |
| 44 | +// hints: ["힌트1", "힌트2","힌트3"], |
| 45 | +// subtitles: ["소제목1", "소제목2","소제목3"] |
| 46 | +// }, |
| 47 | +// { |
| 48 | +// id: 2, |
| 49 | +// info: "template_2_info", |
| 50 | +// title: "template_2_title", |
| 51 | +// shortInfo: "emplate_2_shortInfo", |
| 52 | +// questions: ["질문1", "질문2","질문3"], |
| 53 | +// guideline: "template_2_guideline", |
| 54 | +// hints: ["힌트1", "힌트2","힌트3"], |
| 55 | +// subtitles: ["소제목1", "소제목2","소제목3"] |
| 56 | +// } |
| 57 | +// ] |
| 58 | +// (templateRepository.findAllTemplate as jest.Mock).mockResolvedValue(templates); |
| 59 | + |
| 60 | +// const result = [ |
| 61 | +// { |
| 62 | +// templateId: templates[0].id, |
| 63 | +// title: templates[0].title, |
| 64 | +// shortInfo: templates[0].shortInfo, |
| 65 | +// info: templates[0].info, |
| 66 | +// hasUsed: createdNewUser.used_template.includes(templates[0].id), |
| 67 | +// }, |
| 68 | +// { |
| 69 | +// templateId: templates[1].id, |
| 70 | +// title: templates[1].title, |
| 71 | +// shortInfo: templates[1].shortInfo, |
| 72 | +// info: templates[1].info, |
| 73 | +// hasUsed: createdNewUser.used_template.includes(templates[1].id), |
| 74 | +// } |
| 75 | +// ] |
| 76 | + |
| 77 | +// await expect(templateRepository.findAllTemplate()).resolves.toEqual(templates); |
| 78 | +// await expect(templateService.getAllTemplate(createdNewUser.id)).resolves.toEqual(result); |
| 79 | + |
| 80 | +// }) |
| 81 | + |
| 82 | +describe('getAllTemplate', () => { |
| 83 | + it('should return all templates with hasUsed flag', async () => { |
| 84 | + const mockTemplates = [ |
| 85 | + { |
| 86 | + id: 1, |
| 87 | + info: "template_1_info", |
| 88 | + title: "template_1_title", |
| 89 | + shortInfo: "emplate_1_shortInfo", |
| 90 | + questions: ["질문1", "질문2","질문3"], |
| 91 | + guideline: "template_1_guideline", |
| 92 | + hints: ["힌트1", "힌트2","힌트3"], |
| 93 | + subtitles: ["소제목1", "소제목2","소제목3"] |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 2, |
| 97 | + info: "template_2_info", |
| 98 | + title: "template_2_title", |
| 99 | + shortInfo: "emplate_2_shortInfo", |
| 100 | + questions: ["질문1", "질문2","질문3"], |
| 101 | + guideline: "template_2_guideline", |
| 102 | + hints: ["힌트1", "힌트2","힌트3"], |
| 103 | + subtitles: ["소제목1", "소제목2","소제목3"] |
| 104 | + } |
| 105 | + ]; |
| 106 | + const mockUser = { |
| 107 | + id:1, |
| 108 | + name: "user", |
| 109 | + email: "email@prisma.io", |
| 110 | + used_template:[1,2], |
| 111 | + created_at: new Date(), |
| 112 | + updated_at: new Date(), |
| 113 | + kakao_id: BigInt(2020200), |
| 114 | + apple_id: "apple_id", |
| 115 | + age_range: "20~29", |
| 116 | + gender: "male" |
| 117 | + }; |
| 118 | + |
| 119 | + // Mock repository behavior |
| 120 | + jest.spyOn(templateRepository, 'findAllTemplate').mockResolvedValue(mockTemplates); |
| 121 | + |
| 122 | + // Mock service behavior |
| 123 | + jest.spyOn(userService, 'getUserById').mockResolvedValue(mockUser); |
| 124 | + |
| 125 | + const result = await templateService.getAllTemplate(1); |
| 126 | + |
| 127 | + expect(result).toEqual([ |
| 128 | + { |
| 129 | + templateId: mockTemplates[0].id, |
| 130 | + title: mockTemplates[0].title, |
| 131 | + shortInfo: mockTemplates[0].shortInfo, |
| 132 | + info: mockTemplates[0].info, |
| 133 | + hasUsed: true |
| 134 | + }, |
| 135 | + { |
| 136 | + templateId: mockTemplates[1].id, |
| 137 | + title: mockTemplates[1].title, |
| 138 | + shortInfo: mockTemplates[1].shortInfo, |
| 139 | + info: mockTemplates[1].info, |
| 140 | + hasUsed: true |
| 141 | + } |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +}); |
0 commit comments