Skip to content

Commit

Permalink
Test: 대시보드 테스트 코드 작성
Browse files Browse the repository at this point in the history
- Dto, Service, Controller 테스트 코드 작성

Related to #29
  • Loading branch information
Zamoca42 committed Jan 6, 2024
1 parent cebdd9a commit 1505374
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 1 deletion.
77 changes: 77 additions & 0 deletions src/dashboard/dashboard.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DashboardController } from './dashboard.controller';
import { DashboardService } from './dashboard.service';
import { ResponseEntity } from '../common/entity/response.entity';
import { Statistics } from './dto/get-statistics.dto';
import { HttpStatus } from '@nestjs/common';

class MockDashboardService {
combineResultsByDate = jest.fn().mockResolvedValue([]);
countUsersByDate = jest.fn().mockResolvedValue([]);
countReviewsByDate = jest.fn().mockResolvedValue([]);
}

describe('DashboardController', () => {
let controller: DashboardController;
let service: DashboardService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DashboardController],
providers: [
{ provide: DashboardService, useClass: MockDashboardService },
],
}).compile();

controller = module.get<DashboardController>(DashboardController);
service = module.get<DashboardService>(DashboardService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('combineResultsByDate', () => {
it('should return combined results by date', async () => {
const expectedResult: ResponseEntity<Statistics[]> =
ResponseEntity.OK_WITH('날짜별 리뷰수, 가입자수 입니다.', []);

jest.spyOn(service, 'combineResultsByDate').mockResolvedValue([]);

const result = await controller.combineResultsByDate();

expect(result).toEqual(expectedResult);
expect(result.code).toEqual(HttpStatus.OK);
expect(service.combineResultsByDate).toHaveBeenCalled();
});
});

describe('countUsersByDate', () => {
it('should return count of users by date', async () => {
const expectedResult: ResponseEntity<Statistics[]> =
ResponseEntity.OK_WITH('날짜별 유저수 입니다.', []);
jest.spyOn(service, 'countUsersByDate').mockResolvedValue([]);

const result = await controller.countUsersByDate();

expect(result).toEqual(expectedResult);
expect(result.code).toEqual(HttpStatus.OK);
expect(service.countUsersByDate).toHaveBeenCalled();
});
});

describe('countReviewsByDate', () => {
it('should return count of reviews by date', async () => {
const expectedResult: ResponseEntity<Statistics[]> =
ResponseEntity.OK_WITH('날짜별 리뷰수 입니다.', []);

jest.spyOn(service, 'countReviewsByDate').mockResolvedValue([]);

const result = await controller.countReviewsByDate();

expect(result).toEqual(expectedResult);
expect(result.code).toEqual(HttpStatus.OK);
expect(service.countReviewsByDate).toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion src/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { ResponseEntity } from 'src/common/entity/response.entity';
import { ResponseEntity } from '../common/entity/response.entity';
import { Statistics } from './dto/get-statistics.dto';
import { ApiTags } from '@nestjs/swagger';
import { SwaggerAPI } from '../common/swagger/api.decorator';
Expand Down
104 changes: 104 additions & 0 deletions src/dashboard/dashboard.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DashboardService } from './dashboard.service';
import { UserRepository } from '../user/repository/user.repository';
import { ReviewRepository } from '../review/repository/review.repository';
import { RawCountByDate, Statistics } from './dto/get-statistics.dto';

class MockUserRepository {
countUsersByDate = jest.fn();
}

class MockReviewRepository {
countReviewsByDate = jest.fn();
}

describe('DashboardService', () => {
let dashboardService: DashboardService;
let userRepository: UserRepository;
let reviewRepository: ReviewRepository;
let expectedUserResults: RawCountByDate[];
let expectedReviewResults: RawCountByDate[];

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DashboardService,
{ provide: UserRepository, useClass: MockUserRepository },
{ provide: ReviewRepository, useClass: MockReviewRepository },
],
}).compile();

dashboardService = module.get<DashboardService>(DashboardService);
userRepository = module.get<UserRepository>(UserRepository);
reviewRepository = module.get<ReviewRepository>(ReviewRepository);

expectedUserResults = [
{ created: new Date('2022-01-01'), user: 10, review: undefined },
{ created: new Date('2022-01-02'), user: 15, review: undefined },
];

expectedReviewResults = [
{ created: new Date('2022-01-01'), user: undefined, review: 5 },
{ created: new Date('2022-01-02'), user: undefined, review: 8 },
];

jest
.spyOn(userRepository, 'countUsersByDate')
.mockResolvedValue(expectedUserResults);

jest
.spyOn(reviewRepository, 'countReviewsByDate')
.mockResolvedValue(expectedReviewResults);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should be defined', () => {
expect(dashboardService).toBeDefined();
expect(userRepository).toBeDefined();
expect(reviewRepository).toBeDefined();
});

describe('countUsersByDate', () => {
it('SUCESS: 날짜별 가입자수 생성', async () => {
const results = await dashboardService.countUsersByDate();

expect(results).toEqual(expectedUserResults);
expect(userRepository.countUsersByDate).toHaveBeenCalled();
});
});

describe('countReviewsByDate', () => {
it('SUCESS: 날짜별 리뷰수 생성', async () => {
const results = await dashboardService.countReviewsByDate();

expect(results).toEqual(expectedReviewResults);
expect(reviewRepository.countReviewsByDate).toHaveBeenCalled();
});
});

describe('combineResultsByDate', () => {
it('SUCESS: 날짜별 가입자수, 리뷰수 통합 생성', async () => {
const expectedResults: RawCountByDate[] = [
{ created: new Date('2022-01-01'), user: 10, review: 5 },
{ created: new Date('2022-01-02'), user: 15, review: 8 },
];

jest.spyOn(Statistics, 'mergeResults').mockReturnValue(expectedResults);
jest.spyOn(Statistics, 'compare').mockReturnValue(0);

const results = await dashboardService.combineResultsByDate();

expect(results).toEqual(expectedResults);
expect(userRepository.countUsersByDate).toHaveBeenCalled();
expect(reviewRepository.countReviewsByDate).toHaveBeenCalled();
expect(Statistics.mergeResults).toHaveBeenCalledWith([
...expectedUserResults,
...expectedReviewResults,
]);
expect(Statistics.compare).toHaveBeenCalled();
});
});
});
100 changes: 100 additions & 0 deletions src/dashboard/dto/get-statistics.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { RawCountByDate, Statistics } from './get-statistics.dto';

