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