-
-
Notifications
You must be signed in to change notification settings - Fork 385
fix: PrevItem does not scroll viewport at top boundary #838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lawrence3699
wants to merge
1
commit into
dlvhdr:main
from
lawrence3699:fix/previtem-viewport-scroll
+120
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
internal/tui/components/listviewport/listviewport_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package listviewport | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dlvhdr/gh-dash/v4/internal/tui/constants" | ||
| "github.com/dlvhdr/gh-dash/v4/internal/tui/context" | ||
| ) | ||
|
|
||
| func newTestModel(numItems, viewportHeight, itemHeight int) Model { | ||
| ctx := context.ProgramContext{} | ||
| dims := constants.Dimensions{Width: 80, Height: viewportHeight} | ||
| now := time.Now() | ||
| return NewModel(ctx, dims, now, now, "items", numItems, itemHeight) | ||
| } | ||
|
|
||
| func TestPrevItemScrollsAtTopBound(t *testing.T) { | ||
| // 10 items, viewport height 5, item height 1 => 5 items per page | ||
| // Initial bounds: topBoundId=0, bottomBoundId=4 | ||
| m := newTestModel(10, 5, 1) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a struct for the arguements? makes it clearer newTestModel(opts testModelOpts) Model {
// ...
} |
||
|
|
||
| // Navigate down past the first page boundary | ||
| // After 5 NextItem calls: currId=5, topBoundId=1, bottomBoundId=5 | ||
| for i := 0; i < 5; i++ { | ||
| m.NextItem() | ||
| } | ||
|
|
||
| if m.GetCurrItem() != 5 { | ||
| t.Fatalf("expected currId=5 after 5 NextItem calls, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 1 { | ||
| t.Fatalf("expected topBoundId=1, got %d", m.topBoundId) | ||
| } | ||
|
|
||
| // Navigate back up to the top bound | ||
| // 4 PrevItem calls: currId goes 4, 3, 2, 1 | ||
| for i := 0; i < 4; i++ { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe the |
||
| m.PrevItem() | ||
| } | ||
|
|
||
| if m.GetCurrItem() != 1 { | ||
| t.Fatalf("expected currId=1, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 1 { | ||
| t.Fatalf("expected topBoundId=1 (no scroll yet), got %d", m.topBoundId) | ||
| } | ||
|
|
||
| // Now one more PrevItem: currId should go to 0, and the viewport | ||
| // should scroll up so that item 0 is visible. | ||
| // Before fix: topBoundId stays at 1, item 0 is above the viewport. | ||
| // After fix: topBoundId becomes 0, item 0 is at the top of the viewport. | ||
| m.PrevItem() | ||
|
|
||
| if m.GetCurrItem() != 0 { | ||
| t.Fatalf("expected currId=0, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 0 { | ||
| t.Errorf("PrevItem did not scroll up at the top boundary: expected topBoundId=0, got %d (item 0 is above the visible viewport)", m.topBoundId) | ||
| } | ||
| } | ||
|
|
||
| func TestNextItemScrollsAtBottomBound(t *testing.T) { | ||
| // Verify NextItem scrolling works symmetrically (this should pass already) | ||
| m := newTestModel(10, 5, 1) | ||
|
|
||
| // Navigate down to bottomBoundId (4) | ||
| for i := 0; i < 4; i++ { | ||
| m.NextItem() | ||
| } | ||
|
|
||
| if m.GetCurrItem() != 4 { | ||
| t.Fatalf("expected currId=4, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 0 { | ||
| t.Fatalf("expected topBoundId=0 (no scroll yet), got %d", m.topBoundId) | ||
| } | ||
|
|
||
| // One more NextItem at the bottom boundary should scroll | ||
| m.NextItem() | ||
|
|
||
| if m.GetCurrItem() != 5 { | ||
| t.Fatalf("expected currId=5, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 1 { | ||
| t.Errorf("NextItem did not scroll down at the bottom boundary: expected topBoundId=1, got %d", m.topBoundId) | ||
| } | ||
| } | ||
|
|
||
| func TestPrevItemAtFirstItem(t *testing.T) { | ||
| // Pressing up when already at the first item should stay at 0 | ||
| // and should not corrupt viewport bounds. | ||
| m := newTestModel(10, 5, 1) | ||
|
|
||
| m.PrevItem() | ||
|
|
||
| if m.GetCurrItem() != 0 { | ||
| t.Errorf("expected currId=0, got %d", m.GetCurrItem()) | ||
| } | ||
| if m.topBoundId != 0 { | ||
| t.Errorf("expected topBoundId=0 (should not scroll), got %d", m.topBoundId) | ||
| } | ||
| if m.bottomBoundId != 4 { | ||
| t.Errorf("expected bottomBoundId=4 (should not scroll), got %d", m.bottomBoundId) | ||
| } | ||
| } | ||
|
|
||
| func TestNextItemAtLastItem(t *testing.T) { | ||
| // Pressing down when already at the last item should stay at last | ||
| m := newTestModel(10, 5, 1) | ||
|
|
||
| for i := 0; i < 20; i++ { | ||
| m.NextItem() | ||
| } | ||
|
|
||
| if m.GetCurrItem() != 9 { | ||
| t.Errorf("expected currId=9, got %d", m.GetCurrItem()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really get the need for the
m.currId > 0condition. I tested it without it and it seems to work fine. Also theatTopOfViewportis misleading with that condition.