Skip to content

Commit 9db181e

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

File tree

2 files changed

+64
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)