feat: initial version
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
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: "empty",
|
||||
args: args[string]{max: 10},
|
||||
wantItems: nil,
|
||||
wantPageInfo: PageInfo{},
|
||||
},
|
||||
{
|
||||
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: "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)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user