-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppStateTests.swift
119 lines (108 loc) · 3.59 KB
/
AppStateTests.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
//
// FeedbackGeneratorTests.swift
// FeedbackGeneratorTests
//
// Created by Michael Kao on 02.11.20.
//
import XCTest
import Combine
import ComposableArchitecture
import ComposableFeedbackGenerator
@testable import FeedbackGenerator
class FeedbackGeneratorTests: XCTestCase {
var environment = AppEnvironment(feedbackGenerator: .unimplemented())
var impactSubject = PassthroughSubject<Never, Never>()
var notificationSubject = PassthroughSubject<Never, Never>()
var selectedSubject = PassthroughSubject<Never, Never>()
func testGeneratorCreation() throws {
var initialImpactStyle: UIImpactFeedbackGenerator.FeedbackStyle?
var createdImpactGenerator = false
var createdNotificationGenerator = false
var createdSelectionGenerator = false
environment.feedbackGenerator.impact.create = { _, style in
initialImpactStyle = style
createdImpactGenerator = true
return self.impactSubject.eraseToEffect()
}
environment.feedbackGenerator.notification.create = { _ in
createdNotificationGenerator = true
return self.notificationSubject.eraseToEffect()
}
environment.feedbackGenerator.selection.create = { _ in
createdSelectionGenerator = true
return self.selectedSubject.eraseToEffect()
}
TestStore(
initialState: AppState(),
reducer: appReducer,
environment: environment
).assert(
.send(.onAppear),
.do { XCTAssertEqual(initialImpactStyle, .medium) },
.do { XCTAssertTrue(createdImpactGenerator) },
.do { XCTAssertTrue(createdNotificationGenerator) },
.do { XCTAssertTrue(createdSelectionGenerator) },
.do { self.impactSubject.send(completion: .finished) },
.do { self.notificationSubject.send(completion: .finished) },
.do { self.selectedSubject.send(completion: .finished) }
)
}
func testImpactGenerator() throws {
var impactOccurred = false
environment.feedbackGenerator.impact.create = { _, _ in
return self.impactSubject.eraseToEffect()
}
environment.feedbackGenerator.impact.impactOccurred = { _ in
impactOccurred = true
return .fireAndForget {}
}
TestStore(
initialState: AppState(),
reducer: appReducer,
environment: environment
).assert(
.send(.impactStylePicked(.rigid)) {
$0.impactStyle = .rigid
},
.send(.tappedImpactButton),
.do { XCTAssertTrue(impactOccurred) },
.do { self.impactSubject.send(completion: .finished) }
)
}
func testNotificationGenerator() throws {
var notificationOccurredType: UINotificationFeedbackGenerator.FeedbackType?
environment.feedbackGenerator.notification.notificationOccurred = { _, type in
notificationOccurredType = type
return .fireAndForget {}
}
TestStore(
initialState: AppState(),
reducer: appReducer,
environment: environment
).assert(
.send(.notificationTypePicked(.warning)) {
$0.notificationType = .warning
},
.send(.tappedNotificationButton),
.do { XCTAssertEqual(notificationOccurredType, .warning) }
)
}
func testSelectionGenerator() throws {
var selectionChanged = false
environment.feedbackGenerator.selection.create = { _ in
return self.selectedSubject.eraseToEffect()
}
environment.feedbackGenerator.selection.selectionChanged = { _ in
selectionChanged = true
return .fireAndForget {}
}
TestStore(
initialState: AppState(),
reducer: appReducer,
environment: environment
).assert(
.send(.tappedSelectionButton),
.do { XCTAssertTrue(selectionChanged) }
)
}
}