Skip to content

Commit

Permalink
Added some tests for Shared State case study. (#262)
Browse files Browse the repository at this point in the history
* 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 <>
  • Loading branch information
nmccann authored and mluisbrown committed Sep 29, 2020
1 parent e5234c2 commit 937b7c4
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Examples/CaseStudies/CaseStudies.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

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

/* Begin PBXFileReference section */
4F5AC11E24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "01-GettingStarted-SharedStateTests.swift"; sourceTree = "<group>"; };
CA0C0C4624B89BEC00CBDD8A /* 04-HigherOrderReducers-LifecycleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "04-HigherOrderReducers-LifecycleTests.swift"; sourceTree = "<group>"; };
CA0C51FA245389CC00A04EAB /* 04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "04-HigherOrderReducers-ReusableOfflineDownloadsTests.swift"; sourceTree = "<group>"; };
CA25E5D124463AD700DA666A /* 01-GettingStarted-Bindings-Basics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "01-GettingStarted-Bindings-Basics.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -419,6 +421,7 @@
children = (
CA50BE5F24A8F46500FE7DBA /* 01-GettingStarted-AlertsAndActionSheetsTests.swift */,
CA34170724A4E89500FAF950 /* 01-GettingStarted-AnimationsTests.swift */,
4F5AC11E24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift */,
CAA9ADC324465AB00003A984 /* 02-Effects-BasicsTests.swift */,
CAA9ADC724465D950003A984 /* 02-Effects-CancellationTests.swift */,
CAA9ADCB2446615B0003A984 /* 02-Effects-LongLivingTests.swift */,
Expand Down Expand Up @@ -778,6 +781,7 @@
DC07231724465D1E003A8B65 /* 02-Effects-TimersTests.swift in Sources */,
CA50BE6024A8F46500FE7DBA /* 01-GettingStarted-AlertsAndActionSheetsTests.swift in Sources */,
CAA9ADC424465AB00003A984 /* 02-Effects-BasicsTests.swift in Sources */,
4F5AC11F24ECC7E4009DC50B /* 01-GettingStarted-SharedStateTests.swift in Sources */,
CAA9ADCC2446615B0003A984 /* 02-Effects-LongLivingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Expand Up @@ -67,19 +67,19 @@ struct SharedState: Equatable {
}
}

enum SharedStateAction {
enum SharedStateAction: Equatable {
case counter(CounterAction)
case profile(ProfileAction)
case selectTab(SharedState.Tab)

enum CounterAction {
enum CounterAction: Equatable {
case alertDismissed
case decrementButtonTapped
case incrementButtonTapped
case isPrimeButtonTapped
}

enum ProfileAction {
enum ProfileAction: Equatable {
case resetCounterButtonTapped
}
}
Expand Down
@@ -0,0 +1,105 @@
import Combine
import ComposableArchitecture
import XCTest

@testable import SwiftUICaseStudies

class SharedStateTests: XCTestCase {
func testTabRestoredOnReset() {
let store = TestStore(
initialState: SharedState(),
reducer: sharedStateReducer,
environment: ()
)

store.assert(
.send(.selectTab(.profile)) {
$0.currentTab = .profile
$0.profile = .init(currentTab: .profile, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
},
.send(.profile(.resetCounterButtonTapped)) {
$0.currentTab = .counter
$0.profile = .init(currentTab: .counter, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
})
}

func testTabSelection() {
let store = TestStore(
initialState: SharedState(),
reducer: sharedStateReducer,
environment: ()
)

store.assert(
.send(.selectTab(.profile)) {
$0.currentTab = .profile
$0.profile = .init(currentTab: .profile, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
},
.send(.selectTab(.counter)) {
$0.currentTab = .counter
$0.profile = .init(currentTab: .counter, count: 0, maxCount: 0, minCount: 0, numberOfCounts: 0)
})
}

func testSharedCounts() {
let store = TestStore(
initialState: SharedState(),
reducer: sharedStateReducer,
environment: ()
)

store.assert(
.send(.counter(.incrementButtonTapped)) {
$0.counter.count = 1
$0.counter.maxCount = 1
$0.counter.numberOfCounts = 1
},
.send(.counter(.decrementButtonTapped)) {
$0.counter.count = 0
$0.counter.numberOfCounts = 2
},
.send(.counter(.decrementButtonTapped)) {
$0.counter.count = -1
$0.counter.minCount = -1
$0.counter.numberOfCounts = 3
})
}

func testIsPrimeWhenPrime() {
let store = TestStore(
initialState: SharedState.CounterState(alert: nil, count: 3, maxCount: 0, minCount: 0, numberOfCounts: 0),
reducer: sharedStateCounterReducer,
environment: ()
)

store.assert(
.send(.isPrimeButtonTapped) {
$0.alert = .init(
title: "👍 The number \($0.count) is prime!"
)
},
.send(.alertDismissed) {
$0.alert = nil
}
)
}

func testIsPrimeWhenNotPrime() {
let store = TestStore(
initialState: SharedState.CounterState(alert: nil, count: 6, maxCount: 0, minCount: 0, numberOfCounts: 0),
reducer: sharedStateCounterReducer,
environment: ()
)

store.assert(
.send(.isPrimeButtonTapped) {
$0.alert = .init(
title: "👎 The number \($0.count) is not prime :("
)
},
.send(.alertDismissed) {
$0.alert = nil
}
)
}
}

0 comments on commit 937b7c4

Please sign in to comment.