-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #517 from arkivanov/add-Single-asPromise-extension
Add Single.asPromise() extension
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
reaktive/src/jsMain/kotlin/com/badoo/reaktive/single/AsPromise.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.badoo.reaktive.single | ||
|
||
import kotlin.js.Promise | ||
|
||
fun <T> Single<T>.asPromise(): Promise<T> = | ||
Promise { resolve, reject -> | ||
subscribe(onSuccess = resolve, onError = reject) | ||
} |
30 changes: 30 additions & 0 deletions
30
reaktive/src/jsTest/kotlin/com/badoo/reaktive/single/AsPromiseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.badoo.reaktive.single | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertSame | ||
|
||
class AsPromiseTest { | ||
|
||
@Test | ||
fun resolves_non_null_value(): dynamic = | ||
singleOf(1) | ||
.asPromise() | ||
.then { assertEquals(1, it) } | ||
|
||
@Test | ||
fun resolves_null_value(): dynamic = | ||
singleOf<Int?>(null) | ||
.asPromise() | ||
.then { assertEquals(null, it) } | ||
|
||
@Test | ||
fun rejects(): dynamic { | ||
val error = Exception() | ||
|
||
return singleOfError<Nothing>(error) | ||
.asPromise() | ||
.catch { it } | ||
.then { assertSame(error, it) } | ||
} | ||
} |