-
Notifications
You must be signed in to change notification settings - Fork 11
/
widget_test.go
188 lines (161 loc) · 5.56 KB
/
widget_test.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package widget_test
import (
"fmt"
"html/template"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/fatih/color"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/test/utils"
"github.com/qor/widget"
)
var db *gorm.DB
var Widgets *widget.Widgets
var Admin *admin.Admin
var Server *httptest.Server
type bannerArgument struct {
Title string
SubTitle string
}
func init() {
db = utils.TestDB()
//db.LogMode(true)
}
// Runner
func setup() {
if err := db.DropTableIfExists(&widget.QorWidgetSetting{}).Error; err != nil {
panic(err)
}
db.AutoMigrate(&widget.QorWidgetSetting{})
mux := http.NewServeMux()
Server = httptest.NewServer(mux)
Widgets = widget.New(&widget.Config{
DB: db,
})
Widgets.RegisterViewPath("github.com/qor/widget/test")
Admin = admin.New(&qor.Config{DB: db})
Admin.AddResource(Widgets)
Admin.MountTo("/admin", mux)
Widgets.RegisterWidget(&widget.Widget{
Name: "Banner",
Templates: []string{"banner"},
Setting: Admin.NewResource(&bannerArgument{}),
Context: func(context *widget.Context, setting interface{}) *widget.Context {
if setting != nil {
argument := setting.(*bannerArgument)
context.Options["Title"] = argument.Title
context.Options["SubTitle"] = argument.SubTitle
}
return context
},
})
Widgets.RegisterScope(&widget.Scope{
Name: "From Google",
Visible: func(context *widget.Context) bool {
if request, ok := context.Get("Request"); ok {
_, ok := request.(*http.Request).URL.Query()["from_google"]
return ok
}
return false
},
})
Widgets.RegisterWidget(&widget.Widget{
Name: "NoTemplate",
Setting: Admin.NewResource(&bannerArgument{}),
Context: func(context *widget.Context, setting interface{}) *widget.Context {
context.Body = "<h1>My html</h1>"
return context
},
})
}
func reset() {
db.DropTable(&widget.QorWidgetSetting{})
db.AutoMigrate(&widget.QorWidgetSetting{})
setup()
}
// Test DB's record after call Render
func TestRenderRecord(t *testing.T) {
reset()
var count int
db.Model(&widget.QorWidgetSetting{}).Where(widget.QorWidgetSetting{Name: "HomeBanner", WidgetType: "Banner", Scope: "default", GroupName: "Banner"}).Count(&count)
if count != 0 {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render Record TestCase: should don't exist widget setting")))
}
widgetContext := Widgets.NewContext(&widget.Context{})
widgetContext.Render("HomeBanner", "Banner")
db.Model(&widget.QorWidgetSetting{}).Where(widget.QorWidgetSetting{Name: "HomeBanner", WidgetType: "Banner", Scope: "default", GroupName: "Banner"}).Count(&count)
if count == 0 {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render Record TestCase: should have default widget setting")))
}
http.PostForm(Server.URL+"/admin/widgets/HomeBanner",
url.Values{"_method": {"PUT"},
"QorResource.Scope": {"from_google"},
"QorResource.ActivatedAt": {"2016-07-14 10:10:42.433372925 +0800 CST"},
"QorResource.Widgets": {"Banner"},
"QorResource.Template": {"banner"},
"QorResource.Kind": {"Banner"},
})
db.Model(&widget.QorWidgetSetting{}).Where(widget.QorWidgetSetting{Name: "HomeBanner", WidgetType: "Banner", Scope: "from_google"}).Count(&count)
if count == 0 {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render Record TestCase: should have from_google widget setting")))
}
}
// Runner
func TestRenderContext(t *testing.T) {
reset()
setting := &widget.QorWidgetSetting{}
db.Where(widget.QorWidgetSetting{Name: "HomeBanner", WidgetType: "Banner", Scope: "default"}).FirstOrInit(setting)
db.Create(setting)
html := Widgets.Render("HomeBanner", "Banner")
if !strings.Contains(string(html), "Hello, \n<h1></h1>\n<h2></h2>\n") {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render TestCase #%d: Failure Result:\n %s\n", 1, html)))
}
widgetContext := Widgets.NewContext(&widget.Context{
Options: map[string]interface{}{"CurrentUser": "Qortex"},
})
html = widgetContext.Render("HomeBanner", "Banner")
if !strings.Contains(string(html), "Hello, Qortex\n<h1></h1>\n<h2></h2>\n") {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render TestCase #%d: Failure Result:\n %s\n", 2, html)))
}
db.Where(widget.QorWidgetSetting{Name: "HomeBanner", WidgetType: "Banner"}).FirstOrInit(setting)
setting.SetSerializableArgumentValue(&bannerArgument{Title: "Title", SubTitle: "SubTitle"})
err := db.Model(setting).Update(setting).Error
if err != nil {
panic(err)
}
html = widgetContext.Render("HomeBanner", "Banner")
if !strings.Contains(string(html), "Hello, Qortex\n<h1>Title</h1>\n<h2>SubTitle</h2>\n") {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render TestCase #%d: Failure Result:\n %s\n", 3, html)))
}
}
func TestRenderNoTemplate(t *testing.T) {
reset()
html := Widgets.Render("abc", "NoTemplate")
if !strings.Contains(string(html), "<h1>My html</h1>") {
t.Errorf(color.RedString(fmt.Sprintf("\nWidget Render TestCase #%d: Failure Result:\n %s\n", 5, html)))
}
}
func TestRegisterFuncMap(t *testing.T) {
func1 := func() {}
Widgets.RegisterFuncMap("func1", func1)
context := Widgets.NewContext(nil)
if _, ok := context.FuncMaps["func1"]; !ok {
t.Errorf("func1 should be assigned to context")
}
context2 := context.Funcs(template.FuncMap{"func2": func() {}})
if _, ok := context.FuncMaps["func2"]; !ok {
t.Errorf("func2 should be assigned to context")
}
if _, ok := context2.FuncMaps["func2"]; !ok {
t.Errorf("func2 should be assigned to context")
}
context3 := Widgets.NewContext(nil)
if _, ok := context3.FuncMaps["func3"]; ok {
t.Errorf("func3 should not be assigned to other contexts")
}
}