Skip to content

Commit f135642

Browse files
committed
Refactor mock server
- Add helper methods for subscriptions/hooks - Report requests with a missing handler - Don't require wait-for-expect package when importing the mock server
1 parent 5a887a4 commit f135642

File tree

6 files changed

+235
-135
lines changed

6 files changed

+235
-135
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "Run Tests With Debugger",
99
"type": "node",
1010
"request": "launch",
11-
"port": 5858,
11+
// "port": 5858,
1212
//"stopOnEntry": false,
1313
"runtimeArgs": [
1414
"--inspect-brk=5858",

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/Socket.test.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {
22
DEFAULT_AUTH_RESPONSE, DEFAULT_CONNECT_PARAMS,
3-
getConnectedSocket, getMockServer, getSocket, waitForExpect
4-
} from './helpers.js';
3+
getConnectedSocket, getMockServer, getSocket
4+
} from './mock-server.js';
55

66
import ApiConstants from '../ApiConstants.js';
77

88
import { HookCallback, HookSubscriberInfo, SubscriptionRemoveHandler } from '../types/subscriptions.js';
99
import { IncomingSubscriptionEvent } from '../types/api_internal.js';
1010

1111
import { jest } from '@jest/globals';
12+
import { waitForExpect } from './test-utils.js';
1213

1314
let server: ReturnType<typeof getMockServer>;
1415

