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