diff --git a/list/list_test.go b/list/list_test.go index 2c925aa..0b025a6 100644 --- a/list/list_test.go +++ b/list/list_test.go @@ -11,7 +11,7 @@ import ( type item string -func (i item) FilterValue() string { return "" } +func (i item) FilterValue() string { return string(i) } type itemDelegate struct{} @@ -72,3 +72,71 @@ func TestCustomStatusBarItemName(t *testing.T) { t.Fatalf("Error: expected view to contain %s", expected) } } + +func TestIndex(t *testing.T) { + list := New([]Item{item("bar"), item("baz"), item("foo"), item("bar"), item("panda"), item("bay")}, itemDelegate{}, 10, 15) + list.cursor = 0 + + // Hardcode filtering + list.filterState = Filtering + list.FilterInput.SetValue("f") + msg := filterItems(list) + val, ok := msg().(FilterMatchesMsg) + if ok { + list.filteredItems = filteredItems(val) + } + + // index updates once the cursor changes + t.Log("cursor", list.Cursor()) + t.Log("i:", list.Index()) + t.Log("gi:", list.GlobalIndex()) + + t.Log("\n", list.View()) + + // take 2 + list.ResetFilter() + + t.Log("\n", list.View()) + list.filterState = Filtering + list.FilterInput.SetValue("ba") + msg = filterItems(list) + val, ok = msg().(FilterMatchesMsg) + if ok { + list.filteredItems = filteredItems(val) + } + // cursor at 0 + t.Log("cursor", list.Cursor()) + t.Log("i:", list.Index()) + t.Log("gi:", list.GlobalIndex()) + + list.CursorDown() + t.Log("moved cursor") + + // cursor at 1 + t.Log("cursor", list.Cursor()) + t.Log("i:", list.Index()) + t.Log("gi:", list.GlobalIndex()) + + list.CursorDown() + t.Log("moved cursor") + + // cursor at 2, gi at 3 + t.Log("cursor", list.Cursor()) + t.Log("i:", list.Index()) + t.Log("gi:", list.GlobalIndex()) + + list.CursorDown() + t.Log("moved cursor") + + // cursor at 2, gi at 5 + t.Log("cursor", list.Cursor()) + t.Log("i:", list.Index()) + t.Log("gi:", list.GlobalIndex()) + + t.Log("\n", list.View()) + realIndex := list.GlobalIndex() + + if realIndex != 5 { + t.Fatalf("expected the item's index to be 5, got %d", realIndex) + } +}