Skip to content

Commit 6c25ffa

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

File tree

2 files changed

+63
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)