-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSingleTests.swift
62 lines (56 loc) · 2.24 KB
/
SingleTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// SingleTests.swift
// KMPNativeCoroutinesRxSwiftTests
//
// Created by Rick Clephas on 05/07/2021.
//
import XCTest
import KMPNativeCoroutinesCore
import KMPNativeCoroutinesRxSwift
class SingleTests: XCTestCase {
private class TestValue { }
func testDisposableInvoked() {
var cancelCount = 0
let nativeSuspend: NativeSuspend<TestValue, NSError, Void> = { _, _, _ in
return { cancelCount += 1 }
}
let disposable = createSingle(for: nativeSuspend).subscribe()
XCTAssertEqual(cancelCount, 0, "Disposable shouldn't be invoked yet")
disposable.dispose()
XCTAssertEqual(cancelCount, 1, "Disposable should be invoked once")
}
func testCompletionWithValue() {
let value = TestValue()
let nativeSuspend: NativeSuspend<TestValue, NSError, Void> = { resultCallback, _, _ in
resultCallback(value, ())
return { }
}
var successCount = 0
let disposable = createSingle(for: nativeSuspend)
.subscribe(onSuccess: { receivedValue in
XCTAssertIdentical(receivedValue, value, "Received incorrect value")
successCount += 1
}, onFailure: { _ in
XCTFail("Single should complete without error")
})
_ = disposable // This is just to remove the unused variable warning
XCTAssertEqual(successCount, 1, "Success closure should be called once")
}
func testCompletionWithError() {
let error = NSError(domain: "Test", code: 0)
let nativeSuspend: NativeSuspend<TestValue, NSError, Void> = { _, errorCallback, _ in
errorCallback(error, ())
return { }
}
var failureCount = 0
let disposable = createSingle(for: nativeSuspend)
.subscribe(onSuccess: { _ in
XCTFail("Single should complete with an error")
}, onFailure: { receivedError in
XCTAssertIdentical(receivedError as NSError, error, "Received incorrect error")
failureCount += 1
})
_ = disposable // This is just to remove the unused variable warning
XCTAssertEqual(failureCount, 1, "Failure closure should be called once")
}
}