|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
|
|
import { RankController } from './rank.controller';
|
|
|
|
import { RankService } from './rank.service';
|
|
|
|
|
|
|
|
describe('RankController', () => {
|
|
|
|
let controller: RankController;
|
|
|
|
|
|
|
|
const mockRankList = [];
|
|
|
|
|
|
|
|
const mockRank = {
|
|
|
|
id: `${Date.now()}`,
|
|
|
|
points: 100,
|
|
|
|
createdAt: Date.now(),
|
|
|
|
updatedAt: Date.now(),
|
|
|
|
userId: `${Date.now()}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
mockRankList.push(mockRank);
|
|
|
|
|
|
|
|
const mockRankService = {
|
|
|
|
findRankList: jest.fn(() => mockRankList),
|
|
|
|
findByUserLatest: jest.fn((_: string) => mockRank),
|
|
|
|
findByUser: jest.fn((_: string) => mockRankList),
|
|
|
|
create: jest.fn((pts: number) => ({
|
|
|
|
...mockRank,
|
|
|
|
points: mockRank.points + pts
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockGuard = {
|
|
|
|
canActivate: jest.fn(() => true)
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
controllers: [RankController],
|
|
|
|
providers: [RankService],
|
|
|
|
}).overrideProvider(RankService)
|
|
|
|
.useValue(mockRankService)
|
|
|
|
.overrideGuard(JwtAuthGuard)
|
|
|
|
.useValue(mockGuard)
|
|
|
|
.compile();
|
|
|
|
|
|
|
|
controller = module.get<RankController>(RankController);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(controller).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return rank list', () => {
|
|
|
|
expect(controller.getRankList()).toEqual(mockRankList);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get latest score of user', () => {
|
|
|
|
expect(controller.getUserRank(mockRank.userId)).toEqual(mockRank);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get user rank history', () => {
|
|
|
|
expect(controller.getUserHistory(mockRank.userId)).toEqual(mockRankList);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create new rank and add 20 points', () => {
|
|
|
|
const req = {
|
|
|
|
user: { id: mockRank.id },
|
|
|
|
};
|
|
|
|
expect(controller.add20Points(req)).toEqual({
|
|
|
|
...mockRank,
|
|
|
|
points: 120,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create new rank and add 60 points', () => {
|
|
|
|
const req = {
|
|
|
|
user: { id: mockRank.id },
|
|
|
|
};
|
|
|
|
expect(controller.add60Points(req)).toEqual({
|
|
|
|
...mockRank,
|
|
|
|
points: 160,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create new rank and add 100 points', () => {
|
|
|
|
const req = {
|
|
|
|
user: { id: mockRank.id },
|
|
|
|
};
|
|
|
|
expect(controller.add100Points(req)).toEqual({
|
|
|
|
...mockRank,
|
|
|
|
points: 200,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|