25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

139 satır
3.3KB

  1. package handlers
  2. import (
  3. "testing"
  4. "qctextbuilder/internal/domain"
  5. )
  6. func TestExtractBlockID(t *testing.T) {
  7. t.Parallel()
  8. tests := []struct {
  9. name string
  10. f domain.TemplateField
  11. want string
  12. }{
  13. {
  14. name: "from key",
  15. f: domain.TemplateField{
  16. KeyName: "textTitle_m1710_1",
  17. Path: "text.textTitle_m1710_1",
  18. },
  19. want: "m1710",
  20. },
  21. {
  22. name: "from path",
  23. f: domain.TemplateField{
  24. KeyName: "servicesTitle_8",
  25. Path: "services.servicesTitle_r4830_8",
  26. },
  27. want: "r4830",
  28. },
  29. {
  30. name: "none",
  31. f: domain.TemplateField{
  32. KeyName: "plainTitle",
  33. Path: "text.plainTitle",
  34. },
  35. want: "",
  36. },
  37. }
  38. for _, tc := range tests {
  39. tc := tc
  40. t.Run(tc.name, func(t *testing.T) {
  41. t.Parallel()
  42. got := extractBlockID(tc.f)
  43. if got != tc.want {
  44. t.Fatalf("extractBlockID() = %q, want %q", got, tc.want)
  45. }
  46. })
  47. }
  48. }
  49. func TestApplyTextGroupingPrefersBlockID(t *testing.T) {
  50. t.Parallel()
  51. section := buildFieldSectionView{Key: "text", Title: "Text"}
  52. fields := []pendingField{
  53. {
  54. Field: domain.TemplateField{
  55. Section: "text",
  56. KeyName: "textTitle_m1710_1",
  57. Path: "text.textTitle_m1710_1",
  58. DisplayLabel: "text.textTitle_m1710_1",
  59. },
  60. View: buildFieldView{Path: "text.textTitle_m1710_1"},
  61. },
  62. {
  63. Field: domain.TemplateField{
  64. Section: "ext",
  65. KeyName: "textDescription_c7886_3",
  66. Path: "ext.textDescription_c7886_3",
  67. DisplayLabel: "ext.textDescription_c7886_3",
  68. },
  69. View: buildFieldView{Path: "ext.textDescription_c7886_3"},
  70. },
  71. {
  72. Field: domain.TemplateField{
  73. Section: "text",
  74. KeyName: "textDescription_m1710_2",
  75. Path: "text.textDescription_m1710_2",
  76. DisplayLabel: "text.textDescription_m1710_2",
  77. },
  78. View: buildFieldView{Path: "text.textDescription_m1710_2"},
  79. },
  80. }
  81. got := applyTextGrouping(section, fields)
  82. if len(got.EditableGroups) != 2 {
  83. t.Fatalf("expected 2 block groups, got %d", len(got.EditableGroups))
  84. }
  85. if got.EditableGroups[0].Title != "Hero / Haupttitel (m1710)" {
  86. t.Fatalf("unexpected first group title: %q", got.EditableGroups[0].Title)
  87. }
  88. if len(got.EditableGroups[0].Fields) != 2 {
  89. t.Fatalf("expected 2 fields in first group, got %d", len(got.EditableGroups[0].Fields))
  90. }
  91. if got.EditableGroups[1].Title != "Intro / Einleitung (c7886)" {
  92. t.Fatalf("unexpected second group title: %q", got.EditableGroups[1].Title)
  93. }
  94. }
  95. func TestPreferredBuildSectionUsesWebsiteSectionFirst(t *testing.T) {
  96. t.Parallel()
  97. field := domain.TemplateField{
  98. Section: "text",
  99. WebsiteSection: domain.WebsiteSectionCTA,
  100. KeyName: "textTitle",
  101. Path: "text.textTitle",
  102. }
  103. got := preferredBuildSection(field)
  104. if got != domain.WebsiteSectionCTA {
  105. t.Fatalf("preferredBuildSection() = %q, want %q", got, domain.WebsiteSectionCTA)
  106. }
  107. }
  108. func TestParseAutofillAction(t *testing.T) {
  109. t.Parallel()
  110. action, field := parseAutofillAction("apply_field::text.textTitle_m1710_1")
  111. if action != "apply_field" {
  112. t.Fatalf("expected action apply_field, got %q", action)
  113. }
  114. if field != "text.textTitle_m1710_1" {
  115. t.Fatalf("expected field path parsed, got %q", field)
  116. }
  117. }
  118. func TestFieldAnchorID(t *testing.T) {
  119. t.Parallel()
  120. got := fieldAnchorID("text.textTitle_m1710_1")
  121. if got != "field-text-texttitle-m1710-1" {
  122. t.Fatalf("unexpected anchor id: %q", got)
  123. }
  124. }