Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement ToDo remover
  • Loading branch information
nagauzzi committed Jun 12, 2021
1 parent 2338442 commit dca044d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions SwiftUITCASample/ContentView.swift
Expand Up @@ -17,6 +17,7 @@ struct ToDoState: Equatable, Identifiable {
enum ToDoAction: Equatable {
case checkTapped
case textChanged(String)
case removed
}

struct ToDoEnvironment {
Expand All @@ -31,6 +32,8 @@ let todoReducer = Reducer<ToDoState, ToDoAction, ToDoEnvironment> {
case .textChanged(let text):
state.description = text
return .none
case .removed:
return .none
}
}

Expand Down Expand Up @@ -66,6 +69,9 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
}
.map (\.element)
return .none
case .todo(index: let index, action: .removed):
state.todoStates.remove(at: index)
return .none
case .todo(index: let index, action: let action):
return .none
case .addButtonTapped:
Expand Down Expand Up @@ -115,6 +121,13 @@ struct ContentView: View {
),
content: ToDoSmallView.init(store:)
)
.onDelete { indexSet in
indexSet.forEach { index in
viewStore.send(
.todo(index: index, action: .removed)
)
}
}
}
.listStyle(PlainListStyle())
.navigationBarTitle("ToDo list")
Expand Down
33 changes: 33 additions & 0 deletions SwiftUITCASampleTests/SwiftUITCASampleTests.swift
Expand Up @@ -71,4 +71,37 @@ class SwiftUITCASampleTests: XCTestCase {
}
)
}

func testRemoveToDo() {
let (uuid1, uuid2) = (UUID.init(), UUID.init())
let store = TestStore(
initialState: AppState(
todoStates: [
ToDoState(
id: uuid1,
description: "ToDo 1",
isCompleted: false
),
ToDoState(
id: uuid2,
description: "ToDo 2",
isCompleted: true
)
]
),
reducer: appReducer,
environment: AppEnvironment()
)
store.assert(
.send(.todo(index: 0, action: .removed)) { step in
step.todoStates = [
ToDoState(
id: uuid2,
description: "ToDo 2",
isCompleted: true
)
]
}
)
}
}

0 comments on commit dca044d

Please sign in to comment.