Skip to content

Commit b4f7e7e

Browse files
mbrandonwp4checo
authored andcommitted
Delete correct todo when filtering. (#1696)
(cherry picked from commit 1c19319aed7ab85e10e07de570f6160e46140d2b)
1 parent 6673001 commit b4f7e7e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Examples/Todos/Todos/Todos.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ struct Todos: ReducerProtocol {
5050
return .none
5151

5252
case let .delete(indexSet):
53-
state.todos.remove(atOffsets: indexSet)
53+
let filteredTodos = state.filteredTodos
54+
for index in indexSet {
55+
state.todos.remove(id: filteredTodos[index].id)
56+
}
5457
return .none
5558

5659
case let .editModeChanged(editMode):

Examples/Todos/TodosTests/TodosTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,41 @@ final class TodosTests: XCTestCase {
196196
}
197197
}
198198

199+
func testDeleteWhileFiltered() async {
200+
let state = Todos.State(
201+
filter: .completed,
202+
todos: [
203+
Todo.State(
204+
description: "",
205+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000000")!,
206+
isComplete: false
207+
),
208+
Todo.State(
209+
description: "",
210+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000001")!,
211+
isComplete: false
212+
),
213+
Todo.State(
214+
description: "",
215+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000002")!,
216+
isComplete: true
217+
),
218+
]
219+
)
220+
221+
let store = TestStore(
222+
initialState: state,
223+
reducer: Todos()
224+
)
225+
226+
await store.send(.delete([0])) {
227+
$0.todos = [
228+
$0.todos[0],
229+
$0.todos[1],
230+
]
231+
}
232+
}
233+
199234
func testEditModeMoving() async {
200235
let state = Todos.State(
201236
todos: [

0 commit comments

Comments
 (0)