diff --git a/CHANGELOG.md b/CHANGELOG.md index ca7f4662f..65822cd14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,9 @@ hook. Plugins should use this instead of the `clientVars` global variable. * New `userJoin` server-side hook. * The `userLeave` server-side hook has a new `socket` context property. + * The `helper.aNewPad()` function (accessible to client-side tests) now + accepts hook functions to inject when opening a pad. This can be used to + test any new client-side hooks your plugin provides. # 1.8.14 diff --git a/src/templates/pad.html b/src/templates/pad.html index bcede8fd3..7ff447dc9 100644 --- a/src/templates/pad.html +++ b/src/templates/pad.html @@ -478,6 +478,10 @@ plugins.baseURL = baseURL; plugins.update(function () { + // Mechanism for tests to register hook functions (install fake plugins). + window._postPluginUpdateForTestingDone = false; + if (window._postPluginUpdateForTesting != null) window._postPluginUpdateForTesting(); + window._postPluginUpdateForTestingDone = true; // Call documentReady hook $(function() { hooks.aCallAll('documentReady'); diff --git a/src/tests/frontend/helper.js b/src/tests/frontend/helper.js index 7da7fff04..8f8f61627 100644 --- a/src/tests/frontend/helper.js +++ b/src/tests/frontend/helper.js @@ -96,6 +96,7 @@ const helper = {}; _retry: 0, clearCookies: true, id: `FRONTEND_TEST_${helper.randomString(20)}`, + hookFns: {}, }, opts); // if opts.params is set we manipulate the URL to include URL parameters IE ?foo=Bah. @@ -122,7 +123,26 @@ const helper = {}; $('#iframe-container iframe').remove(); // set new iframe $('#iframe-container').append($iframe); - await new Promise((resolve) => $iframe.one('load', resolve)); + await Promise.all([ + new Promise((resolve) => $iframe.one('load', resolve)), + // Install the hook functions as early as possible because some of them fire right away. + new Promise((resolve, reject) => { + if ($iframe[0].contentWindow._postPluginUpdateForTestingDone) { + return reject(new Error( + 'failed to set _postPluginUpdateForTesting before it would have been called')); + } + $iframe[0].contentWindow._postPluginUpdateForTesting = () => { + const {hooks} = + $iframe[0].contentWindow.require('ep_etherpad-lite/static/js/pluginfw/plugin_defs'); + for (const [hookName, hookFns] of Object.entries(opts.hookFns)) { + if (hooks[hookName] == null) hooks[hookName] = []; + hooks[hookName].push( + ...hookFns.map((hookFn) => ({hook_name: hookName, hook_fn: hookFn}))); + } + resolve(); + }; + }), + ]); helper.padChrome$ = await helper.getFrameJQuery($('#iframe-container iframe'), true); helper.padChrome$.padeditor = helper.padChrome$.window.require('ep_etherpad-lite/static/js/pad_editor').padeditor;