|
| 1 | +import { INestApplication } from '@nestjs/common'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import { UserRepository } from '../../../src/user/repository/user.repository'; |
| 4 | +import { ActivityRepository } from '../../../src/activity/repository/activity.repository'; |
| 5 | +import { UserFixture } from '../../fixture/user.fixture'; |
| 6 | +import { ActivityFixture } from '../../fixture/activity.fixture'; |
| 7 | +import { User } from '../../../src/user/entity/user.entity'; |
| 8 | + |
| 9 | +describe('GET /api/activity/:userId E2E Test', () => { |
| 10 | + let app: INestApplication; |
| 11 | + let userRepository: UserRepository; |
| 12 | + let activityRepository: ActivityRepository; |
| 13 | + let testUser: User; |
| 14 | + let activitiesData: Array<{ activityDate: Date; viewCount: number }>; |
| 15 | + |
| 16 | + beforeAll(async () => { |
| 17 | + app = global.testApp; |
| 18 | + userRepository = app.get(UserRepository); |
| 19 | + activityRepository = app.get(ActivityRepository); |
| 20 | + |
| 21 | + testUser = await userRepository.save(UserFixture.createUserFixture({})); |
| 22 | + |
| 23 | + activitiesData = [ |
| 24 | + { activityDate: new Date('2024-01-15'), viewCount: 5 }, |
| 25 | + { activityDate: new Date('2024-01-16'), viewCount: 3 }, |
| 26 | + { activityDate: new Date('2024-06-01'), viewCount: 8 }, |
| 27 | + { activityDate: new Date('2024-12-25'), viewCount: 2 }, |
| 28 | + ]; |
| 29 | + |
| 30 | + const activities = ActivityFixture.createMultipleActivitiesFixture( |
| 31 | + testUser, |
| 32 | + activitiesData, |
| 33 | + ); |
| 34 | + |
| 35 | + await activityRepository.save(activities); |
| 36 | + }); |
| 37 | + |
| 38 | + it('존재하는 사용자의 활동 데이터를 정상적으로 조회한다.', async () => { |
| 39 | + // given |
| 40 | + const userId = testUser.id; |
| 41 | + const year = activitiesData[0].activityDate.getFullYear(); |
| 42 | + |
| 43 | + // when |
| 44 | + const response = await request(app.getHttpServer()) |
| 45 | + .get(`/api/activity/${userId}`) |
| 46 | + .query({ year }); |
| 47 | + |
| 48 | + // then |
| 49 | + expect(response.status).toBe(200); |
| 50 | + expect(response.body.message).toBe('요청이 성공적으로 처리되었습니다.'); |
| 51 | + |
| 52 | + const { data } = response.body; |
| 53 | + expect(data.maxStreak).toBe(testUser.maxStreak); |
| 54 | + expect(data.currentStreak).toBe(testUser.currentStreak); |
| 55 | + expect(data.totalViews).toBe(testUser.totalViews); |
| 56 | + |
| 57 | + expect(data.dailyActivities).toHaveLength(activitiesData.length); |
| 58 | + expect(data.dailyActivities).toEqual( |
| 59 | + expect.arrayContaining( |
| 60 | + activitiesData.map((activity) => |
| 61 | + expect.objectContaining({ |
| 62 | + date: activity.activityDate.toISOString().split('T')[0], |
| 63 | + viewCount: activity.viewCount, |
| 64 | + }), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('다른 연도를 요청하면 해당 연도의 데이터만 조회된다.', async () => { |
| 71 | + // given |
| 72 | + const userId = testUser.id; |
| 73 | + const year = activitiesData[0].activityDate.getFullYear() - 1; |
| 74 | + |
| 75 | + // when |
| 76 | + const response = await request(app.getHttpServer()) |
| 77 | + .get(`/api/activity/${userId}`) |
| 78 | + .query({ year }); |
| 79 | + |
| 80 | + // then |
| 81 | + expect(response.status).toBe(200); |
| 82 | + expect(response.body.message).toBe('요청이 성공적으로 처리되었습니다.'); |
| 83 | + |
| 84 | + const { data } = response.body; |
| 85 | + expect(data.maxStreak).toBe(testUser.maxStreak); |
| 86 | + expect(data.currentStreak).toBe(testUser.currentStreak); |
| 87 | + expect(data.totalViews).toBe(testUser.totalViews); |
| 88 | + expect(data.dailyActivities).toHaveLength(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it('존재하지 않는 사용자 ID로 요청하면 404 에러를 반환한다.', async () => { |
| 92 | + // given |
| 93 | + const nonExistentUserId = 99999; |
| 94 | + const year = activitiesData[0].activityDate.getFullYear(); |
| 95 | + |
| 96 | + // when |
| 97 | + const response = await request(app.getHttpServer()) |
| 98 | + .get(`/api/activity/${nonExistentUserId}`) |
| 99 | + .query({ year }); |
| 100 | + |
| 101 | + // then |
| 102 | + expect(response.status).toBe(404); |
| 103 | + expect(response.body.message).toBe('존재하지 않는 사용자입니다.'); |
| 104 | + }); |
| 105 | +}); |
0 commit comments