feat: 支持单元测试
This commit is contained in:
120
src/modules/auth/auth.controller.spec.ts
Normal file
120
src/modules/auth/auth.controller.spec.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { ApiResponseDto } from '../../common/dto/api-response.dto';
|
||||
import type { JwtPayload } from '../../common/guards/jwt-auth.guard';
|
||||
|
||||
describe('AuthController', () => {
|
||||
let controller: AuthController;
|
||||
|
||||
const mockUser: JwtPayload = {
|
||||
sub: 'user-uuid-1',
|
||||
openid: 'wx-openid-123',
|
||||
};
|
||||
|
||||
const mockAuthService = {
|
||||
wxLogin: jest.fn(),
|
||||
getUserAssets: jest.fn(),
|
||||
consumePoint: jest.fn(),
|
||||
earnPoint: jest.fn(),
|
||||
getGameData: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AuthController],
|
||||
providers: [
|
||||
{ provide: AuthService, useValue: mockAuthService },
|
||||
{ provide: JwtService, useValue: { verifyAsync: jest.fn() } },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<AuthController>(AuthController);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('wxLogin', () => {
|
||||
it('should return success response with token and user info', async () => {
|
||||
const loginResponse = {
|
||||
token: 'jwt-token',
|
||||
user: { id: 'user-uuid-1', nickname: 'Test', points: 10 },
|
||||
};
|
||||
mockAuthService.wxLogin.mockResolvedValue(loginResponse);
|
||||
|
||||
const result = await controller.wxLogin({ code: 'wx-code-123' });
|
||||
|
||||
expect(result).toBeInstanceOf(ApiResponseDto);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual(loginResponse);
|
||||
expect(mockAuthService.wxLogin).toHaveBeenCalledWith('wx-code-123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserAssets', () => {
|
||||
it('should return success response with user points', async () => {
|
||||
mockAuthService.getUserAssets.mockResolvedValue({ points: 10 });
|
||||
|
||||
const result = await controller.getUserAssets(mockUser);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual({ points: 10 });
|
||||
expect(mockAuthService.getUserAssets).toHaveBeenCalledWith('user-uuid-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('consumePoint', () => {
|
||||
it('should return success response with updated points', async () => {
|
||||
mockAuthService.consumePoint.mockResolvedValue({ points: 9 });
|
||||
const dto = { reason: 'hint_unlock' as const, levelId: 'level-1', hintIndex: 2 };
|
||||
|
||||
const result = await controller.consumePoint(mockUser, dto);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual({ points: 9 });
|
||||
expect(mockAuthService.consumePoint).toHaveBeenCalledWith(
|
||||
'user-uuid-1',
|
||||
dto,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('earnPoint', () => {
|
||||
it('should return success response with updated points', async () => {
|
||||
mockAuthService.earnPoint.mockResolvedValue({ points: 11 });
|
||||
const dto = {
|
||||
reason: 'level_complete' as const,
|
||||
levelId: 'level-1',
|
||||
timeSpent: 30,
|
||||
};
|
||||
|
||||
const result = await controller.earnPoint(mockUser, dto);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual({ points: 11 });
|
||||
expect(mockAuthService.earnPoint).toHaveBeenCalledWith(
|
||||
'user-uuid-1',
|
||||
dto,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getGameData', () => {
|
||||
it('should return success response with game data', async () => {
|
||||
const gameData = {
|
||||
user: { id: 'user-uuid-1', points: 10 },
|
||||
completedLevelIds: ['level-1', 'level-2'],
|
||||
};
|
||||
mockAuthService.getGameData.mockResolvedValue(gameData);
|
||||
|
||||
const result = await controller.getGameData(mockUser);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data).toEqual(gameData);
|
||||
expect(mockAuthService.getGameData).toHaveBeenCalledWith('user-uuid-1');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user