-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_for_test.go
More file actions
120 lines (112 loc) · 3.68 KB
/
Copy patheval_for_test.go
File metadata and controls
120 lines (112 loc) · 3.68 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
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestVue_EvalFor(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "simple v-for with item variable",
template: "<ul><li v-for=\"item in items\">{{ item }}</li></ul>",
data: map[string]any{"items": []string{"a", "b", "c"}},
expected: "<ul><li>a</li><li>b</li><li>c</li></ul>",
},
{
name: "v-for with index and value",
template: "<ul><li v-for=\"(i, item) in items\">{{ i }}: {{ item }}</li></ul>",
data: map[string]any{"items": []string{"a", "b"}},
expected: "<ul><li>0: a</li><li>1: b</li></ul>",
},
{
name: "v-for with nested content",
template: "<div v-for=\"item in items\"><span>{{ item.name }}</span></div>",
data: map[string]any{"items": []map[string]any{
{"name": "Alice"},
{"name": "Bob"},
}},
expected: "<div><span>Alice</span></div><div><span>Bob</span></div>",
},
{
name: "v-for with empty list",
template: "<ul><li v-for=\"item in items\">{{ item }}</li></ul>",
data: map[string]any{"items": []string{}},
expected: "<ul></ul>",
},
{
name: "v-for with single item",
template: "<div v-for=\"item in items\">{{ item }}</div>",
data: map[string]any{"items": []int{42}},
expected: "<div>42</div>",
},
{
name: "v-for with numbers",
template: "<span v-for=\"n in nums\">{{ n }}</span>",
data: map[string]any{"nums": []int{1, 2, 3}},
expected: "<span>1</span><span>2</span><span>3</span>",
},
{
name: "v-for with nested loops",
template: "<div v-for=\"item in items\"><span v-for=\"tag in item.tags\">{{ tag }}</span></div>",
data: map[string]any{"items": []map[string]any{
{"tags": []string{"a", "b"}},
{"tags": []string{"c"}},
}},
expected: "<div><span>a</span><span>b</span></div><div><span>c</span></div>",
},
{
name: "v-for removes v-for attribute",
template: "<li v-for=\"item in items\" v-for=\"skip-this\">{{ item }}</li>",
data: map[string]any{"items": []string{"x"}},
expected: "<li>x</li>",
},
{
name: "v-for with struct field access and negation",
template: "<span v-for=\"item in navigation.items\"><button v-if=\"!item.disabled\">{{ item.name }}</button></span>",
data: map[string]any{
"navigation": map[string]any{
"items": []map[string]any{
{"name": "Page 1", "disabled": false},
{"name": "Page 2", "disabled": true},
{"name": "Page 3", "disabled": false},
},
},
},
expected: "<span><button>Page 1</button></span><span></span><span><button>Page 3</button></span>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestParseFor(t *testing.T) {
// Note: parseFor is not exported, so we test it indirectly through eval
// This test ensures v-for parsing works correctly
template := []byte("<div v-for=\"item in items\">{{ item }}</div>")
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", map[string]any{"items": []string{"a"}})
require.NoError(t, err)
diff.EqualHTML(t, []byte("<div>a</div>"), buf.Bytes(), nil, nil)
}