flamingo.me/flamingo-commerce/v3@v3.11.0/search/utils/pagination_test.go (about) 1 package utils 2 3 import ( 4 "log" 5 "net/url" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestBuildWith(t *testing.T) { 12 type args struct { 13 currentResult CurrentResultInfos 14 paginationConfig PaginationConfig 15 urlBase *url.URL 16 } 17 tests := []struct { 18 name string 19 args args 20 want PaginationInfo 21 }{ 22 { 23 name: "first page", 24 args: args{ 25 currentResult: CurrentResultInfos{ 26 TotalHits: 10, 27 PageSize: 10, 28 ActivePage: 1, 29 LastPage: 1, 30 }, 31 paginationConfig: PaginationConfig{ 32 ShowAroundActivePageAmount: 1, 33 ShowFirstPage: true, 34 ShowLastPage: true, 35 }, 36 urlBase: &url.URL{}, 37 }, 38 want: PaginationInfo{ 39 TotalHits: 10, 40 //No next oage 41 PageNavigation: []Page{ 42 { 43 Page: 1, 44 URL: makeURL(&url.URL{}, 1, ""), 45 IsActive: true, 46 }, 47 }, 48 }, 49 }, 50 { 51 name: "page 7", 52 args: args{ 53 currentResult: CurrentResultInfos{ 54 TotalHits: 100, 55 PageSize: 10, 56 ActivePage: 7, 57 LastPage: 10, 58 }, 59 paginationConfig: PaginationConfig{ 60 ShowAroundActivePageAmount: 3, 61 ShowFirstPage: false, 62 ShowLastPage: false, 63 }, 64 urlBase: &url.URL{}, 65 }, 66 want: PaginationInfo{ 67 TotalHits: 100, 68 NextPage: &Page{ 69 Page: 8, 70 URL: makeURL(&url.URL{}, 8, ""), 71 }, 72 PreviousPage: &Page{ 73 Page: 6, 74 URL: makeURL(&url.URL{}, 6, ""), 75 }, 76 //No next oage 77 PageNavigation: []Page{ 78 { 79 IsSpacer: true, 80 }, 81 { 82 Page: 4, 83 URL: makeURL(&url.URL{}, 4, ""), 84 }, 85 { 86 Page: 5, 87 URL: makeURL(&url.URL{}, 5, ""), 88 }, 89 { 90 Page: 6, 91 URL: makeURL(&url.URL{}, 6, ""), 92 }, 93 { 94 Page: 7, 95 URL: makeURL(&url.URL{}, 7, ""), 96 IsActive: true, 97 }, 98 { 99 Page: 8, 100 URL: makeURL(&url.URL{}, 8, ""), 101 }, 102 { 103 Page: 9, 104 URL: makeURL(&url.URL{}, 9, ""), 105 }, 106 }, 107 }, 108 }, 109 } 110 for _, tt := range tests { 111 t.Run(tt.name, func(t *testing.T) { 112 got := BuildWith(tt.args.currentResult, tt.args.paginationConfig, tt.args.urlBase) 113 log.Printf("%#v", got) 114 assert.Equal(t, tt.want, got) 115 }) 116 } 117 }