mirror of
https://github.com/zoriya/bubbles.git
synced 2026-08-16 01:43:21 +00:00
feat(list): add SetFilterText and SetFilterState (#335)
* Add SetFilterText and SetFilterState * make linter happy * make SetFilterState state not hardcoded * tests for SetFilterText and SetFilterState * fix small typo --------- Co-authored-by: k-x7 <97758178+k-x7@users.noreply.github.com>
This commit is contained in:
+64
-1
@@ -3,6 +3,7 @@ package list
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
|
||||
type item string
|
||||
|
||||
func (i item) FilterValue() string { return "" }
|
||||
func (i item) FilterValue() string { return string(i) }
|
||||
|
||||
type itemDelegate struct{}
|
||||
|
||||
@@ -72,3 +73,65 @@ func TestCustomStatusBarItemName(t *testing.T) {
|
||||
t.Fatalf("Error: expected view to contain %s", expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetFilterText(t *testing.T) {
|
||||
tc := []Item{item("foo"), item("bar"), item("baz")}
|
||||
|
||||
list := New(tc, itemDelegate{}, 10, 10)
|
||||
list.SetFilterText("ba")
|
||||
|
||||
list.SetFilterState(Unfiltered)
|
||||
expected := tc
|
||||
// TODO: replace with slices.Equal() when project move to go1.18 or later
|
||||
if !reflect.DeepEqual(list.VisibleItems(), expected) {
|
||||
t.Fatalf("Error: expected view to contain only %s", expected)
|
||||
}
|
||||
|
||||
list.SetFilterState(Filtering)
|
||||
expected = []Item{item("bar"), item("baz")}
|
||||
if !reflect.DeepEqual(list.VisibleItems(), expected) {
|
||||
t.Fatalf("Error: expected view to contain only %s", expected)
|
||||
}
|
||||
|
||||
list.SetFilterState(FilterApplied)
|
||||
if !reflect.DeepEqual(list.VisibleItems(), expected) {
|
||||
t.Fatalf("Error: expected view to contain only %s", expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetFilterState(t *testing.T) {
|
||||
tc := []Item{item("foo"), item("bar"), item("baz")}
|
||||
|
||||
list := New(tc, itemDelegate{}, 10, 10)
|
||||
list.SetFilterText("ba")
|
||||
|
||||
list.SetFilterState(Unfiltered)
|
||||
expected, notExpected := "up", "clear filter"
|
||||
|
||||
lines := strings.Split(list.View(), "\n")
|
||||
footer := lines[len(lines)-1]
|
||||
|
||||
if !strings.Contains(footer, expected) || strings.Contains(footer, notExpected) {
|
||||
t.Fatalf("Error: expected view to contain '%s' not '%s'", expected, notExpected)
|
||||
}
|
||||
|
||||
list.SetFilterState(Filtering)
|
||||
expected, notExpected = "filter", "more"
|
||||
|
||||
lines = strings.Split(list.View(), "\n")
|
||||
footer = lines[len(lines)-1]
|
||||
|
||||
if !strings.Contains(footer, expected) || strings.Contains(footer, notExpected) {
|
||||
t.Fatalf("Error: expected view to contain '%s' not '%s'", expected, notExpected)
|
||||
}
|
||||
|
||||
list.SetFilterState(FilterApplied)
|
||||
expected = "clear"
|
||||
|
||||
lines = strings.Split(list.View(), "\n")
|
||||
footer = lines[len(lines)-1]
|
||||
|
||||
if !strings.Contains(footer, expected) {
|
||||
t.Fatalf("Error: expected view to contain '%s'", expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user