-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathRowOperationsDispatchTests.swift
More file actions
113 lines (99 loc) · 4.11 KB
/
RowOperationsDispatchTests.swift
File metadata and controls
113 lines (99 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// RowOperationsDispatchTests.swift
// TableProTests
//
// Locks the dispatch wiring from RowOperations into TableViewCoordinating.
// These tests guard the path that PR #938 (Phase D-b) accidentally severed:
// invalidateCachesForUndoRedo must fire on soft-delete (existing rows) so the
// red row background and yellow modified marker propagate to NSTableView's
// visible cell views without requiring a tab switch or scroll-recycle.
//
import Foundation
import Testing
@testable import TablePro
@MainActor
private final class FakeTableViewCoordinator: TableViewCoordinating {
var fullReplaceCount = 0
var insertedCount = 0
var removedCount = 0
var deltaCount = 0
var invalidateCount = 0
var commitEditCount = 0
var refreshFKCount = 0
var scrollToTopCount = 0
var beginEditingCalls: [(row: Int, column: Int)] = []
func applyInsertedRows(_ indices: IndexSet) { insertedCount += 1 }
func applyRemovedRows(_ indices: IndexSet) { removedCount += 1 }
func applyFullReplace() { fullReplaceCount += 1 }
func applyDelta(_ delta: Delta) { deltaCount += 1 }
func invalidateCachesForUndoRedo() { invalidateCount += 1 }
func commitActiveCellEdit() { commitEditCount += 1 }
func beginEditing(displayRow: Int, column: Int) {
beginEditingCalls.append((row: displayRow, column: column))
}
func refreshForeignKeyColumns() { refreshFKCount += 1 }
func scrollToTop() { scrollToTopCount += 1 }
}
@Suite("RowOperations dispatch")
@MainActor
struct RowOperationsDispatchTests {
private struct Fixture {
let coordinator: MainContentCoordinator
let tabManager: QueryTabManager
let delegate: DataTabGridDelegate
let fake: FakeTableViewCoordinator
let tabId: UUID
}
private func makeFixture(rowCount: Int = 5) -> Fixture {
let tabManager = QueryTabManager()
let coordinator = MainContentCoordinator(
connection: TestFixtures.makeConnection(),
tabManager: tabManager,
changeManager: DataChangeManager(),
filterStateManager: FilterStateManager(),
columnVisibilityManager: ColumnVisibilityManager(),
toolbarState: ConnectionToolbarState()
)
let delegate = DataTabGridDelegate()
let fake = FakeTableViewCoordinator()
delegate.tableViewCoordinator = fake
coordinator.dataTabDelegate = delegate
tabManager.addTableTab(tableName: "users")
let tabIndex = tabManager.selectedTabIndex ?? 0
tabManager.tabs[tabIndex].tableContext.isEditable = true
let tabId = tabManager.tabs[tabIndex].id
let columns = ["id", "name"]
let rows = (0..<rowCount).map { i in ["\(i)", "name\(i)"] }
let columnTypes: [ColumnType] = Array(repeating: .text(rawType: nil), count: columns.count)
coordinator.setActiveTableRows(
TableRows.from(queryRows: rows, columns: columns, columnTypes: columnTypes),
for: tabId
)
return Fixture(
coordinator: coordinator,
tabManager: tabManager,
delegate: delegate,
fake: fake,
tabId: tabId
)
}
@Test("Soft-delete of existing rows dispatches invalidateCachesForUndoRedo")
func softDeleteDispatchesInvalidate() {
let f = makeFixture(rowCount: 5)
let beforeInvalidate = f.fake.invalidateCount
f.coordinator.deleteSelectedRows(indices: [0, 1])
#expect(f.fake.invalidateCount == beforeInvalidate + 1)
#expect(f.fake.deltaCount == 0)
}
@Test("Physical delete of inserted rows dispatches applyDelta, not invalidate")
func physicalDeleteDispatchesDelta() {
let f = makeFixture(rowCount: 3)
f.coordinator.addNewRow()
let insertedIndex = f.coordinator.tableRowsStore.tableRows(for: f.tabId).count - 1
let beforeInvalidate = f.fake.invalidateCount
let beforeDelta = f.fake.deltaCount
f.coordinator.deleteSelectedRows(indices: [insertedIndex])
#expect(f.fake.invalidateCount == beforeInvalidate)
#expect(f.fake.deltaCount == beforeDelta + 1)
}
}