Skip to content

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 6 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Tests/ComposableArchitectureTests/EffectFailureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
var line: UInt!
XCTExpectFailure {
$0.compactDescription == """
An "EffectTask.task" returned from \
"ComposableArchitectureTests/EffectFailureTests.swift:\(line+1)" threw an unhandled \
error. …
An "EffectTask.task" returned from "\(#fileID):\(line+1)" threw an unhandled error. …
Copy link
Member Author

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.


EffectFailureTests.Unexpected()

Expand All @@ -39,9 +37,7 @@
var line: UInt!
XCTExpectFailure {
$0.compactDescription == """
An "EffectTask.run" returned from \
"ComposableArchitectureTests/EffectFailureTests.swift:\(line+1)" threw an unhandled \
error. …
An "EffectTask.run" returned from "\(#fileID):\(line+1)" threw an unhandled error. …

EffectFailureTests.Unexpected()

Expand Down
3 changes: 1 addition & 2 deletions Tests/ComposableArchitectureTests/EffectRunTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ final class EffectRunTests: XCTestCase {
var line: UInt!
XCTExpectFailure(nil, enabled: nil, strict: nil) {
$0.compactDescription == """
An "EffectTask.run" returned from \
"ComposableArchitectureTests/EffectRunTests.swift:\(line+1)" threw an unhandled error. …
An "EffectTask.run" returned from "\(#fileID):\(line+1)" threw an unhandled error. …

EffectRunTests.Failure()

Expand Down
3 changes: 1 addition & 2 deletions Tests/ComposableArchitectureTests/EffectTaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ final class EffectTaskTests: XCTestCase {
var line: UInt!
XCTExpectFailure(nil, enabled: nil, strict: nil) {
$0.compactDescription == """
An "EffectTask.task" returned from \
"ComposableArchitectureTests/EffectTaskTests.swift:\(line+1)" threw an unhandled error. …
An "EffectTask.task" returned from "\(#fileID):\(line+1)" threw an unhandled error. …

EffectTaskTests.Failure()

Expand Down
99 changes: 99 additions & 0 deletions Tests/ComposableArchitectureTests/ForEachReducerTests.swift
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 Tests/ComposableArchitectureTests/IfCaseLetReducerTests.swift
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
}
41 changes: 41 additions & 0 deletions Tests/ComposableArchitectureTests/IfLetReducerTests.swift
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
}
Loading