Files
pagination/pagination_test.go
T

128 lines
3.8 KiB
Go
Raw Normal View History

2025-06-07 19:23:44 +02:00
package pagination
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetPage(t *testing.T) {
type args[T any] struct {
items []T
first *int
after *string
last *int
before *string
max int
fn func(T) string
}
type testCase[T any] struct {
name string
args args[T]
wantItems []T
wantPageInfo PageInfo
}
tests := []testCase[string]{
{
name: "nil items",
2025-06-07 19:23:44 +02:00
args: args[string]{max: 10},
wantItems: nil,
wantPageInfo: PageInfo{},
},
{
name: "empty items",
args: args[string]{items: []string{}, max: 10},
wantItems: nil,
wantPageInfo: PageInfo{},
},
2025-06-07 19:23:44 +02:00
{
name: "first 2",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"1", "2"},
wantPageInfo: PageInfo{
StartCursor: ptr("MQ=="),
HasNextPage: true,
EndCursor: ptr("Mg=="),
},
},
{
name: "no more items",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), after: ptr("Mw=="), max: 10, fn: func(s string) string { return s }},
wantItems: nil,
wantPageInfo: PageInfo{},
},
{
name: "first 10",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(10), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"1", "2", "3"},
wantPageInfo: PageInfo{
StartCursor: ptr("MQ=="),
HasNextPage: false,
EndCursor: ptr("Mw=="),
},
},
2025-06-07 19:23:44 +02:00
{
name: "after 2",
args: args[string]{items: []string{"1", "2", "3", "4"}, first: ptr(2), after: ptr("Mg=="), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"3", "4"},
wantPageInfo: PageInfo{
StartCursor: ptr("Mw=="),
HasNextPage: false,
HasPreviousPage: true,
EndCursor: ptr("NA=="),
},
},
{
name: "end of items",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), after: ptr("Mg=="), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"3"},
wantPageInfo: PageInfo{
StartCursor: ptr("Mw=="),
HasNextPage: false,
HasPreviousPage: true,
EndCursor: ptr("Mw=="),
},
},
{
name: "last 2",
args: args[string]{items: []string{"1", "2", "3"}, last: ptr(2), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"2", "3"},
wantPageInfo: PageInfo{
StartCursor: ptr("Mg=="),
HasNextPage: false,
HasPreviousPage: true,
EndCursor: ptr("Mw=="),
},
},
{
name: "before 3",
args: args[string]{items: []string{"1", "2", "3"}, last: ptr(2), before: ptr("Mw=="), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"1", "2"},
wantPageInfo: PageInfo{
StartCursor: ptr("MQ=="),
HasNextPage: true,
HasPreviousPage: false,
EndCursor: ptr("Mg=="),
},
},
{
name: "before 2",
args: args[string]{items: []string{"1", "2", "3"}, last: ptr(2), before: ptr("Mg=="), max: 10, fn: func(s string) string { return s }},
wantItems: []string{"1"},
wantPageInfo: PageInfo{
StartCursor: ptr("MQ=="),
HasNextPage: true,
HasPreviousPage: false,
EndCursor: ptr("MQ=="),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetPage(tt.args.items, tt.args.first, tt.args.after, tt.args.last, tt.args.before, tt.args.max, tt.args.fn)
assert.Equalf(t, tt.wantItems, got, "GetPage(%v, %v, %v, %v, %v, %v)", tt.args.items, tt.args.first, tt.args.after, tt.args.last, tt.args.before, tt.args.max)
assert.Equalf(t, tt.wantPageInfo, got1, "GetPage(%v, %v, %v, %v, %v, %v)", tt.args.items, tt.args.first, tt.args.after, tt.args.last, tt.args.before, tt.args.max)
})
}
}