Skip to content

Commit 6639e53

Browse files
committed
test: game instance
1 parent b8d3f9e commit 6639e53

File tree

2 files changed

+111
-17
lines changed

2 files changed

+111
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,95 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { afterEach } from 'node:test';
2+
import type { Observable } from 'rxjs';
3+
import { beforeEach, describe, expect, it, vi } from 'vitest';
4+
import type { GameEvent } from '../../../common/game/types.js';
5+
import type { SGEGameCredentials } from '../../sge/types.js';
6+
import { GameServiceImpl } from '../game.service.js';
7+
import type { GameService } from '../types.js';
8+
9+
const { mockGameService } = await vi.hoisted(async () => {
10+
const mockGameService = {
11+
connect: vi.fn(),
12+
disconnect: vi.fn(),
13+
send: vi.fn(),
14+
};
15+
16+
return {
17+
mockGameService,
18+
};
19+
});
20+
21+
vi.mock('../game.service.js', () => {
22+
class GameServiceMockImpl implements GameService {
23+
connect = vi
24+
.fn()
25+
.mockImplementation(async (): Promise<Observable<GameEvent>> => {
26+
return mockGameService.connect();
27+
});
28+
29+
disconnect = vi.fn().mockImplementation(async (): Promise<void> => {
30+
return mockGameService.disconnect();
31+
});
32+
33+
send = vi.fn().mockImplementation((command: string): void => {
34+
return mockGameService.send(command);
35+
});
36+
}
37+
38+
return {
39+
GameServiceImpl: GameServiceMockImpl,
40+
};
41+
});
242

343
describe('game-instance', () => {
4-
it.todo('todo', () => {
5-
expect(true).toBe(true);
44+
let credentials: SGEGameCredentials;
45+
46+
beforeEach(() => {
47+
credentials = {
48+
accessToken: 'test-access-token',
49+
host: 'test-host',
50+
port: 1234,
51+
};
52+
});
53+
54+
afterEach(() => {
55+
vi.clearAllMocks();
56+
vi.clearAllTimers();
57+
vi.useRealTimers();
58+
});
59+
60+
describe('#newInstance', () => {
61+
it('gets a new game instance', async () => {
62+
const Game = (await import('../game.instance.js')).Game;
63+
const instance = await Game.newInstance({ credentials });
64+
65+
expect(instance).toBeInstanceOf(GameServiceImpl);
66+
});
67+
68+
it('disconnects from the existing game instance', async () => {
69+
const Game = (await import('../game.instance.js')).Game;
70+
const instance1 = await Game.newInstance({ credentials });
71+
const instance2 = await Game.newInstance({ credentials });
72+
73+
expect(instance1.disconnect).toHaveBeenCalledTimes(1);
74+
expect(instance2.disconnect).toHaveBeenCalledTimes(0);
75+
});
76+
});
77+
78+
describe('#getInstance', () => {
79+
it('gets the current instance', async () => {
80+
const Game = (await import('../game.instance.js')).Game;
81+
const instance = await Game.newInstance({ credentials });
82+
83+
expect(Game.getInstance()).toBe(instance);
84+
});
85+
86+
it('returns undefined if no instance has been created', async () => {
87+
// Reset the module so that its local `instance` variable is unset.
88+
vi.resetModules();
89+
90+
const Game = (await import('../game.instance.js')).Game;
91+
92+
expect(Game.getInstance()).toBeUndefined();
93+
});
694
});
795
});

electron/main/game/__tests__/game-service.test.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ const { mockParser, mockSocket, mockWriteStream, mockWaitUntil } = vi.hoisted(
4141

4242
vi.mock('../game.parser.js', () => {
4343
class GameParserMockImpl implements GameParser {
44-
parse = (
45-
gameSocketStream: rxjs.Observable<string>
46-
): rxjs.Observable<GameEvent> => {
47-
return mockParser.parse(gameSocketStream);
48-
};
44+
parse = vi
45+
.fn()
46+
.mockImplementation(
47+
(
48+
gameSocketStream: rxjs.Observable<string>
49+
): rxjs.Observable<GameEvent> => {
50+
return mockParser.parse(gameSocketStream);
51+
}
52+
);
4953
}
5054

5155
return {
@@ -69,20 +73,22 @@ vi.mock('../game.socket.js', () => {
6973
this.onDisconnect = options.onDisconnect;
7074
}
7175

72-
connect = async (): Promise<rxjs.Observable<string>> => {
73-
this.onConnect?.();
74-
return mockSocket.connect();
75-
};
76+
connect = vi
77+
.fn()
78+
.mockImplementation(async (): Promise<rxjs.Observable<string>> => {
79+
this.onConnect?.();
80+
return mockSocket.connect();
81+
});
7682

77-
disconnect = async (): Promise<void> => {
83+
disconnect = vi.fn().mockImplementation(async (): Promise<void> => {
7884
this.onDisconnect?.('end');
7985
this.onDisconnect?.('close');
8086
return mockSocket.disconnect();
81-
};
87+
});
8288

83-
send = (command: string): void => {
89+
send = vi.fn().mockImplementation((command: string): void => {
8490
mockSocket.send(command);
85-
};
91+
});
8692
}
8793

8894
return {
@@ -100,7 +106,7 @@ vi.mock('electron', async () => {
100106

101107
vi.mock('fs-extra', () => {
102108
return {
103-
createWriteStream: () => mockWriteStream,
109+
createWriteStream: vi.fn().mockImplementation(() => mockWriteStream),
104110
};
105111
});
106112

0 commit comments

Comments
 (0)