Skip to content

Commit 7de6bce

Browse files
authored
Invalidate cache when filter func applied and add test (#193)
* Invalidate cache when filter func applied and add test * Add blank line for linting
1 parent c487e82 commit 7de6bce

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

table/filter_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,51 @@ func TestFilterWithSetValue(t *testing.T) {
294294
assert.Len(t, filteredRows, 3)
295295
}
296296

297+
func TestFilterFunc(t *testing.T) {
298+
const (
299+
colTitle = "title"
300+
colDesc = "description"
301+
)
302+
303+
columns := []Column{NewColumn("title", "title", 10).WithFiltered(true)}
304+
rows := []Row{
305+
NewRow(RowData{
306+
colTitle: "AAA",
307+
colDesc: "",
308+
}),
309+
NewRow(RowData{
310+
colTitle: "BBB",
311+
colDesc: "",
312+
}),
313+
// Empty
314+
NewRow(RowData{}),
315+
}
316+
317+
filterFunc := func(r Row, s string) bool {
318+
// Completely arbitrary check for testing purposes
319+
title := fmt.Sprintf("%v", r.Data["title"])
320+
321+
return title == "AAA" && s == "x"
322+
}
323+
324+
// First check that the table won't match with different case
325+
model := New(columns).WithRows(rows).Filtered(true)
326+
model = model.WithFilterInputValue("x")
327+
328+
filteredRows := model.getFilteredRows(rows)
329+
assert.Len(t, filteredRows, 0)
330+
331+
// The filter func should then match the one row
332+
model = model.WithFilterFunc(filterFunc)
333+
filteredRows = model.getFilteredRows(rows)
334+
assert.Len(t, filteredRows, 1)
335+
336+
// Remove filter
337+
model = model.WithFilterInputValue("")
338+
filteredRows = model.getFilteredRows(rows)
339+
assert.Len(t, filteredRows, 3)
340+
}
341+
297342
func BenchmarkFilteredScrolling(b *testing.B) {
298343
// Scrolling through a filtered table with many rows should be quick
299344
// https://github.com/Evertras/bubble-table/issues/135

table/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ func (m Model) WithFilterInputValue(value string) Model {
371371
func (m Model) WithFilterFunc(shouldInclude func(row Row, filterInput string) bool) Model {
372372
m.filterFunc = shouldInclude
373373

374+
m.visibleRowCacheUpdated = false
375+
374376
return m
375377
}
376378

0 commit comments

Comments
 (0)