-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add a few missing tests #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add a few missing tests #1539
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
99 changes: 99 additions & 0 deletions
99
Tests/ComposableArchitectureTests/ForEachReducerTests.swift
This file contains hidden or 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,99 @@ | ||
import ComposableArchitecture | ||
import XCTest | ||
|
||
@MainActor | ||
final class ForEachReducerTests: XCTestCase { | ||
func testElementAction() async { | ||
let store = TestStore( | ||
initialState: Elements.State( | ||
rows: [ | ||
.init(id: 1, value: "Blob"), | ||
.init(id: 2, value: "Blob Jr."), | ||
.init(id: 3, value: "Blob Sr."), | ||
] | ||
), | ||
reducer: Elements() | ||
) | ||
|
||
await store.send(.row(id: 1, action: "Blob Esq.")) { | ||
$0.rows[id: 1]?.value = "Blob Esq." | ||
} | ||
await store.send(.row(id: 2, action: "")) { | ||
$0.rows[id: 2]?.value = "" | ||
} | ||
await store.receive(.row(id: 2, action: "Empty")) { | ||
$0.rows[id: 2]?.value = "Empty" | ||
} | ||
} | ||
|
||
func testNonElementAction() async { | ||
let store = TestStore( | ||
initialState: Elements.State(), | ||
reducer: Elements() | ||
) | ||
|
||
await store.send(.buttonTapped) | ||
} | ||
|
||
#if DEBUG | ||
func testMissingElement() async { | ||
let store = TestStore( | ||
initialState: Elements.State(), | ||
reducer: EmptyReducer() | ||
.forEach(\.rows, action: /Elements.Action.row) {} | ||
) | ||
|
||
XCTExpectFailure { | ||
$0.compactDescription == """ | ||
A "forEach" at "\(#fileID):\(#line - 5)" received an action for a missing element. | ||
|
||
Action: | ||
Elements.Action.row(id:, action:) | ||
|
||
This is generally considered an application logic error, and can happen for a few reasons: | ||
|
||
• A parent reducer removed an element with this ID before this reducer ran. This reducer \ | ||
must run before any other reducer removes an element, which ensures that element reducers \ | ||
can handle their actions while their state is still available. | ||
|
||
• An in-flight effect emitted this action when state contained no element at this ID. \ | ||
While it may be perfectly reasonable to ignore this action, consider canceling the \ | ||
associated effect before an element is removed, especially if it is a long-living effect. | ||
|
||
• This action was sent to the store while its state contained no element at this ID. To \ | ||
fix this make sure that actions for this reducer can only be sent from a view store when \ | ||
its state contains an element at this id. In SwiftUI applications, use "ForEachStore". | ||
""" | ||
} | ||
|
||
await store.send(.row(id: 1, action: "Blob Esq.")) | ||
} | ||
#endif | ||
} | ||
|
||
struct Elements: ReducerProtocol { | ||
struct State: Equatable { | ||
struct Row: Equatable, Identifiable { | ||
var id: Int | ||
var value: String | ||
} | ||
var rows: IdentifiedArrayOf<Row> = [] | ||
} | ||
enum Action: Equatable { | ||
case buttonTapped | ||
case row(id: Int, action: String) | ||
} | ||
var body: Reduce<State, Action> { | ||
Reduce<State, Action> { state, action in | ||
.none | ||
} | ||
.forEach(\.rows, action: /Action.row) { | ||
Reduce { state, action in | ||
state.value = action | ||
return action.isEmpty | ||
? .run { await $0("Empty") } | ||
: .none | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Tests/ComposableArchitectureTests/IfCaseLetReducerTests.swift
This file contains hidden or 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,73 @@ | ||
import ComposableArchitecture | ||
import XCTest | ||
|
||
@MainActor | ||
final class IfCaseLetReducerTests: XCTestCase { | ||
func testChildAction() async { | ||
struct SomeError: Error, Equatable {} | ||
|
||
let store = TestStore( | ||
initialState: Result.success(0), | ||
reducer: Reduce<Result<Int, SomeError>, Result<Int, SomeError>> { state, action in | ||
.none | ||
} | ||
.ifCaseLet(/Result.success, action: /Result.success) { | ||
Reduce { state, action in | ||
state = action | ||
return state < 0 ? .run { await $0(0) } : .none | ||
} | ||
} | ||
) | ||
|
||
await store.send(.success(1)) { | ||
$0 = .success(1) | ||
} | ||
await store.send(.failure(SomeError())) | ||
await store.send(.success(-1)) { | ||
$0 = .success(-1) | ||
} | ||
await store.receive(.success(0)) { | ||
$0 = .success(0) | ||
} | ||
} | ||
|
||
#if DEBUG | ||
func testNilChild() async { | ||
struct SomeError: Error, Equatable {} | ||
|
||
let store = TestStore( | ||
initialState: Result.failure(SomeError()), | ||
reducer: EmptyReducer<Result<Int, SomeError>, Result<Int, SomeError>>() | ||
.ifCaseLet(/Result.success, action: /Result.success) {} | ||
) | ||
|
||
XCTExpectFailure { | ||
$0.compactDescription == """ | ||
An "ifCaseLet" at "\(#fileID):\(#line - 5)" received a child action when child state was \ | ||
set to a different case. … | ||
|
||
Action: | ||
Result.success | ||
State: | ||
Result.failure | ||
|
||
This is generally considered an application logic error, and can happen for a few reasons: | ||
|
||
• A parent reducer set "Result" to a different case before this reducer ran. This reducer \ | ||
must run before any other reducer sets child state to a different case. This ensures that \ | ||
child reducers can handle their actions while their state is still available. | ||
|
||
• An in-flight effect emitted this action when child state was unavailable. While it may \ | ||
be perfectly reasonable to ignore this action, consider canceling the associated effect \ | ||
before child state changes to another case, especially if it is a long-living effect. | ||
|
||
• This action was sent to the store while state was another case. Make sure that actions \ | ||
for this reducer can only be sent from a view store when state is set to the appropriate \ | ||
case. In SwiftUI applications, use "SwitchStore". | ||
""" | ||
} | ||
|
||
await store.send(.success(1)) | ||
} | ||
#endif | ||
} |
This file contains hidden or 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,41 @@ | ||
import ComposableArchitecture | ||
import XCTest | ||
|
||
@MainActor | ||
final class IfLetReducerTests: XCTestCase { | ||
#if DEBUG | ||
func testNilChild() async { | ||
let store = TestStore( | ||
initialState: Int?.none, | ||
reducer: EmptyReducer<Int?, Void>() | ||
.ifLet(\.self, action: /.self) {} | ||
) | ||
|
||
XCTExpectFailure { | ||
$0.compactDescription == """ | ||
An "ifLet" at "\(#fileID):\(#line - 5)" received a child action when child state was \ | ||
"nil". … | ||
|
||
Action: | ||
() | ||
|
||
This is generally considered an application logic error, and can happen for a few reasons: | ||
|
||
• A parent reducer set child state to "nil" before this reducer ran. This reducer must run \ | ||
before any other reducer sets child state to "nil". This ensures that child reducers can \ | ||
handle their actions while their state is still available. | ||
|
||
• An in-flight effect emitted this action when child state was "nil". While it may be \ | ||
perfectly reasonable to ignore this action, consider canceling the associated effect \ | ||
before child state becomes "nil", especially if it is a long-living effect. | ||
|
||
• This action was sent to the store while state was "nil". Make sure that actions for this \ | ||
reducer can only be sent from a view store when state is non-"nil". In SwiftUI \ | ||
applications, use "IfLetStore". | ||
""" | ||
} | ||
|
||
await store.send(()) | ||
} | ||
#endif | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed a small update to make some of our test failure tests a bit more resilient by using
#fileID
instead of hard coding the file name. This makes it so that if we rename the file it does not break tests.