Skip to content

Commit f9ecac0

Browse files
Added AsyncTestSpec.executeAfterDelay
1 parent cb7e203 commit f9ecac0

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

test-dom/src/main/scala/scommons/react/test/dom/AsyncTestSpec.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,18 @@ trait AsyncTestSpec extends AsyncFlatSpec
7878

7979
promise.future
8080
}
81+
82+
def executeAfterDelay(millis: Int)(block: => Assertion): Future[Assertion] = {
83+
val promise = Promise[Assertion]()
84+
85+
dom.window.setTimeout({ () =>
86+
try {
87+
promise.success(block)
88+
} catch {
89+
case NonFatal(ex) => promise.failure(ex)
90+
}
91+
}, millis.toDouble)
92+
93+
promise.future
94+
}
8195
}

test-dom/src/test/scala/scommons/react/test/dom/AsyncTestSpecTest.scala

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,40 @@ class AsyncTestSpecTest extends AsyncTestSpec {
2626
//given
2727
val value = 5
2828

29+
//when
30+
val resultF = eventually {
31+
value shouldBe 1
32+
}
33+
34+
//then
35+
resultF.failed.map { result =>
36+
result.getMessage should include ("5 was not equal to 1")
37+
}
38+
}
39+
40+
it should "run successfully after delay when executeAfterDelay" in {
41+
//given
42+
val delay = 500
43+
val value = 5
44+
2945
//when & then
30-
eventually {
46+
executeAfterDelay(delay) {
47+
value shouldBe 5
48+
}
49+
}
50+
51+
it should "fail if not succeeded after delay when executeAfterDelay" in {
52+
//given
53+
val delay = 500
54+
val value = 5
55+
56+
//when
57+
val resultF = executeAfterDelay(delay) {
3158
value shouldBe 1
32-
}.failed.map { result =>
59+
}
60+
61+
//then
62+
resultF.failed.map { result =>
3363
result.getMessage should include ("5 was not equal to 1")
3464
}
3565
}

0 commit comments

Comments
 (0)