-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_component_use_test.go
More file actions
148 lines (128 loc) · 5.17 KB
/
node_component_use_test.go
File metadata and controls
148 lines (128 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gotmx
import (
"bytes"
"context"
"testing"
stringutils "github.com/authentic-devel/gotmx/utils"
)
func TestRenderOuterWorks(t *testing.T) {
template := `<div id="outer" data-g-define="my-template"><div id="inner">Hello World</div></div>`
expected := `<div id="outer"><div id="inner">Hello World</div></div>`
g, _, tl := initTestEngine()
if err := tl.LoadFromString(template, "dummy"); err != nil {
t.Error("Error parsing template: ", err)
return
}
component, err := g.createComponent("my-template", nil)
if err != nil {
t.Error("Expected component to exist, got error:", err)
return
}
buf := new(bytes.Buffer)
// Create a RenderContext from the Engine instance for rendering
// Uses context.Background() since tests don't need request cancellation
renderErr := component.Render(g.NewRenderContext(context.Background()), buf, RenderOuter)
if renderErr != nil {
t.Error("Error rendering component: ", renderErr)
return
}
result := buf.String()
compareStrings(result, expected, t)
}
func TestRenderInnerWorks(t *testing.T) {
template := `<div data-g-define="my-template"><div>Hello World</div></div>`
expected := `<div>Hello World</div>`
g, _, tl := initTestEngine()
if err := tl.LoadFromString(template, "dummy"); err != nil {
t.Error("Error parsing template: ", err)
return
}
component, err := g.createComponent("my-template", nil)
if err != nil {
t.Error("Expected component to exist, got error:", err)
return
}
buf := new(bytes.Buffer)
// Create a RenderContext from the Engine instance for rendering
// Uses context.Background() since tests don't need request cancellation
renderErr := component.Render(g.NewRenderContext(context.Background()), buf, RenderInner)
if renderErr != nil {
t.Error("Error rendering component: ", renderErr)
return
}
result := buf.String()
compareStrings(result, expected, t)
}
func TestRenderInnerWorksWithGInnerText(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template1">
|<div g-inner-use="my-template2"></div>
|</div>
|<div data-g-define id="my-template2" g-inner-text="Hello World from my-template2">Should get replaced</div>`)
expectation := stringutils.TrimMargin(
`<div>
|Hello World from my-template2
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template1", nil, expectation, t)
}
// Tests that g-inner-use works with g-outer-text defined on the target template.
// What is actually happening is:
// the target component is request to be rendered by using g-inner-use
// When the target component is actually rendered, it normally would skip the surrounding tag because of the g-inner-use.
// But it doesn't even get to that point, because the g-outer-text is evaluated before the element and its children
// are rendered. It has higher priority
func TestGINnerUseWorksWithTargetGOuterText(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template1">
|<div g-inner-use="my-template2"></div>
|</div>
|<div data-g-define id="my-template2" g-outer-text="Hello World from my-template2">Not relevant</div>`)
expectation := stringutils.TrimMargin(
`<div>
|Hello World from my-template2
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template1", nil, expectation, t)
}
// todo: re-implement support for golang templates
// Tests that a g-outer-repeat works as expected when a g-inner-use is on the same element.
// The expectation is, that the g-inner-use is repeated for each element in the model path, so calling the second
// template multiple times and every time only render its innerHTML.f
//func TestGInnerUseWorksWithGOuterRepeatOnSameElement(t *testing.T) {
// data := nodeComponentTestModel{
// StringSlice: []string{"One,", "Two,", "Three,"},
// }
// template := stringutils.TrimMargin(
// `<div data-g-define="my-template1">
// |<div g-inner-use="my-template2" g-outer-repeat="[[ .StringSlice ]]"></div>
// |</div>
// |<div data-g-define="my-template2" data-g-as-template>{{ . }}</div>`)
//
// expectation := stringutils.TrimMargin(
// `<div>
// |One,Two,Three,
// |</div>`)
// parseRenderAndCompareTemplate(nil, template, "my-template1", data, expectation, t)
//}
func TestGInnerUseWorksWithSlots(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template1" id="root">
|<div g-inner-use="my-template2">
| <article g-use-slot="main">Hello World in the middle</article>
|</div>
|</div>
|<div data-g-define="my-template2">
|Content before the slot
|<div data-g-define-slot="main"></div>
|Content after the slot
|</div>`)
expectation := stringutils.TrimMargin(
`<div id="root">
|
|Content before the slot
|<div><article>Hello World in the middle</article></div>
|Content after the slot
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template1", nil, expectation, t)
}
// todo: once we have inner-repeat, we also need to test that g-inner-use works with g-inner-repeat on the same element