flamingo.me/flamingo-commerce/v3@v3.11.0/category/application/breadcrumbservice_test.go (about) 1 package application_test 2 3 import ( 4 "context" 5 "net/url" 6 "testing" 7 8 "flamingo.me/flamingo-commerce/v3/breadcrumbs" 9 "flamingo.me/flamingo-commerce/v3/category/application" 10 "flamingo.me/flamingo-commerce/v3/category/domain" 11 "flamingo.me/flamingo/v3/framework/web" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type ( 16 MockRouter struct{} 17 ) 18 19 var ( 20 _ application.RouterRouter = (*MockRouter)(nil) 21 ) 22 23 func (r *MockRouter) URL(name string, params map[string]string) (*url.URL, error) { 24 return &url.URL{ 25 Path: "/foo", 26 }, nil 27 } 28 29 func TestBreadcrumbService_AddBreadcrumb(t *testing.T) { 30 type args struct { 31 category domain.Tree 32 } 33 34 controller := new(breadcrumbs.Controller) 35 36 tests := []struct { 37 name string 38 args args 39 want interface{} 40 }{ 41 { 42 name: "no category active", 43 args: args{ 44 category: getCategoryTreeWithoutActive(), 45 }, 46 want: []breadcrumbs.Crumb{}, 47 }, 48 { 49 name: "one category active", 50 args: args{ 51 category: getCategoryTreeWithSingleActive(), 52 }, 53 want: []breadcrumbs.Crumb{ 54 { 55 Title: "Root", 56 URL: "/foo", // hardcoded value, we test only for the title to be correct 57 Code: "root", 58 }, 59 }, 60 }, 61 { 62 name: "my funny full tree", 63 args: args{ 64 category: getFullCategoryTree(), 65 }, 66 want: []breadcrumbs.Crumb{ 67 { 68 Title: "Root", 69 URL: "/foo", // hardcoded value, we test only for the title to be correct 70 Code: "root", 71 }, 72 { 73 Title: "Sub1 Active", 74 URL: "/foo", // hardcoded value, we test only for the title to be correct 75 Code: "root-sub1-active", 76 }, 77 { 78 Title: "Sub2 Active", 79 URL: "/foo", // hardcoded value, we test only for the title to be correct 80 Code: "root-sub2-active", 81 }, 82 { 83 Title: "Sub3 Active", 84 URL: "/foo", // hardcoded value, we test only for the title to be correct 85 Code: "root-sub3-active", 86 }, 87 }, 88 }, 89 } 90 for _, tt := range tests { 91 t.Run(tt.name, func(t *testing.T) { 92 93 // Init context and add breadcrumb 94 bs := &application.BreadcrumbService{} 95 bs.Inject(&MockRouter{}) 96 request := web.CreateRequest(nil, nil) 97 ctx := web.ContextWithRequest(context.Background(), request) 98 bs.AddBreadcrumb(ctx, tt.args.category) 99 100 // get breadcrumb and validate it 101 breadcrumb := controller.Data(ctx, nil, nil) 102 103 assert.Equal(t, tt.want, breadcrumb) 104 105 }) 106 } 107 } 108 109 func getCategoryTreeWithoutActive() domain.Tree { 110 categoryRoot := domain.TreeData{ 111 CategoryCode: "root", 112 CategoryName: "Root", 113 IsActive: false, 114 SubTreesData: nil, 115 } 116 return categoryRoot 117 } 118 119 func getCategoryTreeWithSingleActive() domain.Tree { 120 categoryRoot := domain.TreeData{ 121 CategoryCode: "root", 122 CategoryName: "Root", 123 IsActive: true, 124 SubTreesData: nil, 125 } 126 return categoryRoot 127 } 128 129 func getFullCategoryTree() domain.Tree { 130 categoryRoot := domain.TreeData{ 131 CategoryCode: "root", 132 CategoryName: "Root", 133 IsActive: true, 134 SubTreesData: []*domain.TreeData{ 135 { 136 CategoryCode: "root-sub1-inactive", 137 CategoryName: "Sub1 Inactive", 138 IsActive: false, 139 SubTreesData: nil, 140 }, 141 { 142 CategoryCode: "root-sub1-active", 143 CategoryName: "Sub1 Active", 144 IsActive: true, 145 SubTreesData: []*domain.TreeData{ 146 { 147 CategoryCode: "root-sub2-active", 148 CategoryName: "Sub2 Active", 149 IsActive: true, 150 SubTreesData: []*domain.TreeData{ 151 { 152 CategoryCode: "root-sub3-active", 153 CategoryName: "Sub3 Active", 154 IsActive: true, 155 SubTreesData: nil, 156 }, 157 { 158 CategoryCode: "root-sub3-inactive", 159 CategoryName: "Sub3 Inactive", 160 IsActive: false, 161 SubTreesData: nil, 162 }, 163 }, 164 }, 165 { 166 CategoryCode: "root-sub2-inactive", 167 CategoryName: "Sub2 Inactive", 168 IsActive: false, 169 SubTreesData: nil, 170 }, 171 }, 172 }, 173 }, 174 } 175 return categoryRoot 176 }