Skip to content

Commit

Permalink
test: Add tests to the GetBreadcrumbs function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Jan 17, 2025
1 parent 200ee28 commit e504ead
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/api/utils/get-breadcrumbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
)

var apiGetEntryByUid = api.GetEntryByUid
var apiGetEntryByUrl = api.GetEntryByUrl

func GetBreadcrumbs(entry structs.Route) ([]structs.Route, error) {
results := []structs.Route{entry}
currentEntry := entry

var err error

for currentEntry.Parent != "" {
currentEntry, err = api.GetEntryByUid(currentEntry.Parent, entry.Locale, false)
currentEntry, err = apiGetEntryByUid(currentEntry.Parent, entry.Locale, false)

if err != nil {
continue
Expand All @@ -22,7 +25,7 @@ func GetBreadcrumbs(entry structs.Route) ([]structs.Route, error) {
}

if results[0].Url != "/" {
homeEntry, err := api.GetEntryByUrl("/", entry.Locale, false)
homeEntry, err := apiGetEntryByUrl("/", entry.Locale, false)

if err == nil {
results = append([]structs.Route{homeEntry}, results...)
Expand Down
85 changes: 85 additions & 0 deletions cmd/api/utils/get-breadcrumbs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package utils

import (
"errors"
"testing"

"github.com/Dobefu/csb/cmd/api"
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
"github.com/stretchr/testify/assert"
)

func TestGetBreadcrumbs(t *testing.T) {
apiGetEntryByUid = mockGetEntryByUid
apiGetEntryByUrl = mockGetEntryByUrl

defer func() {
apiGetEntryByUid = api.GetEntryByUid
apiGetEntryByUrl = api.GetEntryByUrl
}()

tests := []struct {
name string
entry structs.Route
expected []structs.Route
}{
{
name: "No parent",
entry: structs.Route{Uid: "entry1", Parent: "", Url: "/entry1", Locale: "en"},
expected: []structs.Route{
{Uid: "home", Parent: "", Url: "/", Locale: "en"},
{Uid: "entry1", Parent: "", Url: "/entry1", Locale: "en"},
},
},
{
name: "With parents",
entry: structs.Route{Uid: "entry2", Parent: "parent1", Url: "/entry2", Locale: "en"},
expected: []structs.Route{
{Uid: "home", Parent: "", Url: "/", Locale: "en"},
{Uid: "parent2", Parent: "", Url: "/parent2", Locale: "en"},
{Uid: "parent1", Parent: "parent2", Url: "/parent1", Locale: "en"},
{Uid: "entry2", Parent: "parent1", Url: "/entry2", Locale: "en"},
},
},
{
name: "Parent not found",
entry: structs.Route{Uid: "entry3", Parent: "nonexistent", Url: "/entry3", Locale: "en"},
expected: []structs.Route{
{Uid: "home", Parent: "", Url: "/", Locale: "en"},
{Uid: "entry3", Parent: "nonexistent", Url: "/entry3", Locale: "en"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := GetBreadcrumbs(tt.entry)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

func mockGetEntryByUid(uid, locale string, flag bool) (structs.Route, error) {
if uid == "parent1" {
return structs.Route{Uid: "parent1", Parent: "parent2", Url: "/parent1", Locale: locale}, nil
}

if uid == "parent2" {
return structs.Route{Uid: "parent2", Parent: "", Url: "/parent2", Locale: locale}, nil
}

if uid == "nonexistent" {
return structs.Route{}, errors.New("")
}

return structs.Route{}, errors.New("entry not found")
}

func mockGetEntryByUrl(url, locale string, flag bool) (structs.Route, error) {
if url == "/" {
return structs.Route{Uid: "home", Parent: "", Url: "/", Locale: locale}, nil
}

return structs.Route{}, errors.New("")
}

0 comments on commit e504ead

Please sign in to comment.