Skip to content

Commit 937b7c4

Browse files
nmccannmluisbrown
authored andcommitted
Added some tests for Shared State case study. (#262)
* Added some tests for Shared State case study. * Expanded upon tests by also validating that state is mirrored State is expected to be mirrored (or shared) between the Counter and Profile - the tests have been updated to verify this. * Modified tests to focus more on what is changing between steps Co-authored-by: Noah McCann <>
1 parent e5234c2 commit 937b7c4

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

Examples/CaseStudies/CaseStudies.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4F5AC11F24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F5AC11E24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift */; };
1011
CA0C0C4724B89BEC00CBDD8A /* 04-HigherOrderReducers-LifecycleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA0C0C4624B89BEC00CBDD8A /* 04-HigherOrderReducers-LifecycleTests.swift */; };
1112
CA0C51FB245389CC00A04EAB /* 04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA0C51FA245389CC00A04EAB /* 04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift */; };
1213
CA25E5D224463AD700DA666A /* 01-GettingStarted-Bindings-Basics.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA25E5D124463AD700DA666A /* 01-GettingStarted-Bindings-Basics.swift */; };
@@ -144,6 +145,7 @@
144145
/* End PBXCopyFilesBuildPhase section */
145146

146147
/* Begin PBXFileReference section */
148+
4F5AC11E24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "01-GettingStarted-SharedStateTests.swift"; sourceTree = "<group>"; };
147149
CA0C0C4624B89BEC00CBDD8A /* 04-HigherOrderReducers-LifecycleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "04-HigherOrderReducers-LifecycleTests.swift"; sourceTree = "<group>"; };
148150
CA0C51FA245389CC00A04EAB /* 04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift"; sourceTree = "<group>"; };
149151
CA25E5D124463AD700DA666A /* 01-GettingStarted-Bindings-Basics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "01-GettingStarted-Bindings-Basics.swift"; sourceTree = "<group>"; };
@@ -419,6 +421,7 @@
419421
children = (
420422
CA50BE5F24A8F46500FE7DBA /* 01-GettingStarted-AlertsAndActionSheetsTests.swift */,
421423
CA34170724A4E89500FAF950 /* 01-GettingStarted-AnimationsTests.swift */,
424+
4F5AC11E24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift */,
422425
CAA9ADC324465AB00003A984 /* 02-Effects-BasicsTests.swift */,
423426
CAA9ADC724465D950003A984 /* 02-Effects-CancellationTests.swift */,
424427
CAA9ADCB2446615B0003A984 /* 02-Effects-LongLivingTests.swift */,
@@ -778,6 +781,7 @@
778781
DC07231724465D1E003A8B65 /* 02-Effects-TimersTests.swift in Sources */,
779782
CA50BE6024A8F46500FE7DBA /* 01-GettingStarted-AlertsAndActionSheetsTests.swift in Sources */,
780783
CAA9ADC424465AB00003A984 /* 02-Effects-BasicsTests.swift in Sources */,
784+
4F5AC11F24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift in Sources */,
781785
CAA9ADCC2446615B0003A984 /* 02-Effects-LongLivingTests.swift in Sources */,
782786
);
783787
runOnlyForDeploymentPostprocessing = 0;

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-SharedState.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ struct SharedState: Equatable {
6767
}
6868
}
6969

70-
enum SharedStateAction {
70+
enum SharedStateAction: Equatable {
7171
case counter(CounterAction)
7272
case profile(ProfileAction)
7373
case selectTab(SharedState.Tab)
7474

75-
enum CounterAction {
75+
enum CounterAction: Equatable {
7676
case alertDismissed
7777
case decrementButtonTapped
7878
case incrementButtonTapped
7979
case isPrimeButtonTapped
8080
}
8181

82-
enum ProfileAction {
82+
enum ProfileAction: Equatable {
8383
case resetCounterButtonTapped
8484
}
8585
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import Combine
2+
import ComposableArchitecture
3+
import XCTest
4+
5+
@testable import SwiftUICaseStudies
6+
7+
class SharedStateTests: XCTestCase {
8+
func testTabRestoredOnReset() {
9+
let store = TestStore(
10+
initialState: SharedState(),
11+
reducer: sharedStateReducer,
12+
environment: ()
13+
)
14+
15+
store.assert(
16+
.send(.selectTab(.profile)) {
17+
$0.currentTab = .profile
18+
$0.profile = .init(currentTab: .profile, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
19+
},
20+
.send(.profile(.resetCounterButtonTapped)) {
21+
$0.currentTab = .counter
22+
$0.profile = .init(currentTab: .counter, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
23+
})
24+
}
25+
26+
func testTabSelection() {
27+
let store = TestStore(
28+
initialState: SharedState(),
29+
reducer: sharedStateReducer,
30+
environment: ()
31+
)
32+
33+
store.assert(
34+
.send(.selectTab(.profile)) {
35+
$0.currentTab = .profile
36+
$0.profile = .init(currentTab: .profile, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
37+
},
38+
.send(.selectTab(.counter)) {
39+
$0.currentTab = .counter
40+
$0.profile = .init(currentTab: .counter, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
41+
})
42+
}
43+
44+
func testSharedCounts() {
45+
let store = TestStore(
46+
initialState: SharedState(),
47+
reducer: sharedStateReducer,
48+
environment: ()
49+
)
50+
51+
store.assert(
52+
.send(.counter(.incrementButtonTapped)) {
53+
$0.counter.count = 1
54+
$0.counter.maxCount = 1
55+
$0.counter.numberOfCounts = 1
56+
},
57+
.send(.counter(.decrementButtonTapped)) {
58+
$0.counter.count = 0
59+
$0.counter.numberOfCounts = 2
60+
},
61+
.send(.counter(.decrementButtonTapped)) {
62+
$0.counter.count = -1
63+
$0.counter.minCount = -1
64+
$0.counter.numberOfCounts = 3
65+
})
66+
}
67+
68+
func testIsPrimeWhenPrime() {
69+
let store = TestStore(
70+
initialState: SharedState.CounterState(alert: nil, count: 3, maxCount: 0, minCount: 0, numberOfCounts: 0),
71+
reducer: sharedStateCounterReducer,
72+
environment: ()
73+
)
74+
75+
store.assert(
76+
.send(.isPrimeButtonTapped) {
77+
$0.alert = .init(
78+
title: "👍 The number \($0.count) is prime!"
79+
)
80+
},
81+
.send(.alertDismissed) {
82+
$0.alert = nil
83+
}
84+
)
85+
}
86+
87+
func testIsPrimeWhenNotPrime() {
88+
let store = TestStore(
89+
initialState: SharedState.CounterState(alert: nil, count: 6, maxCount: 0, minCount: 0, numberOfCounts: 0),
90+
reducer: sharedStateCounterReducer,
91+
environment: ()
92+
)
93+
94+
store.assert(
95+
.send(.isPrimeButtonTapped) {
96+
$0.alert = .init(
97+
title: "👎 The number \($0.count) is not prime :("
98+
)
99+
},
100+
.send(.alertDismissed) {
101+
$0.alert = nil
102+
}
103+
)
104+
}
105+
}

0 commit comments

Comments
 (0)