forked from pointfreeco/swift-composable-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTests.swift
189 lines (169 loc) · 5.21 KB
/
SearchTests.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import ComposableArchitecture
import XCTest
@testable import Search
@MainActor
final class SearchTests: XCTestCase {
func testSearchAndClearQuery() async {
let store = TestStore(initialState: Search.State()) {
Search()
} withDependencies: {
$0.weatherClient.search = { _ in .mock }
}
await store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
await store.send(.searchQueryChangeDebounced)
await store.receive(\.searchResponse.success) {
$0.results = GeocodingSearch.mock.results
}
await store.send(.searchQueryChanged("")) {
$0.results = []
$0.searchQuery = ""
}
}
func testSearchFailure() async {
let store = TestStore(initialState: Search.State()) {
Search()
} withDependencies: {
$0.weatherClient.search = { _ in throw SomethingWentWrong() }
}
await store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
await store.send(.searchQueryChangeDebounced)
await store.receive(\.searchResponse.failure)
}
func testClearQueryCancelsInFlightSearchRequest() async {
let store = TestStore(initialState: Search.State()) {
Search()
} withDependencies: {
$0.weatherClient.search = { _ in .mock }
}
let searchQueryChanged = await store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
await searchQueryChanged.cancel()
await store.send(.searchQueryChanged("")) {
$0.searchQuery = ""
}
}
func testTapOnLocation() async {
let specialResult = GeocodingSearch.Result(
country: "Special Country",
latitude: 0,
longitude: 0,
id: 42,
name: "Special Place"
)
var results = GeocodingSearch.mock.results
results.append(specialResult)
let store = TestStore(initialState: Search.State(results: results)) {
Search()
} withDependencies: {
$0.weatherClient.forecast = { _ in .mock }
}
await store.send(.searchResultTapped(specialResult)) {
$0.resultForecastRequestInFlight = specialResult
}
await store.receive(\.forecastResponse) {
$0.resultForecastRequestInFlight = nil
$0.weather = Search.State.Weather(
id: 42,
days: [
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 0),
temperatureMax: 90,
temperatureMaxUnit: "°F",
temperatureMin: 70,
temperatureMinUnit: "°F"
),
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 86_400),
temperatureMax: 70,
temperatureMaxUnit: "°F",
temperatureMin: 50,
temperatureMinUnit: "°F"
),
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 172_800),
temperatureMax: 100,
temperatureMaxUnit: "°F",
temperatureMin: 80,
temperatureMinUnit: "°F"
),
]
)
}
}
func testTapOnLocationCancelsInFlightRequest() async {
let specialResult = GeocodingSearch.Result(
country: "Special Country",
latitude: 0,
longitude: 0,
id: 42,
name: "Special Place"
)
var results = GeocodingSearch.mock.results
results.append(specialResult)
let clock = TestClock()
let store = TestStore(initialState: Search.State(results: results)) {
Search()
} withDependencies: {
$0.weatherClient.forecast = { _ in
try await clock.sleep(for: .seconds(0))
return .mock
}
}
await store.send(.searchResultTapped(results.first!)) {
$0.resultForecastRequestInFlight = results.first!
}
await store.send(.searchResultTapped(specialResult)) {
$0.resultForecastRequestInFlight = specialResult
}
await clock.advance()
await store.receive(\.forecastResponse) {
$0.resultForecastRequestInFlight = nil
$0.weather = Search.State.Weather(
id: 42,
days: [
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 0),
temperatureMax: 90,
temperatureMaxUnit: "°F",
temperatureMin: 70,
temperatureMinUnit: "°F"
),
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 86_400),
temperatureMax: 70,
temperatureMaxUnit: "°F",
temperatureMin: 50,
temperatureMinUnit: "°F"
),
Search.State.Weather.Day(
date: Date(timeIntervalSince1970: 172_800),
temperatureMax: 100,
temperatureMaxUnit: "°F",
temperatureMin: 80,
temperatureMinUnit: "°F"
),
]
)
}
}
func testTapOnLocationFailure() async {
let results = GeocodingSearch.mock.results
let store = TestStore(initialState: Search.State(results: results)) {
Search()
} withDependencies: {
$0.weatherClient.forecast = { _ in throw SomethingWentWrong() }
}
await store.send(.searchResultTapped(results.first!)) {
$0.resultForecastRequestInFlight = results.first!
}
await store.receive(\.forecastResponse) {
$0.resultForecastRequestInFlight = nil
}
}
}
private struct SomethingWentWrong: Equatable, Error {}