@@ -29,7 +30,7 @@ describe('socket', () => {
2930

3031
describe('auth', () => {
3132
test('should handle valid credentials', async () => {
32-
server.addDataHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
33+
server.addRequestHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
3334
const connectedCallback = jest.fn();
3435

3536
const { socket, mockConsole } = getSocket();
@@ -48,7 +49,7 @@ describe('socket', () => {
4849
});
4950

5051
test('should handle valid refresh token', async () => {
51-
server.addDataHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
52+
server.addRequestHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
5253
const connectedCallback = jest.fn();
5354

5455
const { socket, mockConsole } = getSocket();
@@ -106,7 +107,7 @@ describe('socket', () => {
106107

107108
// Valid connect attempt
108109
server = getMockServer();
109-
server.addDataHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
110+
server.addRequestHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE);
110111

111112
await socket.connect(DEFAULT_CONNECT_PARAMS.username, DEFAULT_CONNECT_PARAMS.password, false);
112113

@@ -125,17 +126,19 @@ describe('socket', () => {
125126
socket.onDisconnected = disconnectedCallback;
126127

127128
// Dummy listener
128-
server.addDataHandler('POST', 'hubs/listeners/hub_updated', undefined);
129+
server.addRequestHandler('POST', 'hubs/listeners/hub_updated', undefined);
129130
await socket.addListener('hubs', 'hub_updated', dummyfn);
130131

131132
// Dummy pending request
133+
server.ignoreMissingHandler('DELETE', 'dummyLogoutDelete');
134+
132135
socket.delete('dummyLogoutDelete').catch((error: Error) => {
133136
// TODO: fix, too unreliable at the moment (depends on the timings)
134137
//expect(error.message).toEqual('Socket disconnected');
135138
});
136139

137140
// Logout
138-
server.addDataHandler('DELETE', ApiConstants.LOGOUT_URL);
141+
server.addRequestHandler('DELETE', ApiConstants.LOGOUT_URL);
139142
await socket.logout();
140143

141144
expect(sessionResetCallback.mock.calls.length).toBe(1);
@@ -206,7 +209,7 @@ describe('socket', () => {
206209
expect(mockConsole.error.mock.calls.length).toBe(1);
207210

208211
server = getMockServer();
209-
server.addDataHandler('POST', ApiConstants.CONNECT_URL, undefined);
212+
server.addRequestHandler('POST', ApiConstants.CONNECT_URL, undefined);
210213
jest.runOnlyPendingTimers();
211214
jest.runOnlyPendingTimers();
212215
jest.runOnlyPendingTimers();
@@ -257,7 +260,7 @@ describe('socket', () => {
257260
socket.disconnect();
258261
await waitForExpect(() => expect(socket.isActive()).toEqual(false));
259262

260-
server.addDataHandler('POST', ApiConstants.CONNECT_URL, undefined);
263+
server.addRequestHandler('POST', ApiConstants.CONNECT_URL, undefined);
261264
await socket.reconnect();
262265
expect(socket.isConnected()).toEqual(true);
263266

@@ -285,7 +288,7 @@ describe('socket', () => {
285288
server.addErrorHandler('POST', ApiConstants.CONNECT_URL, ErrorResponse, 400);
286289

287290
const authCallback = jest.fn();
288-
server.addDataHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE, authCallback);
291+
server.addRequestHandler('POST', ApiConstants.LOGIN_URL, DEFAULT_AUTH_RESPONSE, authCallback);
289292

290293
jest.runOnlyPendingTimers();
291294
socket.reconnect();
@@ -328,6 +331,9 @@ describe('socket', () => {
328331
test('should report request timeouts', async () => {
329332
const { socket, mockConsole } = await getConnectedSocket(server);
330333

334+
server.ignoreMissingHandler('POST', 'hubs/listeners/hub_updated');
335+
server.ignoreMissingHandler('POST', 'hubs/listeners/hub_added');
336+
331337
jest.useFakeTimers();
332338
socket.addListener('hubs', 'hub_updated', dummyfn)
333339
.catch(() => {});
@@ -388,8 +394,8 @@ describe('socket', () => {
388394

389395
test('should handle listener messages', async () => {
390396
const { socket, mockConsole } = await getConnectedSocket(server);
391-
server.addDataHandler('POST', 'hubs/listeners/hub_updated', undefined);
392-
server.addDataHandler('POST', `hubs/${entityId}/listeners/hub_updated`, undefined);
397+
server.addSubscriptionHandler('hubs', 'hub_updated');
398+
server.addSubscriptionHandler('hubs', 'hub_updated', entityId);
393399

394400
const commonSubscriptionCallback = jest.fn();
395401
const entitySubscriptionCallback = jest.fn();
@@ -415,8 +421,7 @@ describe('socket', () => {
415421
test('should handle listener removal', async () => {
416422
const { socket, mockConsole } = await getConnectedSocket(server);
417423

418-
const subscribeCallback = jest.fn();
419-
server.addDataHandler('POST', 'hubs/listeners/hub_updated', undefined, subscribeCallback);
424+
const hubUpdatedListener = server.addSubscriptionHandler('hubs', 'hub_updated');
420425

421426
// Add two simultaneous pending add events
422427
const p1 = socket.addListener('hubs', 'hub_updated', dummyfn);
@@ -428,17 +433,14 @@ describe('socket', () => {
428433
const removeListener1 = await p1;
429434
const removeListener2 = await p2;
430435

431-
expect(subscribeCallback.mock.calls.length).toBe(1);
436+
expect(hubUpdatedListener.subscribeFn.mock.calls.length).toBe(1);
432437
expect(socket.getPendingSubscriptionCount()).toBe(0);
433438

434-
const deleteCallback = jest.fn();
435-
server.addDataHandler('DELETE', 'hubs/listeners/hub_updated', undefined, deleteCallback);
436-
437439
removeListener1();
438-
expect(deleteCallback.mock.calls.length).toBe(0); // Shouldn't call API yet, still one left
440+
expect(hubUpdatedListener.unsubscribeFn.mock.calls.length).toBe(0); // Shouldn't call API yet, still one left
439441

440442
removeListener2();
441-
await waitForExpect(() => expect(deleteCallback.mock.calls.length).toBe(1));
443+
await waitForExpect(() => expect(hubUpdatedListener.unsubscribeFn.mock.calls.length).toBe(1));
442444

443445
expect(socket.hasListeners()).toBe(false);
444446

@@ -466,11 +468,9 @@ describe('socket', () => {
466468
});
467469

468470
describe('hooks', () => {
469-
const hookEventData: IncomingSubscriptionEvent = {
470-
event: 'queue_bundle_finished_hook',
471-
data: {},
472-
completion_id: 1,
473-
};
471+
const HOOK_MODULE = 'queue';
472+
const HOOK_NAME = 'queue_bundle_finished_hook';
473+
const HOOK_COMPLETION_ID = 1;
474474

475475
const hookSubscriberInfo: HookSubscriberInfo = {
476476
id: 'sfv_checker',
@@ -487,26 +487,22 @@ describe('socket', () => {
487487

488488
// Add hook
489489
{
490-
const hookAddCallback = jest.fn();
491-
server.addDataHandler('POST', 'queue/hooks/queue_bundle_finished_hook', undefined, hookAddCallback);
490+
const hook = server.addHookHandler(HOOK_MODULE, HOOK_NAME);
492491

493492
removeListener = await socket.addHook(
494-
'queue',
495-
'queue_bundle_finished_hook',
493+
HOOK_MODULE,
494+
HOOK_NAME,
496495
rejectCallback,
497496
hookSubscriberInfo
498497
);
499498

500-
expect((hookAddCallback.mock.calls[0][0] as any).data).toEqual(hookSubscriberInfo);
501-
expect(hookAddCallback.mock.calls.length).toBe(1);
502-
}
499+
expect((hook.subscribeFn.mock.calls[0][0] as any).data).toEqual(hookSubscriberInfo);
500+
expect(hook.subscribeFn.mock.calls.length).toBe(1);
503501

504-
// Simulate action
505-
{
506-
const hookEventCallback = jest.fn();
507-
server.addDataHandler('POST', 'queue/hooks/queue_bundle_finished_hook/1/reject', undefined, hookEventCallback);
508-
server.send(hookEventData);
509-
await waitForExpect(() => expect(hookEventCallback.mock.calls.length).toBe(1));
502+
// Simulate action
503+
const hookResolver = hook.addResolver(HOOK_COMPLETION_ID);
504+
hookResolver.fire({});
505+
await waitForExpect(() => expect(hookResolver.rejectFn.mock.calls.length).toBe(1));
510506
}
511507

512508
// Clean up

0 commit comments

Comments
 (0)