Skip to content

Commit f66172b

Browse files
mkslancnightwing
andauthored
Chore: Tests refactoring timeouts (ajaxorg#5929)
* add `sleep` utility function and replace `setTimeout` in async test cases * replace `setTimeout` with `lang.sleep` and update tests to use async/await * replace `setTimeout` with `lang.sleep` in tests and refactor to use async/await * resolve merge conflicts * refactor tests to use async/await and remove redundant `Promise` wrappers * undo formatting changes * fix regression in autocomplete_test.js * cleanup --------- Co-authored-by: nightwing <amirjanyan@gmail.com>
1 parent bb92012 commit f66172b

20 files changed

Lines changed: 1110 additions & 1295 deletions

src/ace_test.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (typeof process !== "undefined") {
77
var dom = require("./lib/dom");
88
var ace = require("./ace");
99
var assert = require("./test/assertions");
10+
var lang = require("./lib/lang");
1011

1112
module.exports = {
1213
"test: ace edit" : function() {
@@ -50,7 +51,7 @@ module.exports = {
5051
assert.equal("h", editor.getValue());
5152
document.body.removeChild(el);
5253
},
53-
"test: destroy": function(done) {
54+
"test: destroy": async function(done) {
5455
var editor = ace.edit();
5556
var mouseTarget = editor.renderer.getMouseEventTarget();
5657
var textarea = editor.textInput.getElement();
@@ -76,15 +77,14 @@ module.exports = {
7677
click(mouseTarget);
7778
assert.equal(focusCalled, 1);
7879

79-
setTimeout(function() {
80-
assert.notOk(!!editor.curOp);
81-
82-
// input commands on destroed editor without session do not throw errors
83-
editor.setSession(null);
84-
sendText(textarea, "2");
85-
86-
done();
87-
});
80+
await lang.sleep(0);
81+
assert.notOk(!!editor.curOp);
82+
83+
// input commands on destroed editor without session do not throw errors
84+
editor.setSession(null);
85+
sendText(textarea, "2");
86+
87+
done();
8888
},
8989
"test: useStrictCSP": function() {
9090
ace.config.set("useStrictCSP", undefined);
@@ -101,7 +101,7 @@ module.exports = {
101101
ace.config.set("useStrictCSP", false);
102102
assert.ok(getStyleNode());
103103
},
104-
"test: resizeObserver": function(done) {
104+
"test: resizeObserver": async function(done) {
105105
var mockObserver = {
106106
disconnect: function() { mockObserver.target = null; },
107107
observe: function(el) {
@@ -131,31 +131,31 @@ module.exports = {
131131
assert.equal(editor.renderer.$size.width, 100);
132132
editor.container.style.width = "200px";
133133
mockObserver.call();
134-
setTimeout(function() {
135-
if (editor.renderer.$resizeTimer.isPending())
136-
editor.renderer.$resizeTimer.call();
137-
assert.equal(editor.renderer.$size.width, 200);
138-
editor.container.style.height = "200px";
139-
mockObserver.call();
140-
setTimeout(function() {
141-
assert.ok(editor.renderer.$resizeTimer.isPending());
142-
editor.container.style.height = "100px";
143-
mockObserver.call();
144-
setTimeout(function() {
145-
assert.ok(!editor.renderer.$resizeTimer.isPending());
146-
editor.setOption("useResizeObserver", false);
147-
editor.container.style.height = "300px";
148-
mockObserver.call();
149-
assert.ok(!editor.renderer.$resizeObserver);
150-
editor.setOption("useResizeObserver", true);
151-
assert.ok(editor.renderer.$resizeObserver);
152-
if (window.ResizeObserver === mockObserver.$create)
153-
window.ResizeObserver = undefined;
154-
editor.destroy();
155-
done();
156-
}, 15);
157-
}, 15);
158-
}, 15);
134+
135+
await lang.sleep(15);
136+
if (editor.renderer.$resizeTimer.isPending())
137+
editor.renderer.$resizeTimer.call();
138+
assert.equal(editor.renderer.$size.width, 200);
139+
editor.container.style.height = "200px";
140+
mockObserver.call();
141+
142+
await lang.sleep(15);
143+
assert.ok(editor.renderer.$resizeTimer.isPending());
144+
editor.container.style.height = "100px";
145+
mockObserver.call();
146+
147+
await lang.sleep(15);
148+
assert.ok(!editor.renderer.$resizeTimer.isPending());
149+
editor.setOption("useResizeObserver", false);
150+
editor.container.style.height = "300px";
151+
mockObserver.call();
152+
assert.ok(!editor.renderer.$resizeObserver);
153+
editor.setOption("useResizeObserver", true);
154+
assert.ok(editor.renderer.$resizeObserver);
155+
if (window.ResizeObserver === mockObserver.$create)
156+
window.ResizeObserver = undefined;
157+
editor.destroy();
158+
done();
159159
},
160160
"test: edit template" : function() {
161161
var template = document.createElement("template");

src/autocomplete/inline_test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var AceInline = require("./inline").AceInline;
1010
var Editor = require("../ace").Editor;
1111
var EditSession = require("../ace").EditSession;
1212
var VirtualRenderer = require("../ace").VirtualRenderer;
13+
var lang = require("../lib/lang");
1314

1415
var editor;
1516
var editor2;
@@ -272,7 +273,7 @@ module.exports = {
272273

273274
done();
274275
},
275-
"test: should scroll if inline preview outside": function(done) {
276+
"test: should scroll if inline preview outside": async function(done) {
276277
// Fill the editor with new lines to get the cursor to the bottom
277278
// of the container
278279
editor.execCommand("insertstring", "\n".repeat(200));
@@ -289,25 +290,23 @@ module.exports = {
289290

290291
inline.show(editor, completions[6], "l");
291292
editor.renderer.$loop._flush();
292-
293-
setTimeout(() => {
294-
// Should scroll 5 lines to get the inline preview into view
295-
assert.strictEqual(deltaY, 50);
296293

297-
inline.hide();
298-
editor.renderer.$loop._flush();
294+
await lang.sleep(50);
295+
// Should scroll 5 lines to get the inline preview into view
296+
assert.strictEqual(deltaY, 50);
299297

300-
inline.show(editor, completions[7], "l");
301-
editor.renderer.$loop._flush();
298+
inline.hide();
299+
editor.renderer.$loop._flush();
300+
301+
inline.show(editor, completions[7], "l");
302+
editor.renderer.$loop._flush();
302303

303-
setTimeout(() => {
304-
// Should scroll as much as possbile while keeping the cursor on screen
305-
assert.strictEqual(row, 202);
306-
editor.renderer.scrollBy = initialScrollBy;
307-
editor.renderer.scrollToRow = initialScrollToRow;
308-
done();
309-
}, 50);
310-
}, 50);
304+
await lang.sleep(50);
305+
// Should scroll as much as possbile while keeping the cursor on screen
306+
assert.strictEqual(row, 202);
307+
editor.renderer.scrollBy = initialScrollBy;
308+
editor.renderer.scrollToRow = initialScrollToRow;
309+
done();
311310
},
312311
"test: renders multi-line ghost text with empty lines": function(done) {
313312
assert.equal(editor.renderer.$ghostTextWidget, null);

0 commit comments

Comments
 (0)