page_injector_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package web_test
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/qor5/web"
  7. h "github.com/theplant/htmlgo"
  8. "github.com/theplant/testingutils"
  9. )
  10. var cases = []struct {
  11. name string
  12. operation func(b *web.PageInjector)
  13. expected string
  14. }{
  15. {
  16. name: "title",
  17. operation: func(b *web.PageInjector) {
  18. b.Title("Hello")
  19. },
  20. expected: `<title>Hello</title>
  21. <meta charset='utf8'>
  22. <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
  23. `,
  24. },
  25. {
  26. name: "title and charset",
  27. operation: func(b *web.PageInjector) {
  28. b.Title("Hello")
  29. b.Meta(web.MetaKey("charset"), "charset", "shiftjis")
  30. },
  31. expected: `<title>Hello</title>
  32. <meta charset='shiftjis'>
  33. <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
  34. `,
  35. },
  36. {
  37. name: "title and charset double",
  38. operation: func(b *web.PageInjector) {
  39. b.Title("Hello")
  40. b.Meta(web.MetaKey("charset"), "charset", "shiftjis")
  41. b.Meta(web.MetaKey("charset"), "charset", "utf8")
  42. b.MetaNameContent("keywords", "Hello")
  43. },
  44. expected: `<title>Hello</title>
  45. <meta charset='utf8'>
  46. <meta name='keywords' content='Hello'>
  47. <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
  48. `,
  49. },
  50. {
  51. name: "script tag",
  52. operation: func(b *web.PageInjector) {
  53. b.HeadHTML(`
  54. <!-- Global site tag (gtag.js) - Google Analytics -->
  55. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123123-1"></script>
  56. <script>
  57. window.dataLayer = window.dataLayer || [];
  58. function gtag(){dataLayer.push(arguments);}
  59. gtag('js', new Date());
  60. gtag('config', 'UA-123123-1');
  61. </script>
  62. `)
  63. },
  64. expected: `
  65. <!-- Global site tag (gtag.js) - Google Analytics -->
  66. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123123-1"></script>
  67. <script>
  68. window.dataLayer = window.dataLayer || [];
  69. function gtag(){dataLayer.push(arguments);}
  70. gtag('js', new Date());
  71. gtag('config', 'UA-123123-1');
  72. </script>
  73. <meta charset='utf8'>
  74. <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
  75. `,
  76. },
  77. }
  78. func TestDefaultPageInjector(t *testing.T) {
  79. for _, c := range cases {
  80. t.Run(c.name, func(t *testing.T) {
  81. var b web.PageInjector
  82. c.operation(&b)
  83. web.SetDefault(&b, "")
  84. diff := testingutils.PrettyJsonDiff(
  85. strings.TrimSpace(c.expected),
  86. strings.TrimSpace(h.MustString(b.GetHeadHTMLComponent(), context.TODO())))
  87. if len(diff) > 0 {
  88. t.Error(diff)
  89. }
  90. })
  91. }
  92. }