-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_attributes_typed_test.go
More file actions
84 lines (76 loc) · 2.14 KB
/
Copy patheval_attributes_typed_test.go
File metadata and controls
84 lines (76 loc) · 2.14 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
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
)
// Article is a sample model type to test with
type Article struct {
Title string
Body string
}
func TestVue_TypedAttributePassthrough(t *testing.T) {
tests := []struct {
name string
parent string
child string
data map[string]any
expected string
}{
{
name: "slice of structs preserves type through binding",
parent: `<template include="child.vuego" :articles="articles"></template>`,
child: `<template>{{ articles | type }}</template>`,
data: map[string]any{
"articles": []Article{
{Title: "Article 1", Body: "Content 1"},
{Title: "Article 2", Body: "Content 2"},
},
},
expected: `[]vuego_test.Article`,
},
{
name: "map preserves type through binding",
parent: `<template include="child.vuego" :data="myMap"></template>`,
child: `<template>{{ data | type }}</template>`,
data: map[string]any{
"myMap": map[string]string{"key": "value"},
},
expected: `map[string]string`,
},
{
name: "int preserves type through binding",
parent: `<template include="child.vuego" :count="count"></template>`,
child: `<template>{{ count | type }}</template>`,
data: map[string]any{
"count": 42,
},
expected: `int`,
},
{
name: "complex object type preservation",
parent: `<template include="child.vuego" :article="article"></template>`,
child: `<template>{{ article | type }}</template>`,
data: map[string]any{
"article": Article{Title: "Test", Body: "Test body"},
},
expected: `vuego_test.Article`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fs := fstest.MapFS{
"parent.vuego": &fstest.MapFile{Data: []byte(tc.parent)},
"child.vuego": &fstest.MapFile{Data: []byte(tc.child)},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "parent.vuego", tc.data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, tc.expected, "expected type %s but got %s", tc.expected, output)
})
}
}