presets_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package presets
  2. import (
  3. "net/http"
  4. "net/url"
  5. "strconv"
  6. "testing"
  7. "github.com/qor5/web"
  8. )
  9. func TestIsMenuItemActive(t *testing.T) {
  10. cases := []struct {
  11. // path means current url path
  12. path string
  13. // link means menu item link
  14. link string
  15. excepted bool
  16. }{
  17. {"", "/", true},
  18. {"/", "/", true},
  19. {"/", "/order", false},
  20. {"/order", "/order", true},
  21. {"/order/1", "/order", true},
  22. {"/order#", "/order", true},
  23. {"/product", "/order", false},
  24. {"/product", "/", false},
  25. }
  26. type io struct {
  27. ctx *web.EventContext
  28. m *ModelBuilder
  29. excepted bool
  30. }
  31. var toIO []io
  32. b := New()
  33. for _, c := range cases {
  34. toIO = append(toIO, io{
  35. ctx: &web.EventContext{
  36. R: &http.Request{
  37. URL: &url.URL{
  38. Path: c.path,
  39. },
  40. },
  41. },
  42. m: &ModelBuilder{
  43. link: c.link,
  44. modelInfo: &ModelInfo{mb: NewModelBuilder(b, &struct{}{})},
  45. },
  46. excepted: c.excepted,
  47. })
  48. }
  49. for i, io := range toIO {
  50. t.Run(strconv.Itoa(i), func(t *testing.T) {
  51. if b.isMenuItemActive(io.ctx, io.m) != io.excepted {
  52. t.Errorf("isMenuItemActive() = %v, excepted %v", b.isMenuItemActive(io.ctx, io.m), io.excepted)
  53. }
  54. })
  55. }
  56. }