From c8e0916e1ac997ff0268f98368e7fc87cf60b947 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 Oct 2021 17:44:10 -0400 Subject: [PATCH] tests: Spy on socket.io messages as early as possible --- src/static/js/pad.js | 23 +++++++++++++++-------- src/tests/frontend/helper.js | 31 ++++++++++--------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 66e46391e..1d33a3c99 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -311,14 +311,21 @@ const handshake = async () => { } }); - await new Promise((resolve) => { - const h = (obj) => { - if (obj.accessStatus || obj.type !== 'CLIENT_VARS') return; - socket.off('message', h); - resolve(); - }; - socket.on('message', h); - }); + await Promise.all([ + new Promise((resolve) => { + const h = (obj) => { + if (obj.accessStatus || obj.type !== 'CLIENT_VARS') return; + socket.off('message', h); + resolve(); + }; + socket.on('message', h); + }), + // This hook is only intended to be used by test code. If a plugin would like to use this hook, + // the hook must first be promoted to officially supported by deleting the leading underscore + // from the name, adding documentation to `doc/api/hooks_client-side.md`, and deleting this + // comment. + hooks.aCallAll('_socketCreated', {socket}), + ]); }; /** Defers message handling until setCollabClient() is called with a non-null value. */ diff --git a/src/tests/frontend/helper.js b/src/tests/frontend/helper.js index 8f8f61627..dbb5d4b2d 100644 --- a/src/tests/frontend/helper.js +++ b/src/tests/frontend/helper.js @@ -99,6 +99,16 @@ const helper = {}; hookFns: {}, }, opts); + // Set up socket.io spying as early as possible. + /** chat messages received */ + helper.chatMessages = []; + /** changeset commits from the server */ + helper.commits = []; + /** userInfo messages from the server */ + helper.userInfos = []; + if (opts.hookFns._socketCreated == null) opts.hookFns._socketCreated = []; + opts.hookFns._socketCreated.unshift(() => helper.spyOnSocketIO()); + // if opts.params is set we manipulate the URL to include URL parameters IE ?foo=Bah. let encodedParams; if (opts.params) { @@ -171,27 +181,6 @@ const helper = {}; helper.padOuter$.fx.off = true; helper.padInner$.fx.off = true; - /* - * chat messages received - * @type {Array} - */ - helper.chatMessages = []; - - /* - * changeset commits from the server - * @type {Array} - */ - helper.commits = []; - - /* - * userInfo messages from the server - * @type {Array} - */ - helper.userInfos = []; - - // listen for server messages - helper.spyOnSocketIO(); - return opts.id; };