import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import request from 'supertest'; import { App } from 'supertest/types'; import { configureApplication } from '../app.config'; import { DEMO_CHARACTER_ID } from '../demo/demo-character.constants'; import { HuntingController } from './hunting.controller'; import { HuntingService } from './hunting.service'; describe('HuntingController', () => { let app: INestApplication; const startHunt = jest.fn(); const getActiveHunt = jest.fn(); beforeEach(async () => { startHunt.mockReset(); getActiveHunt.mockReset(); const module = await Test.createTestingModule({ controllers: [HuntingController], providers: [ { provide: HuntingService, useValue: { startHunt, getActiveHunt }, }, ], }).compile(); app = module.createNestApplication(); configureApplication(app); await app.init(); }); afterEach(async () => { await app.close(); }); it('delegates to huntingService.startHunt with the demo character id and returns its result', async () => { const huntResult = { id: 'hunt-1', location: { id: 'loc-1', key: 'burned-road', name: 'Verbrannte Strasse' }, encounters: [], }; startHunt.mockResolvedValue(huntResult); const response = await request(app.getHttpServer()) .post('/api/hunts') .expect(201); expect(startHunt).toHaveBeenCalledWith(DEMO_CHARACTER_ID); expect(response.body).toEqual(huntResult); }); it('serves the resumable hunt with its encounter statuses', async () => { const huntResult = { id: 'hunt-1', location: { id: 'loc-1', key: 'burned-road', name: 'Verbrannte Strasse' }, encounters: [ { id: 'encounter-1', monster: { key: 'ash-rat', name: 'Aschenratte', level: 1, artworkPath: '/images/monsters/ash-rat.png', }, dangerRating: 'WEAK', status: 'DEFEATED', }, ], }; getActiveHunt.mockResolvedValue(huntResult); const response = await request(app.getHttpServer()) .get('/api/hunts/active') .expect(200); expect(getActiveHunt).toHaveBeenCalledWith(DEMO_CHARACTER_ID); expect(response.body).toEqual(huntResult); }); it('serves an empty body when there is no resumable hunt', async () => { getActiveHunt.mockResolvedValue(null); const response = await request(app.getHttpServer()) .get('/api/hunts/active') .expect(200); expect(response.body).toEqual({}); }); });