2020-11-23 18:21:51 +00:00
|
|
|
describe('the test helper', function () {
|
|
|
|
describe('the newPad method', function () {
|
|
|
|
xit("doesn't leak memory if you creates iframes over and over again", function (done) {
|
2012-10-08 11:37:24 +00:00
|
|
|
this.timeout(100000);
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
let times = 10;
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
var loadPad = function () {
|
|
|
|
helper.newPad(() => {
|
2012-10-07 22:34:29 +00:00
|
|
|
times--;
|
2020-11-23 18:21:51 +00:00
|
|
|
if (times > 0) {
|
2012-10-07 22:34:29 +00:00
|
|
|
loadPad();
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
2020-11-23 18:21:51 +00:00
|
|
|
});
|
|
|
|
};
|
2012-10-07 22:34:29 +00:00
|
|
|
|
|
|
|
loadPad();
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('gives me 3 jquery instances of chrome, outer and inner', function (done) {
|
2020-03-22 20:30:00 +00:00
|
|
|
this.timeout(10000);
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.newPad(() => {
|
|
|
|
// check if the jquery selectors have the desired elements
|
|
|
|
expect(helper.padChrome$('#editbar').length).to.be(1);
|
|
|
|
expect(helper.padOuter$('#outerdocbody').length).to.be(1);
|
|
|
|
expect(helper.padInner$('#innerdocbody').length).to.be(1);
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
// check if the document object was set correctly
|
2012-10-07 22:34:29 +00:00
|
|
|
expect(helper.padChrome$.window.document).to.be(helper.padChrome$.document);
|
|
|
|
expect(helper.padOuter$.window.document).to.be(helper.padOuter$.document);
|
|
|
|
expect(helper.padInner$.window.document).to.be(helper.padInner$.document);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2020-03-24 17:36:08 +00:00
|
|
|
// 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
|
|
|
|
// observed that the former can happen without the latter if there
|
|
|
|
// isn't a timeout (within `newPad`) after clearing the cookies.
|
|
|
|
// 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
|
|
|
|
// to catch it if the bug comes up again.
|
2020-11-23 18:21:51 +00:00
|
|
|
it('clears cookies', function (done) {
|
2020-03-24 17:36:08 +00:00
|
|
|
this.timeout(60000);
|
|
|
|
|
|
|
|
// set cookies far into the future to make sure they're not expired yet
|
|
|
|
window.document.cookie = 'token=foo;expires=Thu, 01 Jan 3030 00:00:00 GMT; path=/';
|
|
|
|
window.document.cookie = 'language=bar;expires=Thu, 01 Jan 3030 00:00:00 GMT; path=/';
|
|
|
|
|
2020-05-28 13:25:07 +00:00
|
|
|
expect(window.document.cookie).to.contain('token=foo');
|
|
|
|
expect(window.document.cookie).to.contain('language=bar');
|
2020-03-24 17:36:08 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.newPad(() => {
|
2020-03-24 17:36:08 +00:00
|
|
|
// 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
|
2020-11-23 18:21:51 +00:00
|
|
|
const firstCookie = window.document.cookie;
|
2020-05-28 13:25:07 +00:00
|
|
|
expect(firstCookie).to.not.contain('token=foo');
|
|
|
|
expect(firstCookie).to.not.contain('language=bar');
|
2020-03-24 17:36:08 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const chrome$ = helper.padChrome$;
|
2020-03-24 17:36:08 +00:00
|
|
|
|
|
|
|
// click on the settings button to make settings visible
|
2020-11-23 18:21:51 +00:00
|
|
|
const $userButton = chrome$('.buttonicon-showusers');
|
2020-03-24 17:36:08 +00:00
|
|
|
$userButton.click();
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $usernameInput = chrome$('#myusernameedit');
|
2020-03-24 17:36:08 +00:00
|
|
|
$usernameInput.click();
|
|
|
|
|
|
|
|
$usernameInput.val('John McLear');
|
|
|
|
$usernameInput.blur();
|
|
|
|
|
|
|
|
// Before refreshing, make sure the name is there
|
2020-05-28 13:25:07 +00:00
|
|
|
expect($usernameInput.val()).to.be('John McLear');
|
|
|
|
|
|
|
|
// Now that we have a chrome, we can set a pad cookie, so we can confirm it gets wiped as well
|
|
|
|
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), AND we
|
|
|
|
// didn't put path=/, we shouldn't expect it to be visible on window.document.cookie. Let's just
|
|
|
|
// be sure.
|
|
|
|
expect(window.document.cookie).to.not.contain('prefsHtml=baz');
|
2020-03-24 17:36:08 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
setTimeout(() => { // give it a second to save the username on the server side
|
|
|
|
helper.newPad(() => { // get a new pad, let it clear the cookies
|
|
|
|
const chrome$ = helper.padChrome$;
|
2020-03-24 17:36:08 +00:00
|
|
|
|
|
|
|
// helper function seems to have cleared cookies
|
|
|
|
// NOTE: this doesn't yet mean cookies were cleared effectively.
|
|
|
|
// We still need to test below that we're in a new session
|
2020-05-28 13:25:07 +00:00
|
|
|
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);
|
2020-03-24 17:36:08 +00:00
|
|
|
|
|
|
|
// click on the settings button to make settings visible
|
2020-11-23 18:21:51 +00:00
|
|
|
const $userButton = chrome$('.buttonicon-showusers');
|
2020-03-24 17:36:08 +00:00
|
|
|
$userButton.click();
|
|
|
|
|
|
|
|
// confirm that the session was actually cleared
|
2020-11-23 18:21:51 +00:00
|
|
|
const $usernameInput = chrome$('#myusernameedit');
|
2020-05-28 14:27:48 +00:00
|
|
|
expect($usernameInput.val()).to.be('');
|
2020-03-24 17:36:08 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, 1000);
|
2020-05-28 13:25:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('sets pad prefs cookie', function (done) {
|
2020-05-28 13:25:07 +00:00
|
|
|
this.timeout(60000);
|
|
|
|
|
|
|
|
helper.newPad({
|
2020-11-23 18:21:51 +00:00
|
|
|
padPrefs: {foo: 'bar'},
|
|
|
|
cb() {
|
|
|
|
const chrome$ = helper.padChrome$;
|
2020-05-28 13:25:07 +00:00
|
|
|
expect(chrome$.document.cookie).to.contain('prefsHttp=%7B%22');
|
|
|
|
expect(chrome$.document.cookie).to.contain('foo%22%3A%22bar');
|
|
|
|
done();
|
2020-11-23 18:21:51 +00:00
|
|
|
},
|
2020-05-28 13:25:07 +00:00
|
|
|
});
|
2020-03-24 17:36:08 +00:00
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('the waitFor method', function () {
|
|
|
|
it('takes a timeout and waits long enough', function (done) {
|
2013-12-05 07:41:29 +00:00
|
|
|
this.timeout(2000);
|
2020-11-23 18:21:51 +00:00
|
|
|
const startTime = Date.now();
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.waitFor(() => false, 1500).fail(() => {
|
|
|
|
const duration = Date.now() - startTime;
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(duration).to.be.greaterThan(1490);
|
2012-10-07 22:34:29 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('takes an interval and checks on every interval', function (done) {
|
2012-10-07 22:34:29 +00:00
|
|
|
this.timeout(4000);
|
2020-11-23 18:21:51 +00:00
|
|
|
let checks = 0;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.waitFor(() => {
|
2012-10-07 22:34:29 +00:00
|
|
|
checks++;
|
2013-12-05 07:41:29 +00:00
|
|
|
return false;
|
2020-11-23 18:21:51 +00:00
|
|
|
}, 2000, 100).fail(() => {
|
2020-10-21 17:43:17 +00:00
|
|
|
// One at the beginning, and 19-20 more depending on whether it's the timeout or the final
|
|
|
|
// poll that wins at 2000ms.
|
2020-10-30 10:40:11 +00:00
|
|
|
expect(checks).to.be.greaterThan(15);
|
2020-10-22 15:01:51 +00:00
|
|
|
expect(checks).to.be.lessThan(24);
|
2012-10-07 22:34:29 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('rejects if the predicate throws', async function () {
|
2020-10-14 01:57:19 +00:00
|
|
|
let err;
|
|
|
|
await helper.waitFor(() => { throw new Error('test exception'); })
|
|
|
|
.fail(() => {}) // Suppress the redundant uncatchable exception.
|
|
|
|
.catch((e) => { err = e; });
|
|
|
|
expect(err).to.be.an(Error);
|
|
|
|
expect(err.message).to.be('test exception');
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('returns a deferred object', function () {
|
|
|
|
it('it calls done after success', function (done) {
|
|
|
|
helper.waitFor(() => true).done(() => {
|
2012-10-07 22:34:29 +00:00
|
|
|
done();
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('calls fail after failure', function (done) {
|
|
|
|
helper.waitFor(() => false, 0).fail(() => {
|
2012-10-07 22:34:29 +00:00
|
|
|
done();
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
xit("throws if you don't listen for fails", function (done) {
|
|
|
|
const onerror = window.onerror;
|
|
|
|
window.onerror = function () {
|
2012-10-07 22:34:29 +00:00
|
|
|
window.onerror = onerror;
|
2013-12-05 07:41:29 +00:00
|
|
|
done();
|
2020-11-23 18:21:51 +00:00
|
|
|
};
|
2012-10-07 22:34:29 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.waitFor(() => false, 100);
|
2012-10-07 22:34:29 +00:00
|
|
|
});
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|
2020-10-14 02:02:46 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('checks first then sleeps', function () {
|
|
|
|
it('resolves quickly if the predicate is immediately true', async function () {
|
2020-10-14 02:02:46 +00:00
|
|
|
const before = Date.now();
|
|
|
|
await helper.waitFor(() => true, 1000, 900);
|
|
|
|
expect(Date.now() - before).to.be.lessThan(800);
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('polls exactly once if timeout < interval', async function () {
|
2020-10-14 02:02:46 +00:00
|
|
|
let calls = 0;
|
|
|
|
await helper.waitFor(() => { calls++; }, 1, 1000)
|
|
|
|
.fail(() => {}) // Suppress the redundant uncatchable exception.
|
|
|
|
.catch(() => {}); // Don't throw an exception -- we know it rejects.
|
|
|
|
expect(calls).to.be(1);
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('resolves if condition is immediately true even if timeout is 0', async function () {
|
2020-10-14 02:02:46 +00:00
|
|
|
await helper.waitFor(() => true, 0);
|
|
|
|
});
|
|
|
|
});
|
2012-10-07 22:34:29 +00:00
|
|
|
});
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('the waitForPromise method', function () {
|
|
|
|
it('returns a Promise', async function () {
|
2020-10-14 00:44:07 +00:00
|
|
|
expect(helper.waitForPromise(() => true)).to.be.a(Promise);
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('takes a timeout and waits long enough', async function () {
|
2020-10-09 18:50:47 +00:00
|
|
|
this.timeout(2000);
|
2020-10-14 00:44:07 +00:00
|
|
|
const startTime = Date.now();
|
|
|
|
let rejected;
|
|
|
|
await helper.waitForPromise(() => false, 1500)
|
|
|
|
.catch(() => { rejected = true; });
|
|
|
|
expect(rejected).to.be(true);
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
|
expect(duration).to.be.greaterThan(1490);
|
2020-10-09 18:50:47 +00:00
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('takes an interval and checks on every interval', async function () {
|
2020-10-09 18:50:47 +00:00
|
|
|
this.timeout(4000);
|
2020-10-14 00:44:07 +00:00
|
|
|
let checks = 0;
|
|
|
|
let rejected;
|
|
|
|
await helper.waitForPromise(() => { checks++; return false; }, 2000, 100)
|
|
|
|
.catch(() => { rejected = true; });
|
|
|
|
expect(rejected).to.be(true);
|
2020-11-23 18:21:51 +00:00
|
|
|
// `checks` is expected to be 20 or 21: one at the beginning, plus 19 or 20 more depending on
|
|
|
|
// whether it's the timeout or the final poll that wins at 2000ms. Margin is added to reduce
|
|
|
|
// flakiness on slow test machines.
|
2020-10-30 10:40:11 +00:00
|
|
|
expect(checks).to.be.greaterThan(15);
|
2020-10-14 15:54:39 +00:00
|
|
|
expect(checks).to.be.lessThan(24);
|
2020-10-09 18:50:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('the selectLines method', function () {
|
2016-06-21 09:48:10 +00:00
|
|
|
// function to support tests, use a single way to represent whitespaces
|
2020-11-23 18:21:51 +00:00
|
|
|
const cleanText = function (text) {
|
2016-06-21 09:48:10 +00:00
|
|
|
return text
|
2017-02-20 09:02:31 +00:00
|
|
|
// IE replaces line breaks with a whitespace, so we need to unify its behavior
|
|
|
|
// for other browsers, to have all tests running for all browsers
|
2020-11-23 18:21:51 +00:00
|
|
|
.replace(/\n/gi, '')
|
|
|
|
.replace(/\s/gi, ' ');
|
|
|
|
};
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
before(function (done) {
|
|
|
|
helper.newPad(() => {
|
2016-06-21 09:48:10 +00:00
|
|
|
// create some lines to be used on the tests
|
2020-11-23 18:21:51 +00:00
|
|
|
const $firstLine = helper.padInner$('div').first();
|
|
|
|
$firstLine.sendkeys('{selectall}some{enter}short{enter}lines{enter}to test{enter}{enter}');
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
// wait for lines to be split
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.waitFor(() => {
|
|
|
|
const $fourthLine = helper.padInner$('div').eq(3);
|
|
|
|
return $fourthLine.text() === 'to test';
|
2016-06-21 09:48:10 +00:00
|
|
|
}).done(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.timeout(60000);
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('changes editor selection to be between startOffset of $startLine and endOffset of $endLine', function (done) {
|
|
|
|
const inner$ = helper.padInner$;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const startOffset = 2;
|
|
|
|
const endOffset = 4;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $lines = inner$('div');
|
|
|
|
const $startLine = $lines.eq(1);
|
|
|
|
const $endLine = $lines.eq(3);
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
helper.selectLines($startLine, $endLine, startOffset, endOffset);
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const selection = inner$.document.getSelection();
|
2020-03-22 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* replace() is required here because Firefox keeps the line breaks.
|
|
|
|
*
|
|
|
|
* I'm not sure this is ideal behavior of getSelection() where the text
|
|
|
|
* is not consistent between browsers but that's the situation so that's
|
|
|
|
* how I'm covering it in this test.
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(cleanText(selection.toString().replace(/(\r\n|\n|\r)/gm, ''))).to.be('ort lines to t');
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('ends selection at beginning of $endLine when it is an empty line', function (done) {
|
|
|
|
const inner$ = helper.padInner$;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const startOffset = 2;
|
|
|
|
const endOffset = 1;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $lines = inner$('div');
|
|
|
|
const $startLine = $lines.eq(1);
|
|
|
|
const $endLine = $lines.eq(4);
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
helper.selectLines($startLine, $endLine, startOffset, endOffset);
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const selection = inner$.document.getSelection();
|
2020-03-22 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* replace() is required here because Firefox keeps the line breaks.
|
|
|
|
*
|
|
|
|
* I'm not sure this is ideal behavior of getSelection() where the text
|
|
|
|
* is not consistent between browsers but that's the situation so that's
|
|
|
|
* how I'm covering it in this test.
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(cleanText(selection.toString().replace(/(\r\n|\n|\r)/gm, ''))).to.be('ort lines to test');
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('ends selection at beginning of $endLine when its offset is zero', function (done) {
|
|
|
|
const inner$ = helper.padInner$;
|
2016-06-21 14:07:57 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const startOffset = 2;
|
|
|
|
const endOffset = 0;
|
2016-06-21 14:07:57 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $lines = inner$('div');
|
|
|
|
const $startLine = $lines.eq(1);
|
|
|
|
const $endLine = $lines.eq(3);
|
2016-06-21 14:07:57 +00:00
|
|
|
|
|
|
|
helper.selectLines($startLine, $endLine, startOffset, endOffset);
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const selection = inner$.document.getSelection();
|
2020-03-22 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* replace() is required here because Firefox keeps the line breaks.
|
|
|
|
*
|
|
|
|
* I'm not sure this is ideal behavior of getSelection() where the text
|
|
|
|
* is not consistent between browsers but that's the situation so that's
|
|
|
|
* how I'm covering it in this test.
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(cleanText(selection.toString().replace(/(\r\n|\n|\r)/gm, ''))).to.be('ort lines ');
|
2016-06-21 14:07:57 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('selects full line when offset is longer than line content', function (done) {
|
|
|
|
const inner$ = helper.padInner$;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const startOffset = 2;
|
|
|
|
const endOffset = 50;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $lines = inner$('div');
|
|
|
|
const $startLine = $lines.eq(1);
|
|
|
|
const $endLine = $lines.eq(3);
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
helper.selectLines($startLine, $endLine, startOffset, endOffset);
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const selection = inner$.document.getSelection();
|
2020-03-22 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* replace() is required here because Firefox keeps the line breaks.
|
|
|
|
*
|
|
|
|
* I'm not sure this is ideal behavior of getSelection() where the text
|
|
|
|
* is not consistent between browsers but that's the situation so that's
|
|
|
|
* how I'm covering it in this test.
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(cleanText(selection.toString().replace(/(\r\n|\n|\r)/gm, ''))).to.be('ort lines to test');
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('selects all text between beginning of $startLine and end of $endLine when no offset is provided', function (done) {
|
|
|
|
const inner$ = helper.padInner$;
|
2016-06-21 09:48:10 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const $lines = inner$('div');
|
|
|
|
const $startLine = $lines.eq(1);
|
|
|
|
const $endLine = $lines.eq(3);
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
helper.selectLines($startLine, $endLine);
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const selection = inner$.document.getSelection();
|
2020-03-22 20:32:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* replace() is required here because Firefox keeps the line breaks.
|
|
|
|
*
|
|
|
|
* I'm not sure this is ideal behavior of getSelection() where the text
|
|
|
|
* is not consistent between browsers but that's the situation so that's
|
|
|
|
* how I'm covering it in this test.
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(cleanText(selection.toString().replace(/(\r\n|\n|\r)/gm, ''))).to.be('short lines to test');
|
2016-06-21 09:48:10 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe('helper', function () {
|
|
|
|
before(function (cb) {
|
|
|
|
helper.newPad(() => {
|
2020-10-21 17:43:17 +00:00
|
|
|
cb();
|
2020-11-23 18:21:51 +00:00
|
|
|
});
|
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('.textLines() returns the text of the pad as strings', async function () {
|
|
|
|
const lines = helper.textLines();
|
|
|
|
const defaultText = helper.defaultText();
|
2020-10-21 17:43:17 +00:00
|
|
|
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
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(`${lines.join('\n')}\n`).to.equal(defaultText);
|
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('.linesDiv() returns the text of the pad as div elements', async function () {
|
|
|
|
const lines = helper.linesDiv();
|
|
|
|
const defaultText = helper.defaultText();
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(Array.isArray(lines)).to.be(true);
|
|
|
|
expect(lines[0]).to.be.an('object');
|
|
|
|
expect(lines[0].text()).to.be.an('string');
|
2020-11-23 18:21:51 +00:00
|
|
|
_.each(defaultText.split('\n'), (line, index) => {
|
|
|
|
// last line of default text
|
|
|
|
if (index === lines.length) {
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(line).to.equal('');
|
|
|
|
} else {
|
|
|
|
expect(lines[index].text()).to.equal(line);
|
|
|
|
}
|
2020-11-23 18:21:51 +00:00
|
|
|
});
|
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('.edit() defaults to send an edit to the first line', async function () {
|
|
|
|
const firstLine = helper.textLines()[0];
|
|
|
|
await helper.edit('line');
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(helper.textLines()[0]).to.be(`line${firstLine}`);
|
2020-11-23 18:21:51 +00:00
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('.edit() to the line specified with parameter lineNo', async function () {
|
|
|
|
const firstLine = helper.textLines()[0];
|
|
|
|
await helper.edit('second line', 2);
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const text = helper.textLines();
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(text[0]).to.equal(firstLine);
|
2020-11-23 18:21:51 +00:00
|
|
|
expect(text[1]).to.equal('second line');
|
|
|
|
});
|
2020-10-21 17:43:17 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('.edit() supports sendkeys syntax ({selectall},{del},{enter})', async function () {
|
2020-10-21 17:43:17 +00:00
|
|
|
expect(helper.textLines()[0]).to.not.equal('');
|
|
|
|
|
|
|
|
// select first line
|
2020-11-23 18:21:51 +00:00
|
|
|
helper.linesDiv()[0].sendkeys('{selectall}');
|
2020-10-21 17:43:17 +00:00
|
|
|
// delete first line
|
2020-11-23 18:21:51 +00:00
|
|
|
await helper.edit('{del}');
|
2020-10-21 17:43:17 +00:00
|
|
|
|
|
|
|
expect(helper.textLines()[0]).to.be('');
|
2020-11-23 18:21:51 +00:00
|
|
|
const noOfLines = helper.textLines().length;
|
|
|
|
await helper.edit('{enter}');
|
|
|
|
expect(helper.textLines().length).to.be(noOfLines + 1);
|
|
|
|
});
|
|
|
|
});
|
2013-12-05 07:41:29 +00:00
|
|
|
});
|