tests: refactor some frontend tests (#4408)

* don't include sendkeys in index.html as it's included in helper.init
mocha opts: add default timeout and replace ignoreLeaks with checkLeaks,
as the former is deprecated

* introduce helper.edit to write to a pad

* add test to check if helper.edit() supports line numbers

* helper tests: waitFor/waitForPromise seem to be a little bit faster sometimes

* tests: refactor chat.js

* tests: refactor timeslider_numeric_padID

* tests: refactor timeslider_labels

* tests: refactor timeslider_follow

* ensure followContents is enabled, although it should be by default

* timeslider_follow: increase number of revision for Edge

* make textLines() depend on linesDiv()

Co-authored-by: Richard Hansen <rhansen@rhansen.org>

* make linesDiv return standard Array

* use `contain` instead of `indexOf`

* more fixes from the review

* review fixes

* align waitFor and waitForPromise behaviour

* timeslider_follow: check if it's following to the correct lines

* lower expected waitFor/waitForPromise interval check

* disable responsivness and regression test in timeslider_follow

* timeslider_follow: fix Range detection

* more explicit test for linesDiv

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
This commit is contained in:
webzwo0i 2020-10-21 19:43:17 +02:00 committed by GitHub
parent 94cb000e8f
commit 69c7033a86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 685 additions and 252 deletions

View file

@ -2,134 +2,78 @@ describe("Chat messages and UI", function(){
//create a new pad before each test run
beforeEach(function(cb){
helper.newPad(cb);
this.timeout(60000);
});
it("opens chat, sends a message and makes sure it exists on the page", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
it("opens chat, sends a message, makes sure it exists on the page and hides chat", async function() {
var chatValue = "JohnMcLear";
//click on the chat button to make chat visible
var $chatButton = chrome$("#chaticon");
$chatButton.click();
var $chatInput = chrome$("#chatinput");
$chatInput.sendkeys('JohnMcLear'); // simulate a keypress of typing JohnMcLear
$chatInput.sendkeys('{enter}'); // simulate a keypress of enter actually does evt.which = 10 not 13
await helper.showChat();
await helper.sendChatMessage(`${chatValue}{enter}`);
//check if chat shows up
helper.waitFor(function(){
return chrome$("#chattext").children("p").length !== 0; // wait until the chat message shows up
}).done(function(){
var $firstChatMessage = chrome$("#chattext").children("p");
var containsMessage = $firstChatMessage.text().indexOf("JohnMcLear") !== -1; // does the string contain JohnMcLear?
expect(containsMessage).to.be(true); // expect the first chat message to contain JohnMcLear
expect(helper.chatTextParagraphs().length).to.be(1);
// do a slightly more thorough check
var username = $firstChatMessage.children("b");
var usernameValue = username.text();
var time = $firstChatMessage.children(".time");
var timeValue = time.text();
var discoveredValue = $firstChatMessage.text();
var chatMsgExists = (discoveredValue.indexOf("JohnMcLear") !== -1);
expect(chatMsgExists).to.be(true);
done();
});
// <p data-authorid="a.qjkwNs4z0pPROphS"
// class="author-a-qjkwz78zs4z122z0pz80zz82zz79zphz83z">
// <b>unnamed:</b>
// <span class="time author-a-qjkwz78zs4z122z0pz80zz82zz79zphz83z">12:38
// </span> JohnMcLear
// </p>
let username = helper.chatTextParagraphs().children("b").text();
let time = helper.chatTextParagraphs().children(".time").text();
expect(helper.chatTextParagraphs().text()).to.be(`${username}${time} ${chatValue}`);
await helper.hideChat();
});
it("makes sure that an empty message can't be sent", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
it("makes sure that an empty message can't be sent", async function() {
var chatValue = "mluto";
//click on the chat button to make chat visible
var $chatButton = chrome$("#chaticon");
$chatButton.click();
var $chatInput = chrome$("#chatinput");
$chatInput.sendkeys('{enter}'); // simulate a keypress of enter (to send an empty message)
$chatInput.sendkeys('mluto'); // simulate a keypress of typing mluto
$chatInput.sendkeys('{enter}'); // simulate a keypress of enter (to send 'mluto')
await helper.showChat();
//check if chat shows up
helper.waitFor(function(){
return chrome$("#chattext").children("p").length !== 0; // wait until the chat message shows up
}).done(function(){
// check that the empty message is not there
expect(chrome$("#chattext").children("p").length).to.be(1);
// check that the received message is not the empty one
var $firstChatMessage = chrome$("#chattext").children("p");
var containsMessage = $firstChatMessage.text().indexOf("mluto") !== -1;
expect(containsMessage).to.be(true);
done();
});
await helper.sendChatMessage(`{enter}${chatValue}{enter}`); // simulate a keypress of typing enter, mluto and enter (to send 'mluto')
let chat = helper.chatTextParagraphs();
expect(chat.length).to.be(1);
// check that the received message is not the empty one
let username = chat.children("b").text();
let time = chat.children(".time").text();
expect(chat.text()).to.be(`${username}${time} ${chatValue}`);
});
it("makes chat stick to right side of the screen", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
it("makes chat stick to right side of the screen via settings, remove sticky via settings, close it", async function() {
await helper.showSettings();
//click on the settings button to make settings visible
var $settingsButton = chrome$(".buttonicon-settings");
$settingsButton.click();
//get the chat selector
var $stickychatCheckbox = chrome$("#options-stickychat");
//select chat always on screen
if (!$stickychatCheckbox.is(':checked')) {
$stickychatCheckbox.click();
}
// due to animation, we need to make some timeout...
setTimeout(function() {
//check if chat changed to get the stickychat Class
var $chatbox = chrome$("#chatbox");
var hasStickyChatClass = $chatbox.hasClass("stickyChat");
expect(hasStickyChatClass).to.be(true);
// select chat always on screen and fire change event
$stickychatCheckbox.click();
setTimeout(function() {
//check if chat changed to remove the stickychat Class
var hasStickyChatClass = $chatbox.hasClass("stickyChat");
expect(hasStickyChatClass).to.be(false);
done();
}, 10)
}, 10)
await helper.enableStickyChatviaSettings();
expect(helper.isChatboxShown()).to.be(true);
expect(helper.isChatboxSticky()).to.be(true);
await helper.disableStickyChatviaSettings();
expect(helper.isChatboxSticky()).to.be(false);
expect(helper.isChatboxShown()).to.be(true);
await helper.hideChat();
expect(helper.isChatboxSticky()).to.be(false);
expect(helper.isChatboxShown()).to.be(false);
});
it("makes chat stick to right side of the screen then makes it one step smaller", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
it("makes chat stick to right side of the screen via icon on the top right, remove sticky via icon, close it", async function() {
await helper.showChat();
// open chat
chrome$('#chaticon').click();
await helper.enableStickyChatviaIcon();
expect(helper.isChatboxShown()).to.be(true);
expect(helper.isChatboxSticky()).to.be(true);
// select chat always on screen from chatbox
chrome$('.stick-to-screen-btn').click();
await helper.disableStickyChatviaIcon();
expect(helper.isChatboxShown()).to.be(true);
expect(helper.isChatboxSticky()).to.be(false);
// due to animation, we need to make some timeout...
setTimeout(function() {
//check if chat changed to get the stickychat Class
var $chatbox = chrome$("#chatbox");
var hasStickyChatClass = $chatbox.hasClass("stickyChat");
expect(hasStickyChatClass).to.be(true);
// select chat always on screen and fire change event
chrome$('#titlecross').click();
setTimeout(function() {
//check if chat changed to remove the stickychat Class
var hasStickyChatClass = $chatbox.hasClass("stickyChat");
expect(hasStickyChatClass).to.be(false);
done();
}, 10)
}, 10)
await helper.hideChat();
expect(helper.isChatboxSticky()).to.be(false);
expect(helper.isChatboxShown()).to.be(false);
});
xit("Checks showChat=false URL Parameter hides chat then when removed it shows chat", function(done) {

View file

@ -17,7 +17,7 @@ describe("delete keystroke", function(){
// get the original string value minus the last char
var originalTextValue = $firstTextElement.text();
originalTextValueMinusFirstChar = originalTextValue.substring(1, originalTextValue.length );
var originalTextValueMinusFirstChar = originalTextValue.substring(1, originalTextValue.length );
// simulate key presses to delete content
$firstTextElement.sendkeys('{leftarrow}'); // simulate a keypress of the left arrow key

View file

@ -136,7 +136,7 @@ describe("the test helper", function(){
return false;
}, 1500).fail(function(){
var duration = Date.now() - startTime;
expect(duration).to.be.greaterThan(1400);
expect(duration).to.be.greaterThan(1490);
done();
});
});
@ -149,8 +149,10 @@ describe("the test helper", function(){
checks++;
return false;
}, 2000, 100).fail(function(){
expect(checks).to.be.greaterThan(10);
expect(checks).to.be.lessThan(30);
// One at the beginning, and 19-20 more depending on whether it's the timeout or the final
// poll that wins at 2000ms.
expect(checks).to.be.greaterThan(18);
expect(checks).to.be.lessThan(22);
done();
});
});
@ -399,4 +401,68 @@ describe("the test helper", function(){
done();
});
});
describe('helper',function(){
before(function(cb){
helper.newPad(function(){
cb();
})
})
it(".textLines() returns the text of the pad as strings", async function(){
let lines = helper.textLines();
let defaultText = helper.defaultText();
expect(Array.isArray(lines)).to.be(true);
expect(lines[0]).to.be.an('string');
// @todo
// final "\n" is added automatically, but my understanding is this should happen
// only when the default text does not end with "\n" already
expect(lines.join("\n")+"\n").to.equal(defaultText);
})
it(".linesDiv() returns the text of the pad as div elements", async function(){
let lines = helper.linesDiv();
let defaultText = helper.defaultText();
expect(Array.isArray(lines)).to.be(true);
expect(lines[0]).to.be.an('object');
expect(lines[0].text()).to.be.an('string');
_.each(defaultText.split("\n"), function(line, index){
//last line of default text
if(index === lines.length){
expect(line).to.equal('');
} else {
expect(lines[index].text()).to.equal(line);
}
})
})
it(".edit() defaults to send an edit to the first line", async function(){
let firstLine = helper.textLines()[0];
await helper.edit("line")
expect(helper.textLines()[0]).to.be(`line${firstLine}`);
})
it(".edit() to the line specified with parameter lineNo", async function(){
let firstLine = helper.textLines()[0];
await helper.edit("second line", 2);
let text = helper.textLines();
expect(text[0]).to.equal(firstLine);
expect(text[1]).to.equal("second line");
})
it(".edit() supports sendkeys syntax ({selectall},{del},{enter})", async function(){
expect(helper.textLines()[0]).to.not.equal('');
// select first line
helper.linesDiv()[0].sendkeys("{selectall}")
// delete first line
await helper.edit("{del}")
expect(helper.textLines()[0]).to.be('');
let noOfLines = helper.textLines().length;
await helper.edit("{enter}")
expect(helper.textLines().length).to.be(noOfLines+1);
})
})
});

View file

@ -13,7 +13,7 @@
// Adapted from John McLear's original test case.
describe('Responsiveness of Editor', function() {
xdescribe('Responsiveness of Editor', function() {
// create a new pad before each test run
beforeEach(function(cb) {
helper.newPad(cb);

View file

@ -1,55 +1,120 @@
describe("timeslider", function(){
describe("timeslider follow", function(){
//create a new pad before each test run
beforeEach(function(cb){
helper.newPad(cb);
this.timeout(6000);
});
it("follow content as it's added to timeslider", function(done) { // passes
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
// make some changes to produce 100 revisions
var timePerRev = 900
, revs = 10;
this.timeout(revs*timePerRev+10000);
for(var i=0; i < revs; i++) {
setTimeout(function() {
// enter 'a' in the first text element
inner$("div").last().sendkeys('a\n');
inner$("div").last().sendkeys('{enter}');
inner$("div").last().sendkeys('{enter}');
inner$("div").last().sendkeys('{enter}');
inner$("div").last().sendkeys('{enter}');
}, timePerRev*i);
it("content as it's added to timeslider", async function() {
// send 6 revisions
let revs = 6;
let message = 'a\n\n\n\n\n\n\n\n\n\n';
let newLines = message.split('\n').length
for (let i=0;i<revs;i++){
await helper.edit(message, newLines*i + 1);
}
setTimeout(function() {
// go to timeslider
$('#iframe-container iframe').attr('src', $('#iframe-container iframe').attr('src')+'/timeslider#0');
await helper.gotoTimeslider(0);
await helper.waitForPromise(function(){return helper.contentWindow().location.hash === '#0'})
setTimeout(function() {
var timeslider$ = $('#iframe-container iframe')[0].contentWindow.$;
var $sliderBar = timeslider$('#ui-slider-bar');
let originalTop = helper.contentWindow().$('#innerdocbody').offset();
var latestContents = timeslider$('#innerdocbody').text();
// set to follow contents as it arrives
helper.contentWindow().$('#options-followContents').prop("checked", true);
helper.contentWindow().$('#playpause_button_icon').click();
// set to follow contents as it arrives
timeslider$('#options-followContents').prop("checked", true);
var originalTop = timeslider$('#innerdocbody').offset();
timeslider$('#playpause_button_icon').click();
setTimeout(function() {
//make sure the text has changed
var newTop = timeslider$('#innerdocbody').offset();
expect( originalTop ).not.to.eql( newTop );
done();
}, 1000);
}, 2000);
}, revs*timePerRev);
let newTop;
return helper.waitForPromise(function(){
newTop = helper.contentWindow().$('#innerdocbody').offset();
return newTop.top < originalTop.top;
})
});
/**
* Tests for bug described in #4389
* The goal is to scroll to the first line that contains a change right before
* the change is applied.
*
*/
it("only to lines that exist in the current pad view, see #4389", async function(){
// Select everything and clear via delete key
let e = helper.padInner$.Event(helper.evtType);
e.keyCode = 8; //delete key
let lines = helper.linesDiv();
helper.selectLines(lines[0], lines[lines.length - 1]); // select all lines
// probably unnecessary, but wait for the selection to be Range not Caret
await helper.waitForPromise(function(){
return !helper.padInner$.document.getSelection().isCollapsed;
//only supported in FF57+
//return helper.padInner$.document.getSelection().type === 'Range';
})
helper.padInner$('#innerdocbody').trigger(e);
await helper.waitForPromise(function(){
return helper.commits.length === 1;
})
await helper.edit("Test line\n\n")
await helper.edit("Another test line", 3)
await helper.gotoTimeslider();
// set to follow contents as it arrives
helper.contentWindow().$('#options-followContents').prop("checked", true);
let oldYPosition = helper.contentWindow().$("#editorcontainerbox")[0].scrollTop;
expect(oldYPosition).to.be(0);
/**
* pad content rev 0 [default Pad text]
* pad content rev 1 ['']
* pad content rev 2 ['Test line','','']
* pad content rev 3 ['Test line','','Another test line']
*/
// line 3 changed
helper.contentWindow().$('#leftstep').click();
await helper.waitForPromise(function(){
return hasFollowedToLine(3);
})
// line 1 is the first line that changed
helper.contentWindow().$('#leftstep').click();
await helper.waitForPromise(function(){
return hasFollowedToLine(1);
})
// line 1 changed
helper.contentWindow().$('#leftstep').click();
await helper.waitForPromise(function(){
return hasFollowedToLine(1);
})
// line 1 changed
helper.contentWindow().$('#rightstep').click();
await helper.waitForPromise(function(){
return hasFollowedToLine(1);
})
// line 1 is the first line that changed
helper.contentWindow().$('#rightstep').click();
await helper.waitForPromise(function(){
return hasFollowedToLine(1);
})
// line 3 changed
helper.contentWindow().$('#rightstep').click();
return helper.waitForPromise(function(){
return hasFollowedToLine(3);
})
})
});
/**
* @param {number} lineNum
* @returns {boolean} scrolled to the lineOffset?
*/
function hasFollowedToLine(lineNum) {
let scrollPosition = helper.contentWindow().$("#editorcontainerbox")[0].scrollTop;
let lineOffset = helper.contentWindow().$('#innerdocbody').find(`div:nth-child(${lineNum})`)[0].offsetTop;
return Math.abs(scrollPosition - lineOffset) < 1;
}

View file

@ -2,63 +2,61 @@ describe("timeslider", function(){
//create a new pad before each test run
beforeEach(function(cb){
helper.newPad(cb);
this.timeout(60000);
});
it("Shows a date and time in the timeslider and make sure it doesn't include NaN", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
/**
* @todo test authorsList
*/
it("Shows a date and time in the timeslider and make sure it doesn't include NaN", async function() {
// make some changes to produce 3 revisions
let revs = 3;
// make some changes to produce 100 revisions
var revs = 10;
this.timeout(60000);
for(var i=0; i < revs; i++) {
setTimeout(function() {
// enter 'a' in the first text element
inner$("div").first().sendkeys('a');
}, 200);
for(let i=0; i < revs; i++) {
await helper.edit('a\n');
}
setTimeout(function() {
// go to timeslider
$('#iframe-container iframe').attr('src', $('#iframe-container iframe').attr('src')+'/timeslider');
await helper.gotoTimeslider(revs);
await helper.waitForPromise(function(){return helper.contentWindow().location.hash === '#'+revs})
setTimeout(function() {
var timeslider$ = $('#iframe-container iframe')[0].contentWindow.$;
var $sliderBar = timeslider$('#ui-slider-bar');
// the datetime of last edit
let timerTimeLast = new Date(helper.timesliderTimerTime()).getTime();
var latestContents = timeslider$('#padcontent').text();
// the day of this revision, e.g. August 12, 2020 (stripped the string "Saved")
let dateLast = new Date(helper.revisionDateElem().substr(6)).getTime();
// Expect the date and time to be shown
// the label/revision, ie Version 3
let labelLast = helper.revisionLabelElem().text();
// Click somewhere on the timeslider
var e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 45;
$sliderBar.trigger(e);
// the datetime should be a date
expect( Number.isNaN(timerTimeLast)).to.eql(false);
e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 40;
$sliderBar.trigger(e);
// the Date object of the day should not be NaN
expect( Number.isNaN(dateLast) ).to.eql(false)
e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 50;
$sliderBar.trigger(e);
// the label should be Version `Number`
expect(labelLast).to.be(`Version ${revs}`);
$sliderBar.trigger('mouseup')
// Click somewhere left on the timeslider to go to revision 0
helper.sliderClick(1);
setTimeout(function() {
//make sure the text has changed
expect( timeslider$('#timer').text() ).not.to.eql( "" );
expect( timeslider$('#revision_date').text() ).not.to.eql( "" );
expect( timeslider$('#revision_label').text() ).not.to.eql( "" );
var includesNaN = timeslider$('#revision_label').text().indexOf("NaN"); // NaN is bad. Naan ist gut
expect( includesNaN ).to.eql( -1 ); // not quite so tasty, I like curry.
done();
}, 400);
}, 2000);
}, 2000);
// the datetime of last edit
let timerTime = new Date(helper.timesliderTimerTime()).getTime();
// the day of this revision, e.g. August 12, 2020
let date = new Date(helper.revisionDateElem().substr(6)).getTime();
// the label/revision, e.g. Version 0
let label = helper.revisionLabelElem().text();
// the datetime should be a date
expect( Number.isNaN(timerTime)).to.eql(false);
// the last revision should be newer or have the same time
expect(timerTimeLast).to.not.be.lessThan(timerTime);
// the Date object of the day should not be NaN
expect( Number.isNaN(date) ).to.eql(false)
// the label should be Version 0
expect( label ).to.be('Version 0');
});
});

View file

@ -4,64 +4,26 @@ describe("timeslider", function(){
//create a new pad before each test run
beforeEach(function(cb){
helper.newPad(cb, padId);
this.timeout(60000);
});
it("Makes sure the export URIs are as expected when the padID is numeric", function(done) {
var inner$ = helper.padInner$;
var chrome$ = helper.padChrome$;
it("Makes sure the export URIs are as expected when the padID is numeric", async function() {
await helper.edit('a\n');
// make some changes to produce 100 revisions
var revs = 10;
this.timeout(60000);
for(var i=0; i < revs; i++) {
setTimeout(function() {
// enter 'a' in the first text element
inner$("div").first().sendkeys('a');
}, 100);
}
await helper.gotoTimeslider(1);
setTimeout(function() {
// go to timeslider
$('#iframe-container iframe').attr('src', $('#iframe-container iframe').attr('src')+'/timeslider');
// ensure we are on revision 1
await helper.waitForPromise(function(){return helper.contentWindow().location.hash === '#1'})
setTimeout(function() {
var timeslider$ = $('#iframe-container iframe')[0].contentWindow.$;
var $sliderBar = timeslider$('#ui-slider-bar');
// expect URI to be similar to
// http://192.168.1.48:9001/p/2/1/export/html
// http://192.168.1.48:9001/p/735773577399/1/export/html
let rev1ExportLink = helper.contentWindow().$('#exporthtmla').attr('href');
expect(rev1ExportLink).to.contain('/1/export/html');
var latestContents = timeslider$('#padcontent').text();
// Click somewhere left on the timeslider to go to revision 0
helper.sliderClick(30);
// Expect the date and time to be shown
// Click somewhere on the timeslider
var e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 45;
$sliderBar.trigger(e);
e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 40;
$sliderBar.trigger(e);
e = new jQuery.Event('mousedown');
e.clientX = e.pageX = 150;
e.clientY = e.pageY = 50;
$sliderBar.trigger(e);
$sliderBar.trigger('mouseup')
setTimeout(function() {
// expect URI to be similar to
// http://192.168.1.48:9001/p/2/2/export/html
// http://192.168.1.48:9001/p/735773577399/0/export/html
var exportLink = timeslider$('#exporthtmla').attr('href');
var checkVal = padId + "/0/export/html";
var includesCorrectURI = exportLink.indexOf(checkVal);
expect(includesCorrectURI).to.not.be(-1);
done();
}, 400);
}, 2000);
}, 2000);
let rev0ExportLink = helper.contentWindow().$('#exporthtmla').attr('href');
expect(rev0ExportLink).to.contain('/0/export/html');
});
});