Skip to content

Commit 199d7fe

Browse files
author
Marius Serban
committed
remove counters using re-swift actions
1 parent d5bf2bd commit 199d7fe

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CounterExample/Actions/Actions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ struct CounterActionDecrease: Action {
1111
struct CounterActionAdd: Action {
1212

1313
}
14+
15+
struct CounterActionRemove: Action {
16+
let index: Int
17+
}

CounterExample/Reducers/CounterReducer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct CounterReducer: Reducer {
1818
state.counters[decreaseAction.index] -= 1
1919
case is CounterActionAdd:
2020
state.counters.append(Counter())
21+
case let removeAction as CounterActionRemove:
22+
state.counters.remove(at: removeAction.index)
2123
default:
2224
break
2325
}

CounterExample/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ extension ViewController : UITableViewDataSource {
5353
return cell
5454
}
5555

56-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
57-
mainStore.state.counters.remove(at: indexPath.row)
58-
}
56+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
57+
mainStore.dispatch(CounterActionRemove(index: indexPath.row))
58+
}
5959

6060
}
6161

CounterExampleTests/CounterReducerTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ class CounterReducerTests: XCTestCase {
5050
XCTAssertEqual(appState.counters.count, 1)
5151
}
5252

53+
func testRemoveCounterDecrementsCountersCountByOne() {
54+
let appState = reducer.handleAction(action: CounterActionAdd(), state: nil)
55+
let stateAfterDelete = reducer.handleAction(action: CounterActionRemove(index: 0), state: appState)
56+
XCTAssertEqual(stateAfterDelete.counters.count, 0)
57+
}
58+
59+
func testRemoveCounterAtSpecificIndexRemovesCorrectCounter() {
60+
let oneCounterState = reducer.handleAction(action: CounterActionAdd(), state: nil)
61+
let twoCountersState = reducer.handleAction(action: CounterActionAdd(), state: oneCounterState)
62+
let incrementedSecondCounterState = reducer.handleAction(action: CounterActionIncrease(index: 1), state: twoCountersState)
63+
let stateAfterDelete = reducer.handleAction(action: CounterActionRemove(index: 0), state: incrementedSecondCounterState)
64+
65+
XCTAssertEqual(stateAfterDelete.counters[0], 1)
66+
}
67+
5368
func handle(_ inputValue: Int?, _ action: Action) -> Int {
5469
let appState = AppState(counters: [Counter(inputValue ?? 0)])
5570
return reducer.handleAction(action: action, state: appState).counters[0]

0 commit comments

Comments
 (0)