Skip to content

Commit d5eaf0d

Browse files
author
kiselev
committed
Added tests for _waitForElementByTimeout function
1 parent 33e560b commit d5eaf0d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/util/waitForElement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default function _waitForElement(elSelector, callback) {
2828
});
2929
} else {
3030
// Old browsers will wait by timeout
31-
waitForElementByTimeout(elSelector, callback, 1000, 10000);
31+
_waitForElementByTimeout(elSelector, callback, 1000, 10000);
3232
}
3333
}
3434

@@ -38,7 +38,7 @@ export default function _waitForElement(elSelector, callback) {
3838
* @param {number} checkInterval In milliseconds
3939
* @param {number} maxTimeout In milliseconds
4040
*/
41-
function waitForElementByTimeout(
41+
export function _waitForElementByTimeout(
4242
elSelector,
4343
callback,
4444
checkInterval,

tests/util/waitForElement.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { _waitForElementByTimeout } from "../../src/util/waitForElement";
2+
import { appendDummyElement } from "../helper";
3+
4+
describe("Testing _waitForElementByTimeout", () => {
5+
const interval = 100;
6+
const maxTimeout = 3000;
7+
8+
test("Callback call even if element doesn't appear after timeout", (done) => {
9+
const callback = jest.fn();
10+
_waitForElementByTimeout("#not_existed", callback, interval, maxTimeout);
11+
expect(callback).toBeCalledTimes(0);
12+
setTimeout(function () {
13+
expect(callback).toBeCalledTimes(1);
14+
done();
15+
}, maxTimeout + interval);
16+
});
17+
18+
test("Callback should be called immediately if elements already exists", () => {
19+
const callback = jest.fn();
20+
const id = "prev_created";
21+
const el = appendDummyElement();
22+
el.setAttribute("id", id);
23+
_waitForElementByTimeout("#" + id, callback, interval, maxTimeout);
24+
expect(callback).toBeCalledTimes(1);
25+
});
26+
27+
test("Callback must be called after the element appears", (done) => {
28+
const callback = jest.fn();
29+
const id = "later_created";
30+
_waitForElementByTimeout("#" + id, callback, interval, maxTimeout);
31+
expect(callback).toBeCalledTimes(0);
32+
const el = appendDummyElement();
33+
el.setAttribute("id", id);
34+
setTimeout(function () {
35+
expect(callback).toBeCalledTimes(1);
36+
done();
37+
}, interval);
38+
});
39+
40+
test("Check interval is bigger than maximum timeout", (done) => {
41+
_waitForElementByTimeout("#not_existed", done, 1000, 100);
42+
});
43+
44+
test("Check interval is equal to maximum timeout", (done) => {
45+
_waitForElementByTimeout("#not_existed", done, 1000, 1000);
46+
});
47+
48+
test("Check interval is zero", (done) => {
49+
_waitForElementByTimeout("#not_existed", done, 0, maxTimeout);
50+
});
51+
52+
test("Maximum timeout is zero", (done) => {
53+
_waitForElementByTimeout("#not_existed", done, interval, 0);
54+
});
55+
56+
test("Maximum timeout and interval are zero", (done) => {
57+
_waitForElementByTimeout("#not_existed", done, 0, 0);
58+
});
59+
});

0 commit comments

Comments
 (0)