@@ -14,27 +14,26 @@ class VoiceMemosTests: XCTestCase {
14
14
// NB: Combine's concatenation behavior is different in 13.3
15
15
guard #available( iOS 13 . 4 , * ) else { return }
16
16
17
+ let store = TestStore (
18
+ initialState: VoiceMemosState ( ) ,
19
+ reducer: voiceMemosReducer,
20
+ environment: . unimplemented
21
+ )
22
+
17
23
let didFinish = AsyncThrowingStream < Bool , Error > . streamWithContinuation ( )
18
24
19
- var environment = VoiceMemosEnvironment . unimplemented
20
- environment. audioRecorder. currentTime = { 2.5 }
21
- environment. audioRecorder. requestRecordPermission = { true }
22
- environment. audioRecorder. startRecording = { _ in
25
+ store. environment. audioRecorder. currentTime = { 2.5 }
26
+ store. environment. audioRecorder. requestRecordPermission = { true }
27
+ store. environment. audioRecorder. startRecording = { _ in
23
28
try await didFinish. stream. first { _ in true } !
24
29
}
25
- environment. audioRecorder. stopRecording = {
30
+ store . environment. audioRecorder. stopRecording = {
26
31
didFinish. continuation. yield ( true )
27
32
didFinish. continuation. finish ( )
28
33
}
29
- environment. mainRunLoop = mainRunLoop
30
- environment. temporaryDirectory = { URL ( fileURLWithPath: " /tmp " ) }
31
- environment. uuid = { UUID ( uuidString: " DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF " ) ! }
32
-
33
- let store = TestStore (
34
- initialState: VoiceMemosState ( ) ,
35
- reducer: voiceMemosReducer,
36
- environment: environment
37
- )
34
+ store. environment. mainRunLoop = self . mainRunLoop
35
+ store. environment. temporaryDirectory = { URL ( fileURLWithPath: " /tmp " ) }
36
+ store. environment. uuid = { UUID ( uuidString: " DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF " ) ! }
38
37
39
38
await store. send ( . recordButtonTapped)
40
39
await self . mainRunLoop. advance ( )
@@ -82,17 +81,16 @@ class VoiceMemosTests: XCTestCase {
82
81
func testPermissionDenied( ) async {
83
82
let didOpenSettings = ActorIsolated ( false )
84
83
85
- var environment = VoiceMemosEnvironment . unimplemented
86
- environment. audioRecorder. requestRecordPermission = { false }
87
- environment. mainRunLoop = mainRunLoop
88
- environment. openSettings = { await didOpenSettings. setValue ( true ) }
89
-
90
84
let store = TestStore (
91
85
initialState: VoiceMemosState ( ) ,
92
86
reducer: voiceMemosReducer,
93
- environment: environment
87
+ environment: . unimplemented
94
88
)
95
89
90
+ store. environment. audioRecorder. requestRecordPermission = { false }
91
+ store. environment. mainRunLoop = ImmediateScheduler ( )
92
+ store. environment. openSettings = { await didOpenSettings. setValue ( true ) }
93
+
96
94
await store. send ( . recordButtonTapped)
97
95
await store. receive ( . recordPermissionResponse( false ) ) {
98
96
$0. alert = AlertState ( title: TextState ( " Permission is required to record voice memos. " ) )
@@ -107,24 +105,23 @@ class VoiceMemosTests: XCTestCase {
107
105
108
106
func testRecordMemoFailure( ) async {
109
107
struct SomeError : Error , Equatable { }
110
- let didFinish = AsyncThrowingStream < Bool , Error > . streamWithContinuation ( )
111
-
112
- var environment = VoiceMemosEnvironment . unimplemented
113
- environment. audioRecorder. currentTime = { 2.5 }
114
- environment. audioRecorder. requestRecordPermission = { true }
115
- environment. audioRecorder. startRecording = { _ in
116
- try await didFinish. stream. first { _ in true } !
117
- }
118
- environment. mainRunLoop = TestScheduler ( startDate: Date ( timeIntervalSince1970: 0 ) )
119
- environment. temporaryDirectory = { URL ( fileURLWithPath: " /tmp " ) }
120
- environment. uuid = { UUID ( uuidString: " DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF " ) ! }
121
108
122
109
let store = TestStore (
123
110
initialState: VoiceMemosState ( ) ,
124
111
reducer: voiceMemosReducer,
125
- environment: environment
112
+ environment: . unimplemented
126
113
)
127
114
115
+ let didFinish = AsyncThrowingStream < Bool , Error > . streamWithContinuation ( )
116
+
117
+ store. environment. audioRecorder. requestRecordPermission = { true }
118
+ store. environment. audioRecorder. startRecording = { _ in
119
+ try await didFinish. stream. first { _ in true } !
120
+ }
121
+ store. environment. mainRunLoop = TestScheduler ( startDate: Date ( timeIntervalSince1970: 0 ) )
122
+ store. environment. temporaryDirectory = { URL ( fileURLWithPath: " /tmp " ) }
123
+ store. environment. uuid = { UUID ( uuidString: " DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF " ) ! }
124
+
128
125
await store. send ( . recordButtonTapped)
129
126
await self . mainRunLoop. advance ( by: 0.5 )
130
127
await store. receive ( . recordPermissionResponse( true ) ) {
@@ -149,13 +146,6 @@ class VoiceMemosTests: XCTestCase {
149
146
}
150
147
151
148
func testPlayMemoHappyPath( ) async {
152
- var environment = VoiceMemosEnvironment . unimplemented
153
- environment. audioPlayer. play = { _ in
154
- try await self . mainRunLoop. sleep ( for: . milliseconds( 1250 ) )
155
- return true
156
- }
157
- environment. mainRunLoop = mainRunLoop
158
-
159
149
let url = URL ( fileURLWithPath: " pointfreeco/functions.m4a " )
160
150
let store = TestStore (
161
151
initialState: VoiceMemosState (
@@ -170,9 +160,15 @@ class VoiceMemosTests: XCTestCase {
170
160
]
171
161
) ,
172
162
reducer: voiceMemosReducer,
173
- environment: environment
163
+ environment: . unimplemented
174
164
)
175
165
166
+ store. environment. audioPlayer. play = { _ in
167
+ try await self . mainRunLoop. sleep ( for: . milliseconds( 1250 ) )
168
+ return true
169
+ }
170
+ store. environment. mainRunLoop = self . mainRunLoop
171
+
176
172
let task = await store. send ( . voiceMemo( id: url, action: . playButtonTapped) ) {
177
173
$0. voiceMemos [ id: url] ? . mode = . playing( progress: 0 )
178
174
}
@@ -194,10 +190,6 @@ class VoiceMemosTests: XCTestCase {
194
190
func testPlayMemoFailure( ) async {
195
191
struct SomeError : Error , Equatable { }
196
192
197
- var environment = VoiceMemosEnvironment . unimplemented
198
- environment. audioPlayer. play = { _ in throw SomeError ( ) }
199
- environment. mainRunLoop = mainRunLoop
200
-
201
193
let url = URL ( fileURLWithPath: " pointfreeco/functions.m4a " )
202
194
let store = TestStore (
203
195
initialState: VoiceMemosState (
@@ -212,9 +204,12 @@ class VoiceMemosTests: XCTestCase {
212
204
]
213
205
) ,
214
206
reducer: voiceMemosReducer,
215
- environment: environment
207
+ environment: . unimplemented
216
208
)
217
209
210
+ store. environment. audioPlayer. play = { _ in throw SomeError ( ) }
211
+ store. environment. mainRunLoop = self . mainRunLoop
212
+
218
213
let task = await store. send ( . voiceMemo( id: url, action: . playButtonTapped) ) {
219
214
$0. voiceMemos [ id: url] ? . mode = . playing( progress: 0 )
220
215
}
@@ -273,9 +268,6 @@ class VoiceMemosTests: XCTestCase {
273
268
274
269
func testDeleteMemoWhilePlaying( ) async {
275
270
let url = URL ( fileURLWithPath: " pointfreeco/functions.m4a " )
276
- var environment = VoiceMemosEnvironment . unimplemented
277
- environment. audioPlayer. play = { _ in try await Task . never ( ) }
278
- environment. mainRunLoop = mainRunLoop
279
271
280
272
let store = TestStore (
281
273
initialState: VoiceMemosState (
@@ -290,9 +282,12 @@ class VoiceMemosTests: XCTestCase {
290
282
]
291
283
) ,
292
284
reducer: voiceMemosReducer,
293
- environment: environment
285
+ environment: . unimplemented
294
286
)
295
287
288
+ store. environment. audioPlayer. play = { _ in try await Task . never ( ) }
289
+ store. environment. mainRunLoop = self . mainRunLoop
290
+
296
291
await store. send ( . voiceMemo( id: url, action: . playButtonTapped) ) {
297
292
$0. voiceMemos [ id: url] ? . mode = . playing( progress: 0 )
298
293
}
0 commit comments