Skip to content
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

Add tests for paginator #480

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions paginator/paginator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package paginator

import (
"testing"

tea "github.com/charmbracelet/bubbletea"
)

func TestSetTotalPages(t *testing.T) {
tests := []struct {
name string
items int // total no of items to be set
initialTotal int // intital total pages for the testcase
expected int // expected value after SetTotalPages function call
}{
{"Less than one page", 5, 1, 5},
{"Exactly one page", 10, 1, 10},
{"More than one page", 15, 1, 15},
{"negative value for page", -10, 1, 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New()
if model.TotalPages != tt.initialTotal {
model.SetTotalPages(tt.initialTotal)
}
model.SetTotalPages(tt.items)
if model.TotalPages != tt.expected {
t.Errorf("TotalPages = %d, expected %d", model.TotalPages, tt.expected)
}
})
}
}

func TestPrevPage(t *testing.T) {
tests := []struct {
name string
totalPages int // Total pages to be set for the testcase
page int // intital page for test
expected int
}{
{"Go to previous page", 10, 1, 0},
{"Stay on first page", 5, 0, 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New()
model.SetTotalPages(tt.totalPages)
model.Page = tt.page

model, _ = model.Update(tea.KeyMsg{Type: tea.KeyLeft, Alt: false, Runes: []rune{}})
if model.Page != tt.expected {
t.Errorf("PrevPage() = %d, expected %d", model.Page, tt.expected)
}
})
}
}

func TestNextPage(t *testing.T) {
tests := []struct {
name string
totalPages int
page int
expected int
}{
{"Go to next page", 2, 0, 1},
{"Stay on last page", 2, 1, 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New()
model.SetTotalPages(tt.totalPages)
model.Page = tt.page

model, _ = model.Update(tea.KeyMsg{Type: tea.KeyRight, Alt: false, Runes: []rune{}})
if model.Page != tt.expected {
t.Errorf("NextPage() = %d, expected %d", model.Page, tt.expected)
}
})
}
}

func TestOnLastPage(t *testing.T) {
tests := []struct {
name string
page int
totalPages int
expected bool
}{
{"On last page", 1, 2, true},
{"Not on last page", 0, 2, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New()
model.SetTotalPages(tt.totalPages)
model.Page = tt.page

if result := model.OnLastPage(); result != tt.expected {
t.Errorf("OnLastPage() = %t, expected %t", result, tt.expected)
}
})
}
}

func TestOnFirstPage(t *testing.T) {
tests := []struct {
name string
page int
totalPages int
expected bool
}{
{"On first page", 0, 2, true},
{"Not on first page", 1, 2, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New()
model.SetTotalPages(tt.totalPages)
model.Page = tt.page

if result := model.OnFirstPage(); result != tt.expected {
t.Errorf("OnFirstPage() = %t, expected %t", result, tt.expected)
}
})
}
}

func TestItemsOnPage(t *testing.T) {
testCases := []struct {
currentPage int // current page to be set for the testcase
totalPages int // Total pages to be set for the testcase
totalItems int // Total items
expectedItems int // expected items on current page
}{
{1, 10, 10, 1},
{3, 10, 10, 1},
{7, 10, 10, 1},
}

for _, tc := range testCases {
model := New()
model.Page = tc.currentPage
model.SetTotalPages(tc.totalPages)
if actualItems := model.ItemsOnPage(tc.totalItems); actualItems != tc.expectedItems {
t.Errorf("ItemsOnPage() returned %d, expected %d for total items %d", actualItems, tc.expectedItems, tc.totalItems)
}
}
}
Loading