|
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 | +}); |
2 | 42 |
|
3 | 43 | 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 | + }); |
6 | 94 | });
|
7 | 95 | });
|
0 commit comments