From a3f062af96b05680de2f8090f87a7a07bc1a6d29 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Fri, 9 Oct 2020 20:50:47 +0200 Subject: [PATCH] tests: add waitForPromise method and test for it --- tests/frontend/helper.js | 11 ++++++++ tests/frontend/specs/helper.js | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/tests/frontend/helper.js b/tests/frontend/helper.js index 894b402f..32b3c6bc 100644 --- a/tests/frontend/helper.js +++ b/tests/frontend/helper.js @@ -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; diff --git a/tests/frontend/specs/helper.js b/tests/frontend/specs/helper.js index 14c7944b..fb8ee3ca 100644 --- a/tests/frontend/specs/helper.js +++ b/tests/frontend/specs/helper.js @@ -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){