Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed Mar 30, 2022
1 parent e83a55e commit 7d19157
Showing 1 changed file with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
MongooseModule,
} from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { plainToInstance } from 'class-transformer';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { Connection, Model } from 'mongoose';
import { Connection, Model, Types } from 'mongoose';
import { Subject } from 'rxjs';
import {
ServemeTfGameServer,
Expand All @@ -21,7 +22,14 @@ import { ServemeTfService } from './serveme-tf.service';

jest.mock('@/game-servers/services/game-servers.service');
jest.mock('./serveme-tf-api.service');
jest.mock('@/events/events');

jest.mock('rxjs/operators', () => {
const operators = jest.requireActual('rxjs/operators');
return {
...operators,
delay: jest.fn(() => (s) => s),
};
});

describe('ServemeTfService', () => {
let service: ServemeTfService;
Expand All @@ -30,15 +38,11 @@ describe('ServemeTfService', () => {
let servemeTfGameServerModel: Model<ServemeTfGameServerDocument>;
let servemeTfApiService: jest.Mocked<ServemeTfApiService>;
let gameServersService: jest.Mocked<GameServersService>;
let gameServerUpdated: Subject<any>;
let events: Events;

beforeAll(async () => (mongod = await MongoMemoryServer.create()));
afterAll(async () => await mongod.stop());

beforeEach(() => {
gameServerUpdated = new Subject();
});

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
Expand All @@ -54,12 +58,7 @@ describe('ServemeTfService', () => {
ServemeTfService,
GameServersService,
ServemeTfApiService,
{
provide: Events,
useValue: {
gameServerUpdated,
},
},
Events,
],
}).compile();

Expand All @@ -70,10 +69,11 @@ describe('ServemeTfService', () => {
);
servemeTfApiService = module.get(ServemeTfApiService);
gameServersService = module.get(GameServersService);
events = module.get(Events);
});

beforeEach(async () => {
await service.onModuleInit();
beforeEach(() => {
service.onModuleInit();
});

afterEach(async () => {
Expand All @@ -89,6 +89,31 @@ describe('ServemeTfService', () => {
expect(gameServersService.registerProvider).toHaveBeenCalledWith(service);
});

describe('when a server is released', () => {
beforeEach(() => {
const oldGameServer = plainToInstance(ServemeTfGameServer, {
provider: 'serveme.tf',
game: new Types.ObjectId(),
reservation: {
id: 42,
},
});

const newGameServer = plainToInstance(ServemeTfGameServer, {
provider: 'serveme.tf',
reservation: {
id: 42,
},
});

events.gameServerUpdated.next({ oldGameServer, newGameServer });
});

it('should end the reservation', () => {
expect(servemeTfApiService.endServerReservation).toHaveBeenCalledWith(42);
});
});

describe('#findFirstGameServer()', () => {
describe('when the reservation is successful', () => {
beforeEach(() => {
Expand Down

0 comments on commit 7d19157

Please sign in to comment.