describe('Statistics', () => {
const rawCountByDate: RawCountByDate = {
created: new Date(),
user: 10,
review: 5,
};

describe('constructor()', () => {
it('SUCCESS: RawCountByDate으로 Statistics 인스턴스 생성', () => {
const statistics = new Statistics(rawCountByDate);

expect(statistics).toBeInstanceOf(Statistics);
expect(statistics.date).toEqual(rawCountByDate.created);
expect(statistics.user).toEqual(rawCountByDate.user);
expect(statistics.review).toEqual(rawCountByDate.review);
});
});

describe('compare()', () => {
it('SUCCES: 두 개의 RawCountByDate 객체에 생성 일자 비교', () => {
const a: RawCountByDate = {
created: new Date('2022-01-01'),
user: 10,
review: 5,
};
const b: RawCountByDate = {
created: new Date('2022-01-02'),
user: 15,
review: 8,
};

const result = Statistics.compare(a, b);

expect(result).toBeGreaterThan(0);
});
});

describe('mergeResults()', () => {
it('SUCCES: 배열로 받은 RawCountByDate 객체 합치기', () => {
const results: RawCountByDate[] = [
{
created: new Date('2022-01-01'),
user: 10,
review: 5,
},
{
created: new Date('2022-01-01'),
user: 5,
review: 3,
},
{
created: new Date('2022-01-02'),
user: 15,
review: 8,
},
];

const mergedResults = Statistics.mergeResults(results);

expect(mergedResults.length).toEqual(2);
expect(mergedResults[0].created).toEqual(new Date('2022-01-01'));
expect(mergedResults[0].user).toEqual(15);
expect(mergedResults[0].review).toEqual(8);
expect(mergedResults[1].created).toEqual(new Date('2022-01-02'));
expect(mergedResults[1].user).toEqual(15);
expect(mergedResults[1].review).toEqual(8);
});
});

describe('list()', () => {
it('SUCCES: RawCountByDate 배열을 Statistic 배열 인스턴스 생성', () => {
const response: RawCountByDate[] = [
{
created: new Date('2022-01-01'),
user: 10,
review: 5,
},
{
created: new Date('2022-01-02'),
user: 15,
review: 8,
},
];

const statisticsList = Statistics.list(response);

expect(statisticsList.length).toEqual(2);
expect(statisticsList[0]).toBeInstanceOf(Statistics);
expect(statisticsList[0].date).toEqual(new Date('2022-01-01'));
expect(statisticsList[0].user).toEqual(10);
expect(statisticsList[0].review).toEqual(5);
expect(statisticsList[1]).toBeInstanceOf(Statistics);
expect(statisticsList[1].date).toEqual(new Date('2022-01-02'));
expect(statisticsList[1].user).toEqual(15);
expect(statisticsList[1].review).toEqual(8);
});
});
});

0 comments on commit 1505374

Please sign in to comment.