-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemes_test.go
More file actions
332 lines (310 loc) · 8.63 KB
/
themes_test.go
File metadata and controls
332 lines (310 loc) · 8.63 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package loops
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
const getThemeResponse = `{
"success": true,
"themeId": "thm_abc123",
"name": "Brand",
"styles": {
"backgroundColor": "#ffffff",
"bodyColor": "#fafafa",
"textBaseColor": "#111111",
"textBaseFontSize": 16,
"heading1Color": "#000000",
"heading1FontSize": 32,
"buttonBodyColor": "#0066ff",
"buttonTextColor": "#ffffff",
"borderRadius": 8
},
"isDefault": true,
"createdAt": "2026-04-01T10:00:00Z",
"updatedAt": "2026-04-02T10:00:00Z"
}`
const listThemesResponse = `{
"success": true,
"pagination": {
"totalResults": 2,
"returnedResults": 2,
"perPage": 20,
"totalPages": 1,
"nextCursor": "",
"nextPage": ""
},
"data": [
{
"themeId": "thm_1",
"name": "Brand",
"styles": {
"backgroundColor": "#ffffff",
"textLinkColor": "#0066ff"
},
"isDefault": true,
"createdAt": "2026-04-01T10:00:00Z",
"updatedAt": "2026-04-02T10:00:00Z"
},
{
"themeId": "thm_2",
"name": "Dark",
"styles": {
"backgroundColor": "#000000",
"textBaseColor": "#ffffff"
},
"isDefault": false,
"createdAt": "2026-03-01T10:00:00Z",
"updatedAt": "2026-03-05T10:00:00Z"
}
]
}`
func TestGetTheme(t *testing.T) {
tests := []struct {
name string
id string
statusCode int
body string
wantAPIErr *APIError
wantErrMsg string
wantID string
}{
{
name: "success",
id: "thm_abc123",
statusCode: http.StatusOK,
body: getThemeResponse,
wantID: "thm_abc123",
},
{
name: "not found",
id: "thm_missing",
statusCode: http.StatusNotFound,
body: `{"success":false,"message":"Theme not found"}`,
wantAPIErr: &APIError{StatusCode: http.StatusNotFound, Message: "Theme not found"},
},
{
name: "invalid id",
id: "bad",
statusCode: http.StatusBadRequest,
body: `{"success":false,"message":"Invalid themeId"}`,
wantAPIErr: &APIError{StatusCode: http.StatusBadRequest, Message: "Invalid themeId"},
},
{
name: "invalid json",
id: "thm_abc123",
statusCode: http.StatusOK,
body: `not json`,
wantErrMsg: "failed to decode response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPath string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
result, err := client.GetTheme(tt.id)
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if tt.wantErrMsg != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErrMsg)
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErrMsg)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := "/themes/" + tt.id; gotPath != want {
t.Errorf("path = %q, want %q", gotPath, want)
}
if result.ThemeID != tt.wantID {
t.Errorf("ThemeID = %q, want %q", result.ThemeID, tt.wantID)
}
if result.Name != "Brand" {
t.Errorf("Name = %q, want Brand", result.Name)
}
if !result.IsDefault {
t.Errorf("IsDefault = false, want true")
}
if result.Styles.BackgroundColor != "#ffffff" {
t.Errorf("Styles.BackgroundColor = %q, want #ffffff", result.Styles.BackgroundColor)
}
if result.Styles.TextBaseFontSize != 16 {
t.Errorf("Styles.TextBaseFontSize = %v, want 16", result.Styles.TextBaseFontSize)
}
if result.Styles.BorderRadius != 8 {
t.Errorf("Styles.BorderRadius = %v, want 8", result.Styles.BorderRadius)
}
})
}
}
func TestListThemes(t *testing.T) {
tests := []struct {
name string
statusCode int
body string
wantAPIErr *APIError
wantErrMsg string
wantCount int
}{
{
name: "success",
statusCode: http.StatusOK,
body: listThemesResponse,
wantCount: 2,
},
{
name: "empty list",
statusCode: http.StatusOK,
body: `{"success":true,"pagination":{"totalResults":0},"data":[]}`,
wantCount: 0,
},
{
name: "unauthorized",
statusCode: http.StatusUnauthorized,
body: `{"success":false,"error":"Invalid API key"}`,
wantAPIErr: &APIError{StatusCode: http.StatusUnauthorized, Message: "Invalid API key"},
},
{
name: "invalid perPage",
statusCode: http.StatusBadRequest,
body: `{"success":false,"message":"perPage must be between 10 and 50"}`,
wantAPIErr: &APIError{StatusCode: http.StatusBadRequest, Message: "perPage must be between 10 and 50"},
},
{
name: "invalid json",
statusCode: http.StatusOK,
body: `not json`,
wantErrMsg: "failed to decode response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
themes, pagination, err := client.ListThemes(PaginationParams{})
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
if tt.wantAPIErr.Message != "" && apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if tt.wantErrMsg != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErrMsg)
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErrMsg)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(themes) != tt.wantCount {
t.Errorf("len(themes) = %d, want %d", len(themes), tt.wantCount)
}
if pagination == nil {
t.Fatal("expected pagination, got nil")
}
})
}
}
func TestListThemes_ResponseData(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(listThemesResponse))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
themes, _, err := client.ListThemes(PaginationParams{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if themes[0].ThemeID != "thm_1" {
t.Errorf("ThemeID = %q, want thm_1", themes[0].ThemeID)
}
if !themes[0].IsDefault {
t.Errorf("themes[0].IsDefault = false, want true")
}
if themes[0].Styles.TextLinkColor != "#0066ff" {
t.Errorf("Styles.TextLinkColor = %q, want #0066ff", themes[0].Styles.TextLinkColor)
}
if themes[1].IsDefault {
t.Errorf("themes[1].IsDefault = true, want false")
}
if themes[1].Styles.BackgroundColor != "#000000" {
t.Errorf("Styles.BackgroundColor = %q, want #000000", themes[1].Styles.BackgroundColor)
}
}
func TestListThemes_QueryParams(t *testing.T) {
tests := []struct {
name string
params PaginationParams
wantPerPage string
wantCursor string
}{
{
name: "no params",
params: PaginationParams{},
},
{
name: "both params",
params: PaginationParams{PerPage: "10", Cursor: "xyz"},
wantPerPage: "10",
wantCursor: "xyz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPerPage, gotCursor string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPerPage = r.URL.Query().Get("perPage")
gotCursor = r.URL.Query().Get("cursor")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"pagination":{},"data":[]}`))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
client.ListThemes(tt.params)
if gotPerPage != tt.wantPerPage {
t.Errorf("perPage = %q, want %q", gotPerPage, tt.wantPerPage)
}
if gotCursor != tt.wantCursor {
t.Errorf("cursor = %q, want %q", gotCursor, tt.wantCursor)
}
})
}
}