39 lines
1.3 KiB
Go
39 lines
1.3 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPaginateCollectionPreservesQueryParameters(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodGet, "/g/3/plants?q=tomate&sort=name_asc&page=2", nil)
|
|
request.AddCookie(&http.Cookie{Name: "gardomatic.entries-per-page", Value: "10"})
|
|
values := make([]int, 25)
|
|
for index := range values {
|
|
values[index] = index + 1
|
|
}
|
|
|
|
page, pagination := paginateCollection(request, values)
|
|
if len(page) != 10 || page[0] != 11 || page[9] != 20 {
|
|
t.Fatalf("page = %v, want values 11 through 20", page)
|
|
}
|
|
if pagination == nil || pagination.Page != 2 || pagination.TotalPages != 3 {
|
|
t.Fatalf("pagination = %+v, want page 2 of 3", pagination)
|
|
}
|
|
for _, pageURL := range []string{pagination.PreviousURL, pagination.NextURL} {
|
|
if !strings.Contains(pageURL, "q=tomate") || !strings.Contains(pageURL, "sort=name_asc") {
|
|
t.Fatalf("pagination URL does not preserve filters: %q", pageURL)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPaginateCollectionOmitsNavigationForOnePage(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodGet, "/gardens", nil)
|
|
page, pagination := paginateCollection(request, []int{1, 2})
|
|
if len(page) != 2 || pagination != nil {
|
|
t.Fatalf("page = %v, pagination = %+v; want unmodified page without navigation", page, pagination)
|
|
}
|
|
}
|