forked from pointfreeco/swift-composable-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchTests.swift
187 lines (165 loc) · 5.22 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
import Combine
import ComposableArchitecture
import XCTest
@testable import Search
class SearchTests: XCTestCase {
let scheduler = DispatchQueue.test
func testSearchAndClearQuery() {
let store = TestStore(
initialState: .init(),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: .failing,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.environment.weatherClient.searchLocation = { _ in Effect(value: mockLocations) }
store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
self.scheduler.advance(by: 0.3)
store.receive(.locationsResponse(.success(mockLocations))) {
$0.locations = mockLocations
}
store.send(.searchQueryChanged("")) {
$0.locations = []
$0.searchQuery = ""
}
}
func testSearchFailure() {
let store = TestStore(
initialState: .init(),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: .failing,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.environment.weatherClient.searchLocation = { _ in Effect(error: .init()) }
store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
self.scheduler.advance(by: 0.3)
store.receive(.locationsResponse(.failure(.init())))
}
func testClearQueryCancelsInFlightSearchRequest() {
var weatherClient = WeatherClient.failing
weatherClient.searchLocation = { _ in Effect(value: mockLocations) }
let store = TestStore(
initialState: .init(),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: weatherClient,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.send(.searchQueryChanged("S")) {
$0.searchQuery = "S"
}
self.scheduler.advance(by: 0.2)
store.send(.searchQueryChanged("")) {
$0.searchQuery = ""
}
self.scheduler.run()
}
func testTapOnLocation() {
let specialLocation = Location(id: 42, title: "Special Place")
let specialLocationWeather = LocationWeather(
consolidatedWeather: mockWeather,
id: 42
)
var weatherClient = WeatherClient.failing
weatherClient.weather = { _ in Effect(value: specialLocationWeather) }
let store = TestStore(
initialState: .init(locations: mockLocations + [specialLocation]),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: weatherClient,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.send(.locationTapped(specialLocation)) {
$0.locationWeatherRequestInFlight = specialLocation
}
self.scheduler.advance()
store.receive(.locationWeatherResponse(.success(specialLocationWeather))) {
$0.locationWeatherRequestInFlight = nil
$0.locationWeather = specialLocationWeather
}
}
func testTapOnLocationCancelsInFlightRequest() {
let specialLocation = Location(id: 42, title: "Special Place")
let specialLocationWeather = LocationWeather(
consolidatedWeather: mockWeather,
id: 42
)
var weatherClient = WeatherClient.failing
weatherClient.weather = { _ in Effect(value: specialLocationWeather) }
let store = TestStore(
initialState: .init(locations: mockLocations + [specialLocation]),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: weatherClient,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.send(.locationTapped(mockLocations.first!)) {
$0.locationWeatherRequestInFlight = mockLocations.first!
}
store.send(.locationTapped(specialLocation)) {
$0.locationWeatherRequestInFlight = specialLocation
}
self.scheduler.advance()
store.receive(.locationWeatherResponse(.success(specialLocationWeather))) {
$0.locationWeatherRequestInFlight = nil
$0.locationWeather = specialLocationWeather
}
}
func testTapOnLocationFailure() {
var weatherClient = WeatherClient.failing
weatherClient.weather = { _ in Effect(error: .init()) }
let store = TestStore(
initialState: .init(locations: mockLocations),
reducer: searchReducer,
environment: SearchEnvironment(
weatherClient: weatherClient,
mainQueue: self.scheduler.eraseToAnyScheduler()
)
)
store.send(.locationTapped(mockLocations.first!)) {
$0.locationWeatherRequestInFlight = mockLocations.first!
}
self.scheduler.advance()
store.receive(.locationWeatherResponse(.failure(.init()))) {
$0.locationWeatherRequestInFlight = nil
}
}
}
private let mockWeather: [LocationWeather.ConsolidatedWeather] = [
.init(
applicableDate: Date(timeIntervalSince1970: 0),
maxTemp: 90,
minTemp: 70,
theTemp: 80,
weatherStateName: "Clear"
),
.init(
applicableDate: Date(timeIntervalSince1970: 86_400),
maxTemp: 70,
minTemp: 50,
theTemp: 60,
weatherStateName: "Rain"
),
.init(
applicableDate: Date(timeIntervalSince1970: 172_800),
maxTemp: 100,
minTemp: 80,
theTemp: 90,
weatherStateName: "Cloudy"
),
]
private let mockLocations = [
Location(id: 1, title: "Brooklyn"),
Location(id: 2, title: "Los Angeles"),
Location(id: 3, title: "San Francisco"),
]