Skip to content

Commit

Permalink
Added tests for _waitForElementByTimeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
kiselev committed Aug 8, 2022
1 parent 33e560b commit d5eaf0d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/util/waitForElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function _waitForElement(elSelector, callback) {
});
} else {
// Old browsers will wait by timeout
waitForElementByTimeout(elSelector, callback, 1000, 10000);
_waitForElementByTimeout(elSelector, callback, 1000, 10000);
}
}

Expand All @@ -38,7 +38,7 @@ export default function _waitForElement(elSelector, callback) {
* @param {number} checkInterval In milliseconds
* @param {number} maxTimeout In milliseconds
*/
function waitForElementByTimeout(
export function _waitForElementByTimeout(
elSelector,
callback,
checkInterval,
Expand Down
59 changes: 59 additions & 0 deletions tests/util/waitForElement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { _waitForElementByTimeout } from "../../src/util/waitForElement";
import { appendDummyElement } from "../helper";

describe("Testing _waitForElementByTimeout", () => {
const interval = 100;
const maxTimeout = 3000;

test("Callback call even if element doesn't appear after timeout", (done) => {
const callback = jest.fn();
_waitForElementByTimeout("#not_existed", callback, interval, maxTimeout);
expect(callback).toBeCalledTimes(0);
setTimeout(function () {
expect(callback).toBeCalledTimes(1);
done();
}, maxTimeout + interval);
});

test("Callback should be called immediately if elements already exists", () => {
const callback = jest.fn();
const id = "prev_created";
const el = appendDummyElement();
el.setAttribute("id", id);
_waitForElementByTimeout("#" + id, callback, interval, maxTimeout);
expect(callback).toBeCalledTimes(1);
});

test("Callback must be called after the element appears", (done) => {
const callback = jest.fn();
const id = "later_created";
_waitForElementByTimeout("#" + id, callback, interval, maxTimeout);
expect(callback).toBeCalledTimes(0);
const el = appendDummyElement();
el.setAttribute("id", id);
setTimeout(function () {
expect(callback).toBeCalledTimes(1);
done();
}, interval);
});

test("Check interval is bigger than maximum timeout", (done) => {
_waitForElementByTimeout("#not_existed", done, 1000, 100);
});

test("Check interval is equal to maximum timeout", (done) => {
_waitForElementByTimeout("#not_existed", done, 1000, 1000);
});

test("Check interval is zero", (done) => {
_waitForElementByTimeout("#not_existed", done, 0, maxTimeout);
});

test("Maximum timeout is zero", (done) => {
_waitForElementByTimeout("#not_existed", done, interval, 0);
});

test("Maximum timeout and interval are zero", (done) => {
_waitForElementByTimeout("#not_existed", done, 0, 0);
});
});

0 comments on commit d5eaf0d

Please sign in to comment.