Skip to content

Commit 2829061

Browse files
committed
Clean up fact client tests (#1172)
* Clean up fact client tests * wip
1 parent 744c639 commit 2829061

File tree

7 files changed

+61
-47
lines changed

7 files changed

+61
-47
lines changed

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Basics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ enum EffectsBasicsAction: Equatable {
3232
case decrementButtonTapped
3333
case incrementButtonTapped
3434
case numberFactButtonTapped
35-
case numberFactResponse(Result<String, FactClient.Error>)
35+
case numberFactResponse(Result<String, FactClient.Failure>)
3636
}
3737

3838
struct EffectsBasicsEnvironment {

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Cancellation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum EffectsCancellationAction: Equatable {
2626
case cancelButtonTapped
2727
case stepperChanged(Int)
2828
case triviaButtonTapped
29-
case triviaResponse(Result<String, FactClient.Error>)
29+
case triviaResponse(Result<String, FactClient.Failure>)
3030
}
3131

3232
struct EffectsCancellationEnvironment {

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Refreshable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct RefreshableState: Equatable {
2121
enum RefreshableAction: Equatable {
2222
case cancelButtonTapped
2323
case decrementButtonTapped
24-
case factResponse(Result<String, FactClient.Error>)
24+
case factResponse(Result<String, FactClient.Failure>)
2525
case incrementButtonTapped
2626
case refresh
2727
}

Examples/CaseStudies/SwiftUICaseStudies/FactClient.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ import ReactiveSwift
44
import XCTestDynamicOverlay
55

66
struct FactClient {
7-
var fetch: (Int) -> Effect<String, Error>
7+
var fetch: (Int) -> Effect<String, Failure>
88

9-
struct Error: Swift.Error, Equatable {}
9+
struct Failure: Error, Equatable {}
1010
}
1111

1212
// This is the "live" fact dependency that reaches into the outside world to fetch trivia.
1313
// Typically this live implementation of the dependency would live in its own module so that the
1414
// main feature doesn't need to compile it.
1515
extension FactClient {
16-
static let live = Self(
17-
fetch: { number in
18-
Effect.task {
16+
static let live = Self(
17+
fetch: { number in
18+
Effect.task {
1919
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
20-
let (data, _) = try await URLSession.shared
21-
.data(from: URL(string: "http://numbersapi.com/\(number)/trivia")!)
22-
return String(decoding: data, as: UTF8.self)
23-
}
24-
.mapError { _ in Error() }
20+
let (data, _) = try await URLSession.shared
21+
.data(from: URL(string: "http://numbersapi.com/\(number)/trivia")!)
22+
return String(decoding: data, as: UTF8.self)
2523
}
26-
)
24+
.mapError { _ in Failure() }
25+
}
26+
)
2727
}
2828

2929
#if DEBUG
3030
extension FactClient {
3131
// This is the "unimplemented" fact dependency that is useful to plug into tests that you want
3232
// to prove do not need the dependency.
3333
static let unimplemented = Self(
34-
fetch: { _ in .unimplemented("\(Self.self).fact") }
34+
fetch: { _ in .unimplemented("\(Self.self).fetch") }
3535
)
3636
}
3737
#endif

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-BasicsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class EffectsBasicsTests: XCTestCase {
4949
environment: .unimplemented
5050
)
5151

52-
store.environment.fact.fetch = { _ in Effect(error: FactClient.Error()) }
52+
store.environment.fact.fetch = { _ in Effect(error: FactClient.Failure()) }
5353
store.environment.mainQueue = ImmediateScheduler()
5454

5555
store.send(.incrementButtonTapped) {
@@ -58,7 +58,7 @@ class EffectsBasicsTests: XCTestCase {
5858
store.send(.numberFactButtonTapped) {
5959
$0.isNumberFactRequestInFlight = true
6060
}
61-
store.receive(.numberFactResponse(.failure(FactClient.Error()))) {
61+
store.receive(.numberFactResponse(.failure(FactClient.Failure()))) {
6262
$0.isNumberFactRequestInFlight = false
6363
}
6464
}

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-CancellationTests.swift

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class EffectsCancellationTests: XCTestCase {
99
let store = TestStore(
1010
initialState: EffectsCancellationState(),
1111
reducer: effectsCancellationReducer,
12-
environment: EffectsCancellationEnvironment(
13-
fact: FactClient(fetch: { n in Effect(value: "\(n) is a good number Brent") }),
14-
mainQueue: ImmediateScheduler()
12+
environment: .unimplemented
1513
)
16-
)
14+
15+
store.environment.fact.fetch = { Effect(value: "\($0) is a good number Brent") }
16+
store.environment.mainQueue = ImmediateScheduler()
1717

1818
store.send(.stepperChanged(1)) {
1919
$0.count = 1
@@ -34,16 +34,16 @@ class EffectsCancellationTests: XCTestCase {
3434
let store = TestStore(
3535
initialState: EffectsCancellationState(),
3636
reducer: effectsCancellationReducer,
37-
environment: .init(
38-
fact: .init(fetch: { _ in Effect(error: FactClient.Error()) }),
39-
mainQueue: ImmediateScheduler()
37+
environment: .unimplemented
4038
)
41-
)
39+
40+
store.environment.fact.fetch = { _ in Effect(error: FactClient.Failure()) }
41+
store.environment.mainQueue = ImmediateScheduler()
4242

4343
store.send(.triviaButtonTapped) {
4444
$0.isTriviaRequestInFlight = true
4545
}
46-
store.receive(.triviaResponse(.failure(FactClient.Error()))) {
46+
store.receive(.triviaResponse(.failure(FactClient.Failure()))) {
4747
$0.isTriviaRequestInFlight = false
4848
}
4949
}
@@ -59,11 +59,11 @@ class EffectsCancellationTests: XCTestCase {
5959
let store = TestStore(
6060
initialState: EffectsCancellationState(),
6161
reducer: effectsCancellationReducer,
62-
environment: EffectsCancellationEnvironment(
63-
fact: FactClient(fetch: { n in Effect(value: "\(n) is a good number Brent") }),
64-
mainQueue: mainQueue
62+
environment: .unimplemented
6563
)
66-
)
64+
65+
store.environment.fact.fetch = { Effect(value: "\($0) is a good number Brent") }
66+
store.environment.mainQueue = mainQueue
6767

6868
store.send(.triviaButtonTapped) {
6969
$0.isTriviaRequestInFlight = true
@@ -79,11 +79,11 @@ class EffectsCancellationTests: XCTestCase {
7979
let store = TestStore(
8080
initialState: EffectsCancellationState(),
8181
reducer: effectsCancellationReducer,
82-
environment: EffectsCancellationEnvironment(
83-
fact: FactClient(fetch: { n in Effect(value: "\(n) is a good number Brent") }),
84-
mainQueue: mainQueue
82+
environment: .unimplemented
8583
)
86-
)
84+
85+
store.environment.fact.fetch = { Effect(value: "\($0) is a good number Brent") }
86+
store.environment.mainQueue = mainQueue
8787

8888
store.send(.triviaButtonTapped) {
8989
$0.isTriviaRequestInFlight = true
@@ -95,3 +95,10 @@ class EffectsCancellationTests: XCTestCase {
9595
mainQueue.advance()
9696
}
9797
}
98+
99+
extension EffectsCancellationEnvironment {
100+
static let unimplemented = Self(
101+
fact: .unimplemented,
102+
mainQueue: UnimplementedScheduler()
103+
)
104+
}

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-RefreshableTests.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ class RefreshableTests: XCTestCase {
99
let store = TestStore(
1010
initialState: RefreshableState(),
1111
reducer: refreshableReducer,
12-
environment: RefreshableEnvironment(
13-
fact: FactClient(fetch: { Effect(value: "\($0) is a good number.") }),
14-
mainQueue: ImmediateScheduler()
15-
)
12+
environment: .unimplemented
1613
)
1714

15+
store.environment.fact.fetch = { Effect(value: "\($0) is a good number.") }
16+
store.environment.mainQueue = ImmediateScheduler()
17+
1818
store.send(.incrementButtonTapped) {
1919
$0.count = 1
2020
}
@@ -31,19 +31,19 @@ class RefreshableTests: XCTestCase {
3131
let store = TestStore(
3232
initialState: RefreshableState(),
3333
reducer: refreshableReducer,
34-
environment: RefreshableEnvironment(
35-
fact: FactClient(fetch: { _ in Effect(error: FactClient.Error()) }),
36-
mainQueue: ImmediateScheduler()
37-
)
34+
environment: .unimplemented
3835
)
3936

37+
store.environment.fact.fetch = { _ in Effect(error: FactClient.Failure()) }
38+
store.environment.mainQueue = ImmediateScheduler()
39+
4040
store.send(.incrementButtonTapped) {
4141
$0.count = 1
4242
}
4343
store.send(.refresh) {
4444
$0.isLoading = true
4545
}
46-
store.receive(.factResponse(.failure(FactClient.Error()))) {
46+
store.receive(.factResponse(.failure(FactClient.Failure()))) {
4747
$0.isLoading = false
4848
}
4949
}
@@ -54,12 +54,12 @@ class RefreshableTests: XCTestCase {
5454
let store = TestStore(
5555
initialState: RefreshableState(),
5656
reducer: refreshableReducer,
57-
environment: RefreshableEnvironment(
58-
fact: FactClient(fetch: { Effect(value: "\($0) is a good number.") }),
59-
mainQueue: mainQueue
60-
)
57+
environment: .unimplemented
6158
)
6259

60+
store.environment.fact.fetch = { Effect(value: "\($0) is a good number.") }
61+
store.environment.mainQueue = mainQueue
62+
6363
store.send(.incrementButtonTapped) {
6464
$0.count = 1
6565
}
@@ -71,3 +71,10 @@ class RefreshableTests: XCTestCase {
7171
}
7272
}
7373
}
74+
75+
extension RefreshableEnvironment {
76+
static let unimplemented = Self(
77+
fact: .unimplemented,
78+
mainQueue: UnimplementedScheduler()
79+
)
80+
}

0 commit comments

Comments
 (0)