Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <>
- Loading branch information
1 parent
e5234c2
commit 937b7c4
Showing
3 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Examples/CaseStudies/SwiftUICaseStudiesTests/01-GettingStarted-SharedStateTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
) | ||
} | ||
} |