tests: add waitForPromise method and test for it

pull/4415/head
webzwo0i 2020-10-09 20:50:47 +02:00 committed by John McLear
parent 86c0648ede
commit a3f062af96
2 changed files with 58 additions and 0 deletions

View File

@ -197,6 +197,17 @@ var helper = {};
return deferred;
};
/**
* Same as `waitFor` but using Promises
*
*/
helper.waitForPromise = async function(...args) {
// 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
// passing a no-op function to .fail().
return await this.waitFor(...args).fail(() => {});
};
helper.selectLines = function($startLine, $endLine, startOffset, endOffset){
// if no offset is provided, use beginning of start line and end of end line
startOffset = startOffset || 0;

View File

@ -215,6 +215,53 @@ describe("the test helper", function(){
});
});
describe('the waitForPromise method', function() {
it('takes a timeout and waits long enough', async function() {
this.timeout(2000);
var startTime = Date.now();
await helper.waitForPromise(function() {
return false;
}, 1500).catch(function() {
var duration = Date.now() - startTime;
expect(duration).to.be.greaterThan(1490);
});
});
it('takes an interval and checks on every interval', async function() {
this.timeout(4000);
var checks = 0;
await helper.waitForPromise(function() {
checks++;
return false;
}, 2000, 100).catch(function() {
expect(checks).to.be.greaterThan(15);
expect(checks).to.be.lessThan(21);
});
});
describe('returns a Promise', function() {
it('calls then after success', async function() {
let called = false;
await helper.waitForPromise(function() {
return true;
}).then(function() {
called = true;
});
expect(called).to.be(true);
});
it('calls catch on failure', async function() {
let called = false;
await helper.waitForPromise(function() {
return false;
},0).catch(function() {
called = true;
});
expect(called).to.be(true);
});
});
});
describe("the selectLines method", function(){
// function to support tests, use a single way to represent whitespaces
var cleanText = function(text){