lint: src/tests/frontend/helper.js and friends

This commit is contained in:
Richard Hansen 2021-02-13 00:41:01 -05:00
parent 8f2f6593be
commit eb9d5bb470
3 changed files with 60 additions and 86 deletions

View file

@ -2,11 +2,11 @@
const helper = {}; // eslint-disable-line no-redeclare const helper = {}; // eslint-disable-line no-redeclare
(function () { (() => {
let $iframe; const let $iframe;
jsLibraries = {}; const jsLibraries = {};
helper.init = function (cb) { helper.init = (cb) => {
$.get('/static/js/jquery.js').done((code) => { $.get('/static/js/jquery.js').done((code) => {
// make sure we don't override existing jquery // make sure we don't override existing jquery
jsLibraries.jquery = `if(typeof $ === 'undefined') {\n${code}\n}`; jsLibraries.jquery = `if(typeof $ === 'undefined') {\n${code}\n}`;
@ -19,7 +19,7 @@ const helper = {}; // eslint-disable-line no-redeclare
}); });
}; };
helper.randomString = function randomString(len) { helper.randomString = (len) => {
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let randomstring = ''; let randomstring = '';
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
@ -29,7 +29,7 @@ const helper = {}; // eslint-disable-line no-redeclare
return randomstring; return randomstring;
}; };
const getFrameJQuery = function ($iframe) { const getFrameJQuery = ($iframe) => {
/* /*
I tried over 9001 ways to inject javascript into iframes. I tried over 9001 ways to inject javascript into iframes.
This is the only way I found that worked in IE 7+8+9, FF and Chrome This is the only way I found that worked in IE 7+8+9, FF and Chrome
@ -38,7 +38,7 @@ const helper = {}; // eslint-disable-line no-redeclare
const doc = win.document; const doc = win.document;
// IE 8+9 Hack to make eval appear // IE 8+9 Hack to make eval appear
// http://stackoverflow.com/questions/2720444/why-does-this-window-object-not-have-the-eval-function // https://stackoverflow.com/q/2720444
win.execScript && win.execScript('null'); win.execScript && win.execScript('null');
win.eval(jsLibraries.jquery); win.eval(jsLibraries.jquery);
@ -50,15 +50,15 @@ const helper = {}; // eslint-disable-line no-redeclare
return win.$; return win.$;
}; };
helper.clearSessionCookies = function () { helper.clearSessionCookies = () => {
// Expire cookies, so author and language are changed after reloading the pad. // Expire cookies, so author and language are changed after reloading the pad. See:
// See https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#Example_4_Reset_the_previous_cookie // https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#example_4_reset_the_previous_cookie
window.document.cookie = 'token=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; window.document.cookie = 'token=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
window.document.cookie = 'language=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; window.document.cookie = 'language=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
}; };
// Can only happen when the iframe exists, so we're doing it separately from other cookies // Can only happen when the iframe exists, so we're doing it separately from other cookies
helper.clearPadPrefCookie = function () { helper.clearPadPrefCookie = () => {
helper.padChrome$.document.cookie = 'prefsHttp=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; helper.padChrome$.document.cookie = 'prefsHttp=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
}; };
@ -68,7 +68,7 @@ const helper = {}; // eslint-disable-line no-redeclare
// seem to have independent cookies, UNLESS we put path=/ here (which we don't). // seem to have independent cookies, UNLESS we put path=/ here (which we don't).
// I don't fully understand it, but this function seems to properly simulate // I don't fully understand it, but this function seems to properly simulate
// padCookie.setPref in the client code // padCookie.setPref in the client code
helper.setPadPrefCookie = function (prefs) { helper.setPadPrefCookie = (prefs) => {
helper.padChrome$.document.cookie = helper.padChrome$.document.cookie =
(`prefsHttp=${escape(JSON.stringify(prefs))};expires=Thu, 01 Jan 3000 00:00:00 GMT`); (`prefsHttp=${escape(JSON.stringify(prefs))};expires=Thu, 01 Jan 3000 00:00:00 GMT`);
}; };
@ -94,7 +94,7 @@ const helper = {}; // eslint-disable-line no-redeclare
// This ensures that tests run regardless of this problem // This ensures that tests run regardless of this problem
helper.retry = 0; helper.retry = 0;
helper.newPad = function (cb, padName) { helper.newPad = (cb, padName) => {
// build opts object // build opts object
let opts = {clearCookies: true}; let opts = {clearCookies: true};
if (typeof cb === 'function') { if (typeof cb === 'function') {
@ -181,7 +181,7 @@ const helper = {}; // eslint-disable-line no-redeclare
return padName; return padName;
}; };
helper.newAdmin = async function (page) { helper.newAdmin = async (page) => {
// define the iframe // define the iframe
$iframe = $(`<iframe src='/admin/${page}'></iframe>`); $iframe = $(`<iframe src='/admin/${page}'></iframe>`);
@ -197,7 +197,7 @@ const helper = {}; // eslint-disable-line no-redeclare
}); });
}; };
helper.waitFor = function (conditionFunc, timeoutTime = 1900, intervalTime = 10) { helper.waitFor = (conditionFunc, timeoutTime = 1900, intervalTime = 10) => {
const deferred = new $.Deferred(); const deferred = new $.Deferred();
const _fail = deferred.fail.bind(deferred); const _fail = deferred.fail.bind(deferred);
@ -242,14 +242,12 @@ const helper = {}; // eslint-disable-line no-redeclare
* @returns {Promise} * @returns {Promise}
* *
*/ */
helper.waitForPromise = async function (...args) {
// Note: waitFor() has a strange API: On timeout it rejects, but it also throws an uncatchable // Note: waitFor() has a strange API: On timeout it rejects, but it also throws an uncatchable
// exception unless .fail() has been called. That uncatchable exception is disabled here by // exception unless .fail() has been called. That uncatchable exception is disabled here by
// passing a no-op function to .fail(). // passing a no-op function to .fail().
return await this.waitFor(...args).fail(() => {}); helper.waitForPromise = async (...args) => await helper.waitFor(...args).fail(() => {});
};
helper.selectLines = function ($startLine, $endLine, startOffset, endOffset) { helper.selectLines = ($startLine, $endLine, startOffset, endOffset) => {
// if no offset is provided, use beginning of start line and end of end line // if no offset is provided, use beginning of start line and end of end line
startOffset = startOffset || 0; startOffset = startOffset || 0;
endOffset = endOffset === undefined ? $endLine.text().length : endOffset; endOffset = endOffset === undefined ? $endLine.text().length : endOffset;
@ -268,7 +266,7 @@ const helper = {}; // eslint-disable-line no-redeclare
selection.addRange(range); selection.addRange(range);
}; };
const getTextNodeAndOffsetOf = function ($targetLine, targetOffsetAtLine) { const getTextNodeAndOffsetOf = ($targetLine, targetOffsetAtLine) => {
const $textNodes = $targetLine.find('*').contents().filter(function () { const $textNodes = $targetLine.find('*').contents().filter(function () {
return this.nodeType === Node.TEXT_NODE; return this.nodeType === Node.TEXT_NODE;
}); });
@ -310,5 +308,5 @@ const helper = {}; // eslint-disable-line no-redeclare
/* Ensure console.log doesn't blow up in IE, ugly but ok for a test framework imho*/ /* Ensure console.log doesn't blow up in IE, ugly but ok for a test framework imho*/
window.console = window.console || {}; window.console = window.console || {};
window.console.log = window.console.log || function () {}; window.console.log = window.console.log || (() => {});
})(); })();

View file

@ -4,7 +4,7 @@
* Spys on socket.io messages and saves them into several arrays * Spys on socket.io messages and saves them into several arrays
* that are visible in tests * that are visible in tests
*/ */
helper.spyOnSocketIO = function () { helper.spyOnSocketIO = () => {
helper.contentWindow().pad.socket.on('message', (msg) => { helper.contentWindow().pad.socket.on('message', (msg) => {
if (msg.type === 'COLLABROOM') { if (msg.type === 'COLLABROOM') {
if (msg.data.type === 'ACCEPT_COMMIT') { if (msg.data.type === 'ACCEPT_COMMIT') {
@ -30,7 +30,7 @@ helper.spyOnSocketIO = function () {
* @todo needs to support writing to a specified caret position * @todo needs to support writing to a specified caret position
* *
*/ */
helper.edit = async function (message, line) { helper.edit = async (message, line) => {
const editsNum = helper.commits.length; const editsNum = helper.commits.length;
line = line ? line - 1 : 0; line = line ? line - 1 : 0;
helper.linesDiv()[line].sendkeys(message); helper.linesDiv()[line].sendkeys(message);
@ -45,7 +45,7 @@ helper.edit = async function (message, line) {
* *
* @returns {Array.<HTMLElement>} array of divs * @returns {Array.<HTMLElement>} array of divs
*/ */
helper.linesDiv = function () { helper.linesDiv = () => {
return helper.padInner$('.ace-line').map(function () { return helper.padInner$('.ace-line').map(function () {
return $(this); return $(this);
}).get(); }).get();
@ -57,18 +57,15 @@ helper.linesDiv = function () {
* *
* @returns {Array.<string>} lines of text * @returns {Array.<string>} lines of text
*/ */
helper.textLines = function () { helper.textLines = () => helper.linesDiv().map((div) => div.text());
return helper.linesDiv().map((div) => div.text());
};
/** /**
* The default pad text transmitted via `clientVars` * The default pad text transmitted via `clientVars`
* *
* @returns {string} * @returns {string}
*/ */
helper.defaultText = function () { helper.defaultText =
return helper.padChrome$.window.clientVars.collab_client_vars.initialAttributedText.text; () => helper.padChrome$.window.clientVars.collab_client_vars.initialAttributedText.text;
};
/** /**
* Sends a chat `message` via `sendKeys` * Sends a chat `message` via `sendKeys`
@ -84,7 +81,7 @@ helper.defaultText = function () {
* @param {string} message the chat message to be sent * @param {string} message the chat message to be sent
* @returns {Promise} * @returns {Promise}
*/ */
helper.sendChatMessage = function (message) { helper.sendChatMessage = (message) => {
const noOfChatMessages = helper.chatMessages.length; const noOfChatMessages = helper.chatMessages.length;
helper.padChrome$('#chatinput').sendkeys(message); helper.padChrome$('#chatinput').sendkeys(message);
return helper.waitForPromise(() => noOfChatMessages + 1 === helper.chatMessages.length); return helper.waitForPromise(() => noOfChatMessages + 1 === helper.chatMessages.length);
@ -95,7 +92,7 @@ helper.sendChatMessage = function (message) {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.showSettings = function () { helper.showSettings = () => {
if (!helper.isSettingsShown()) { if (!helper.isSettingsShown()) {
helper.settingsButton().click(); helper.settingsButton().click();
return helper.waitForPromise(() => helper.isSettingsShown(), 2000); return helper.waitForPromise(() => helper.isSettingsShown(), 2000);
@ -108,7 +105,7 @@ helper.showSettings = function () {
* @returns {Promise} * @returns {Promise}
* @todo untested * @todo untested
*/ */
helper.hideSettings = function () { helper.hideSettings = () => {
if (helper.isSettingsShown()) { if (helper.isSettingsShown()) {
helper.settingsButton().click(); helper.settingsButton().click();
return helper.waitForPromise(() => !helper.isSettingsShown(), 2000); return helper.waitForPromise(() => !helper.isSettingsShown(), 2000);
@ -121,7 +118,7 @@ helper.hideSettings = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.enableStickyChatviaSettings = function () { helper.enableStickyChatviaSettings = () => {
const stickyChat = helper.padChrome$('#options-stickychat'); const stickyChat = helper.padChrome$('#options-stickychat');
if (helper.isSettingsShown() && !stickyChat.is(':checked')) { if (helper.isSettingsShown() && !stickyChat.is(':checked')) {
stickyChat.click(); stickyChat.click();
@ -135,7 +132,7 @@ helper.enableStickyChatviaSettings = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.disableStickyChatviaSettings = function () { helper.disableStickyChatviaSettings = () => {
const stickyChat = helper.padChrome$('#options-stickychat'); const stickyChat = helper.padChrome$('#options-stickychat');
if (helper.isSettingsShown() && stickyChat.is(':checked')) { if (helper.isSettingsShown() && stickyChat.is(':checked')) {
stickyChat.click(); stickyChat.click();
@ -149,7 +146,7 @@ helper.disableStickyChatviaSettings = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.enableStickyChatviaIcon = function () { helper.enableStickyChatviaIcon = () => {
const stickyChat = helper.padChrome$('#titlesticky'); const stickyChat = helper.padChrome$('#titlesticky');
if (helper.isChatboxShown() && !helper.isChatboxSticky()) { if (helper.isChatboxShown() && !helper.isChatboxSticky()) {
stickyChat.click(); stickyChat.click();
@ -163,7 +160,7 @@ helper.enableStickyChatviaIcon = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.disableStickyChatviaIcon = function () { helper.disableStickyChatviaIcon = () => {
if (helper.isChatboxShown() && helper.isChatboxSticky()) { if (helper.isChatboxShown() && helper.isChatboxSticky()) {
helper.titlecross().click(); helper.titlecross().click();
return helper.waitForPromise(() => !helper.isChatboxSticky(), 2000); return helper.waitForPromise(() => !helper.isChatboxSticky(), 2000);
@ -182,7 +179,7 @@ helper.disableStickyChatviaIcon = function () {
* @todo for some reason this does only work the first time, you cannot * @todo for some reason this does only work the first time, you cannot
* goto rev 0 and then via the same method to rev 5. Use buttons instead * goto rev 0 and then via the same method to rev 5. Use buttons instead
*/ */
helper.gotoTimeslider = function (revision) { helper.gotoTimeslider = (revision) => {
revision = Number.isInteger(revision) ? `#${revision}` : ''; revision = Number.isInteger(revision) ? `#${revision}` : '';
const iframe = $('#iframe-container iframe'); const iframe = $('#iframe-container iframe');
iframe.attr('src', `${iframe.attr('src')}/timeslider${revision}`); iframe.attr('src', `${iframe.attr('src')}/timeslider${revision}`);
@ -198,7 +195,7 @@ helper.gotoTimeslider = function (revision) {
* @todo no mousemove test * @todo no mousemove test
* @param {number} X coordinate * @param {number} X coordinate
*/ */
helper.sliderClick = function (X) { helper.sliderClick = (X) => {
const sliderBar = helper.sliderBar(); const sliderBar = helper.sliderBar();
const edown = new jQuery.Event('mousedown'); const edown = new jQuery.Event('mousedown');
const eup = new jQuery.Event('mouseup'); const eup = new jQuery.Event('mouseup');
@ -214,11 +211,9 @@ helper.sliderClick = function (X) {
* *
* @returns {Array.<string>} lines of text * @returns {Array.<string>} lines of text
*/ */
helper.timesliderTextLines = function () { helper.timesliderTextLines = () => helper.contentWindow().$('.ace-line').map(function () {
return helper.contentWindow().$('.ace-line').map(function () {
return $(this).text(); return $(this).text();
}).get(); }).get();
};
helper.padIsEmpty = () => ( helper.padIsEmpty = () => (
!helper.padInner$.document.getSelection().isCollapsed || !helper.padInner$.document.getSelection().isCollapsed ||

View file

@ -5,9 +5,7 @@
* *
* @returns {HTMLElement} contentWindow * @returns {HTMLElement} contentWindow
*/ */
helper.contentWindow = function () { helper.contentWindow = () => $('#iframe-container iframe')[0].contentWindow;
return $('#iframe-container iframe')[0].contentWindow;
};
/** /**
* Opens the chat unless it is already open via an * Opens the chat unless it is already open via an
@ -15,7 +13,7 @@ helper.contentWindow = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.showChat = function () { helper.showChat = () => {
const chaticon = helper.chatIcon(); const chaticon = helper.chatIcon();
if (chaticon.hasClass('visible')) { if (chaticon.hasClass('visible')) {
chaticon.click(); chaticon.click();
@ -28,7 +26,7 @@ helper.showChat = function () {
* *
* @returns {Promise} * @returns {Promise}
*/ */
helper.hideChat = function () { helper.hideChat = () => {
if (helper.isChatboxShown() && !helper.isChatboxSticky()) { if (helper.isChatboxShown() && !helper.isChatboxSticky()) {
helper.titlecross().click(); helper.titlecross().click();
return helper.waitForPromise(() => !helper.isChatboxShown(), 2000); return helper.waitForPromise(() => !helper.isChatboxShown(), 2000);
@ -40,53 +38,48 @@ helper.hideChat = function () {
* *
* @returns {HTMLElement} the chat icon * @returns {HTMLElement} the chat icon
*/ */
helper.chatIcon = function () { return helper.padChrome$('#chaticon'); }; helper.chatIcon = () => helper.padChrome$('#chaticon');
/** /**
* The chat messages from the UI * The chat messages from the UI
* *
* @returns {Array.<HTMLElement>} * @returns {Array.<HTMLElement>}
*/ */
helper.chatTextParagraphs = function () { return helper.padChrome$('#chattext').children('p'); }; helper.chatTextParagraphs = () => helper.padChrome$('#chattext').children('p');
/** /**
* Returns true if the chat box is sticky * Returns true if the chat box is sticky
* *
* @returns {boolean} stickyness of the chat box * @returns {boolean} stickyness of the chat box
*/ */
helper.isChatboxSticky = function () { helper.isChatboxSticky = () => helper.padChrome$('#chatbox').hasClass('stickyChat');
return helper.padChrome$('#chatbox').hasClass('stickyChat');
};
/** /**
* Returns true if the chat box is shown * Returns true if the chat box is shown
* *
* @returns {boolean} visibility of the chat box * @returns {boolean} visibility of the chat box
*/ */
helper.isChatboxShown = function () { helper.isChatboxShown = () => helper.padChrome$('#chatbox').hasClass('visible');
return helper.padChrome$('#chatbox').hasClass('visible');
};
/** /**
* Gets the settings menu * Gets the settings menu
* *
* @returns {HTMLElement} the settings menu * @returns {HTMLElement} the settings menu
*/ */
helper.settingsMenu = function () { return helper.padChrome$('#settings'); }; helper.settingsMenu = () => helper.padChrome$('#settings');
/** /**
* Gets the settings button * Gets the settings button
* *
* @returns {HTMLElement} the settings button * @returns {HTMLElement} the settings button
*/ */
helper.settingsButton = function () { helper.settingsButton =
return helper.padChrome$("button[data-l10n-id='pad.toolbar.settings.title']"); () => helper.padChrome$("button[data-l10n-id='pad.toolbar.settings.title']");
};
/** /**
* Toggles user list * Toggles user list
*/ */
helper.toggleUserList = async function () { helper.toggleUserList = async () => {
const isVisible = helper.userListShown(); const isVisible = helper.userListShown();
const button = helper.padChrome$("button[data-l10n-id='pad.toolbar.showusers.title']"); const button = helper.padChrome$("button[data-l10n-id='pad.toolbar.showusers.title']");
button.click(); button.click();
@ -98,18 +91,14 @@ helper.toggleUserList = async function () {
* *
* @returns {HTMLElement} user name input field * @returns {HTMLElement} user name input field
*/ */
helper.usernameField = function () { helper.usernameField = () => helper.padChrome$("input[data-l10n-id='pad.userlist.entername']");
return helper.padChrome$("input[data-l10n-id='pad.userlist.entername']");
};
/** /**
* Is the user list popup shown? * Is the user list popup shown?
* *
* @returns {boolean} * @returns {boolean}
*/ */
helper.userListShown = function () { helper.userListShown = () => helper.padChrome$('div#users').hasClass('popup-show');
return helper.padChrome$('div#users').hasClass('popup-show');
};
/** /**
* Sets the user name * Sets the user name
@ -128,23 +117,21 @@ helper.setUserName = async (name) => {
* *
* @returns {HTMLElement} the titlecross icon * @returns {HTMLElement} the titlecross icon
*/ */
helper.titlecross = function () { return helper.padChrome$('#titlecross'); }; helper.titlecross = () => helper.padChrome$('#titlecross');
/** /**
* Returns true if the settings menu is visible * Returns true if the settings menu is visible
* *
* @returns {boolean} is the settings menu shown? * @returns {boolean} is the settings menu shown?
*/ */
helper.isSettingsShown = function () { helper.isSettingsShown = () => helper.padChrome$('#settings').hasClass('popup-show');
return helper.padChrome$('#settings').hasClass('popup-show');
};
/** /**
* Gets the timer div of a timeslider that has the datetime of the revision * Gets the timer div of a timeslider that has the datetime of the revision
* *
* @returns {HTMLElement} timer * @returns {HTMLElement} timer
*/ */
helper.timesliderTimer = function () { helper.timesliderTimer = () => {
if (typeof helper.contentWindow().$ === 'function') { if (typeof helper.contentWindow().$ === 'function') {
return helper.contentWindow().$('#timer'); return helper.contentWindow().$('#timer');
} }
@ -155,7 +142,7 @@ helper.timesliderTimer = function () {
* *
* @returns {HTMLElement} timer * @returns {HTMLElement} timer
*/ */
helper.timesliderTimerTime = function () { helper.timesliderTimerTime = () => {
if (helper.timesliderTimer()) { if (helper.timesliderTimer()) {
return helper.timesliderTimer().text(); return helper.timesliderTimer().text();
} }
@ -166,9 +153,7 @@ helper.timesliderTimerTime = function () {
* *
* @returns {HTMLElement} * @returns {HTMLElement}
*/ */
helper.sliderBar = function () { helper.sliderBar = () => helper.contentWindow().$('#ui-slider-bar');
return helper.contentWindow().$('#ui-slider-bar');
};
/** /**
* revision_date element * revision_date element
@ -176,9 +161,7 @@ helper.sliderBar = function () {
* *
* @returns {HTMLElement} * @returns {HTMLElement}
*/ */
helper.revisionDateElem = function () { helper.revisionDateElem = () => helper.contentWindow().$('#revision_date').text();
return helper.contentWindow().$('#revision_date').text();
};
/** /**
* revision_label element * revision_label element
@ -186,6 +169,4 @@ helper.revisionDateElem = function () {
* *
* @returns {HTMLElement} * @returns {HTMLElement}
*/ */
helper.revisionLabelElem = function () { helper.revisionLabelElem = () => helper.contentWindow().$('#revision_label');
return helper.contentWindow().$('#revision_label');
};