You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB
TypeScript

2 years ago
import { Test, TestingModule } from '@nestjs/testing';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
2 years ago
import { RankController } from './rank.controller';
import { RankService } from './rank.service';
2 years ago
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)
};
2 years ago
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RankController],
providers: [RankService],
}).overrideProvider(RankService)
.useValue(mockRankService)
.overrideGuard(JwtAuthGuard)
.useValue(mockGuard)
.compile();
2 years ago
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,
});
});
2 years ago
});