/
index.test.ts
200 lines (175 loc) · 4.92 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2017 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import { Server, createServerRunConfig, KoaServer } from '.';
import { StorageAPI } from '../types';
const game = { seed: 0 };
jest.mock('../core/logger', () => ({
info: () => {},
error: () => {},
}));
jest.mock('koa-socket-2', () => {
class MockSocket {
on() {}
}
return class {
constructor() {
(this as any).socket = new MockSocket();
}
attach(app) {
app.io = app._io = this;
}
of() {
return this;
}
on(type, callback) {
callback((this as any).socket);
}
};
});
jest.mock('koa', () => {
return class {
constructor() {
(this as any).context = {};
(this as any).use = () => this;
(this as any).callback = () => {};
(this as any).listen = (port, listeningCallback?: () => void) => {
if (listeningCallback) listeningCallback();
return {
address: () => ({ port: 'mock-api-port' }),
close: () => {},
};
};
}
};
});
describe('new', () => {
test('custom db implementation', () => {
const game = {};
const db = {} as StorageAPI.Sync;
const server = Server({ games: [game], db });
expect(server.db).toBe(db);
});
test('custom transport implementation', () => {
const game = {};
const transport = { init: jest.fn() };
Server({ games: [game], transport });
expect(transport.init).toBeCalled();
});
test('custom auth implementation', () => {
const game = {};
const authenticateCredentials = () => true;
const server = Server({ games: [game], authenticateCredentials });
expect(server.db).not.toBeNull();
});
});
describe('run', () => {
let server: ReturnType<typeof Server> | null;
let runningServer: { appServer: KoaServer; apiServer?: KoaServer } | null;
beforeEach(() => {
server = null;
runningServer = null;
});
afterEach(() => {
if (server && runningServer) {
const { apiServer, appServer } = runningServer;
server.kill({ apiServer, appServer });
}
});
test('single server running', async () => {
server = Server({ games: [game] });
runningServer = await server.run(undefined);
expect(server).not.toBeUndefined();
expect(runningServer.appServer).not.toBeUndefined();
expect(runningServer.apiServer).toBeUndefined();
});
test('multiple servers running', async () => {
server = Server({ games: [game] });
runningServer = await server.run({
port: 57890,
lobbyConfig: { apiPort: 57891 },
});
expect(server).not.toBeUndefined();
expect(runningServer.appServer).not.toBeUndefined();
expect(runningServer.apiServer).not.toBeUndefined();
});
});
describe('kill', () => {
test('call close on both servers', async () => {
const apiServer = ({
close: jest.fn(),
} as unknown) as KoaServer;
const appServer = ({
close: jest.fn(),
} as unknown) as KoaServer;
const server = Server({ games: [game] });
server.kill({ appServer, apiServer });
expect(apiServer.close).toBeCalled();
expect(appServer.close).toBeCalled();
});
test('do not fail if api server is not defined', async () => {
const appServer = ({
close: jest.fn(),
} as unknown) as KoaServer;
const server = Server({ games: [game] });
expect(() => server.kill({ appServer })).not.toThrowError();
expect(appServer.close).toBeCalled();
});
});
describe('createServerRunConfig', () => {
// TODO use data-driven-test here after upgrading to Jest 23+.
test('should return valid config with different server run arguments', () => {
const mockCallback = () => {};
const mockApiCallback = () => {};
expect(createServerRunConfig(8000)).toEqual({
port: 8000,
callback: undefined,
});
expect(createServerRunConfig(8000, mockCallback)).toEqual({
port: 8000,
callback: mockCallback,
});
expect(createServerRunConfig({})).toEqual({
port: undefined,
callback: undefined,
});
expect(createServerRunConfig({ port: 1234 })).toEqual({
port: 1234,
callback: undefined,
});
expect(
createServerRunConfig({ port: 1234, callback: mockCallback })
).toEqual({
port: 1234,
callback: mockCallback,
});
expect(
createServerRunConfig({ port: 1234, lobbyConfig: { apiPort: 5467 } })
).toEqual({
port: 1234,
callback: undefined,
lobbyConfig: { apiPort: 5467 },
});
expect(
createServerRunConfig({
port: 1234,
callback: mockCallback,
lobbyConfig: {
apiPort: 5467,
apiCallback: mockApiCallback,
},
})
).toEqual({
port: 1234,
callback: mockCallback,
lobbyConfig: {
apiPort: 5467,
apiCallback: mockApiCallback,
},
});
});
});