tests: Promisify some of the helper.js tests

This commit is contained in:
Richard Hansen 2021-03-31 00:08:35 -04:00 committed by webzwo0i
parent 701a40ac13
commit 63e6e163b7

View file

@ -21,23 +21,19 @@ describe('the test helper', function () {
loadPad(); loadPad();
}); });
it('gives me 3 jquery instances of chrome, outer and inner', function (done) { it('gives me 3 jquery instances of chrome, outer and inner', async function () {
this.timeout(10000); this.timeout(10000);
await helper.aNewPad();
helper.newPad(() => { // check if the jquery selectors have the desired elements
// check if the jquery selectors have the desired elements expect(helper.padChrome$('#editbar').length).to.be(1);
expect(helper.padChrome$('#editbar').length).to.be(1); expect(helper.padOuter$('#outerdocbody').length).to.be(1);
expect(helper.padOuter$('#outerdocbody').length).to.be(1); expect(helper.padInner$('#innerdocbody').length).to.be(1);
expect(helper.padInner$('#innerdocbody').length).to.be(1); // check if the document object was set correctly
expect(helper.padChrome$.window.document).to.be(helper.padChrome$.document);
// check if the document object was set correctly expect(helper.padOuter$.window.document).to.be(helper.padOuter$.document);
expect(helper.padChrome$.window.document).to.be(helper.padChrome$.document); expect(helper.padInner$.window.document).to.be(helper.padInner$.document);
expect(helper.padOuter$.window.document).to.be(helper.padOuter$.document);
expect(helper.padInner$.window.document).to.be(helper.padInner$.document);
done();
});
}); });
// Make sure the cookies are cleared, and make sure that the cookie // Make sure the cookies are cleared, and make sure that the cookie
// clearing has taken effect at this point in the code. It has been // clearing has taken effect at this point in the code. It has been
// observed that the former can happen without the latter if there // observed that the former can happen without the latter if there
@ -45,7 +41,7 @@ describe('the test helper', function () {
// However this doesn't seem to always be easily replicated, so this // However this doesn't seem to always be easily replicated, so this
// timeout may or may end up in the code. None the less, we test here // timeout may or may end up in the code. None the less, we test here
// to catch it if the bug comes up again. // to catch it if the bug comes up again.
it('clears cookies', function (done) { it('clears cookies', async function () {
this.timeout(60000); this.timeout(60000);
// set cookies far into the future to make sure they're not expired yet // set cookies far into the future to make sure they're not expired yet
@ -55,78 +51,70 @@ describe('the test helper', function () {
expect(window.document.cookie).to.contain('token=foo'); expect(window.document.cookie).to.contain('token=foo');
expect(window.document.cookie).to.contain('language=bar'); expect(window.document.cookie).to.contain('language=bar');
helper.newPad(() => { await helper.aNewPad();
// helper function seems to have cleared cookies
// NOTE: this doesn't yet mean it's proven to have taken effect by this point in execution
const firstCookie = window.document.cookie;
expect(firstCookie).to.not.contain('token=foo');
expect(firstCookie).to.not.contain('language=bar');
const chrome$ = helper.padChrome$; // helper function seems to have cleared cookies
// NOTE: this doesn't yet mean it's proven to have taken effect by this point in execution
const firstCookie = window.document.cookie;
expect(firstCookie).to.not.contain('token=foo');
expect(firstCookie).to.not.contain('language=bar');
// click on the settings button to make settings visible let chrome$ = helper.padChrome$;
const $userButton = chrome$('.buttonicon-showusers');
$userButton.click();
const $usernameInput = chrome$('#myusernameedit'); // click on the settings button to make settings visible
$usernameInput.click(); let $userButton = chrome$('.buttonicon-showusers');
$userButton.click();
$usernameInput.val('John McLear'); let $usernameInput = chrome$('#myusernameedit');
$usernameInput.blur(); $usernameInput.click();
// Before refreshing, make sure the name is there $usernameInput.val('John McLear');
expect($usernameInput.val()).to.be('John McLear'); $usernameInput.blur();
// Now that we have a chrome, we can set a pad cookie // Before refreshing, make sure the name is there
// so we can confirm it gets wiped as well expect($usernameInput.val()).to.be('John McLear');
chrome$.document.cookie = 'prefsHtml=baz;expires=Thu, 01 Jan 3030 00:00:00 GMT';
expect(chrome$.document.cookie).to.contain('prefsHtml=baz');
// Cookies are weird. Because it's attached to chrome$ (as helper.setPadCookies does) // Now that we have a chrome, we can set a pad cookie
// AND we didn't put path=/, we shouldn't expect it to be visible on // so we can confirm it gets wiped as well
// window.document.cookie. Let's just be sure. chrome$.document.cookie = 'prefsHtml=baz;expires=Thu, 01 Jan 3030 00:00:00 GMT';
expect(window.document.cookie).to.not.contain('prefsHtml=baz'); expect(chrome$.document.cookie).to.contain('prefsHtml=baz');
setTimeout(() => { // give it a second to save the username on the server side // Cookies are weird. Because it's attached to chrome$ (as helper.setPadCookies does)
helper.newPad(() => { // get a new pad, let it clear the cookies // AND we didn't put path=/, we shouldn't expect it to be visible on
const chrome$ = helper.padChrome$; // window.document.cookie. Let's just be sure.
expect(window.document.cookie).to.not.contain('prefsHtml=baz');
// helper function seems to have cleared cookies // give it a second to save the username on the server side
// NOTE: this doesn't yet mean cookies were cleared effectively. await new Promise((resolve) => setTimeout(resolve, 1000));
// We still need to test below that we're in a new session
expect(window.document.cookie).to.not.contain('token=foo');
expect(window.document.cookie).to.not.contain('language=bar');
expect(chrome$.document.cookie).to.contain('prefsHtml=baz');
expect(window.document.cookie).to.not.contain('prefsHtml=baz');
expect(window.document.cookie).to.not.be(firstCookie); await helper.aNewPad(); // get a new pad, let it clear the cookies
chrome$ = helper.padChrome$;
// click on the settings button to make settings visible // helper function seems to have cleared cookies
const $userButton = chrome$('.buttonicon-showusers'); // NOTE: this doesn't yet mean cookies were cleared effectively.
$userButton.click(); // We still need to test below that we're in a new session
expect(window.document.cookie).to.not.contain('token=foo');
expect(window.document.cookie).to.not.contain('language=bar');
expect(chrome$.document.cookie).to.contain('prefsHtml=baz');
expect(window.document.cookie).to.not.contain('prefsHtml=baz');
// confirm that the session was actually cleared expect(window.document.cookie).to.not.be(firstCookie);
const $usernameInput = chrome$('#myusernameedit');
expect($usernameInput.val()).to.be('');
done(); // click on the settings button to make settings visible
}); $userButton = chrome$('.buttonicon-showusers');
}, 1000); $userButton.click();
});
// confirm that the session was actually cleared
$usernameInput = chrome$('#myusernameedit');
expect($usernameInput.val()).to.be('');
}); });
it('sets pad prefs cookie', function (done) { it('sets pad prefs cookie', async function () {
this.timeout(60000); this.timeout(60000);
await helper.aNewPad({padPrefs: {foo: 'bar'}});
helper.newPad({ const chrome$ = helper.padChrome$;
padPrefs: {foo: 'bar'}, expect(chrome$.document.cookie).to.contain('prefsHttp=%7B%22');
cb() { expect(chrome$.document.cookie).to.contain('foo%22%3A%22bar');
const chrome$ = helper.padChrome$;
expect(chrome$.document.cookie).to.contain('prefsHttp=%7B%22');
expect(chrome$.document.cookie).to.contain('foo%22%3A%22bar');
done();
},
});
}); });
}); });
@ -416,10 +404,8 @@ describe('the test helper', function () {
}); });
describe('helper', function () { describe('helper', function () {
before(function (cb) { before(async function () {
helper.newPad(() => { await helper.aNewPad();
cb();
});
}); });
it('.textLines() returns the text of the pad as strings', async function () { it('.textLines() returns the text of the pad as strings', async function () {