Skip to content

Commit

Permalink
fix(tests): added unit tests for RoutingOption
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Dec 25, 2024
1 parent e59aeee commit dda008c
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions routing_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ func TestRoutingOption(t *testing.T) {
}
})

app.Get("/", func(c *Context) error {
data := map[string]any{
"s1": c.Routing.Options.GetString("s1"),
"i1": c.Routing.Options.GetInt("i1"),
"name": c.Routing.Options.Get(NavigationName),
"icon": c.Routing.Options.Get(NavigationIcon),
"access": c.Routing.Options.Get(NavigationAccess),
}

return c.View(data)

}, WithMetadata("s1", "v1"),
WithMetadata("i1", 1))

app.Get("/admin", func(c *Context) error {

data := map[string]any{
Expand All @@ -34,14 +48,13 @@ func TestRoutingOption(t *testing.T) {
}

return c.View(data)
}, WithMetadata("s1", "v1"),
WithMetadata("i1", 1),
}, WithMetadata("i1", ""),
WithNavigation("admin", "ha-dash", "admin:view"))

app.Start()
defer app.Close()

req, err := http.NewRequest("GET", srv.URL+"/admin", nil)
req, err := http.NewRequest("GET", srv.URL+"/", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
Expand All @@ -50,14 +63,33 @@ func TestRoutingOption(t *testing.T) {
require.NoError(t, err)
resp.Body.Close()

data := make(map[string]any)
err = json.Unmarshal(buf, &data)
d1 := make(map[string]any)
err = json.Unmarshal(buf, &d1)
require.NoError(t, err)

require.Equal(t, "v1", d1["s1"])
require.EqualValues(t, 1, d1["i1"])
require.Nil(t, d1["name"])
require.Nil(t, d1["icon"])
require.Nil(t, d1["access"])

req, err = http.NewRequest("GET", srv.URL+"/admin", nil)
require.NoError(t, err)
resp, err = client.Do(req)
require.NoError(t, err)

buf, err = io.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()

d2 := make(map[string]any)
err = json.Unmarshal(buf, &d2)
require.NoError(t, err)

require.Equal(t, "v1", data["s1"])
require.EqualValues(t, 1, data["i1"])
require.Equal(t, "admin", data["name"])
require.Equal(t, "ha-dash", data["icon"])
require.Equal(t, "admin:view", data["access"])
require.Equal(t, "", d2["s1"])
require.EqualValues(t, 0, d2["i1"])
require.Equal(t, "admin", d2["name"])
require.Equal(t, "ha-dash", d2["icon"])
require.Equal(t, "admin:view", d2["access"])

}

0 comments on commit dda008c

Please sign in to comment.