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
|
package handlers
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"task-dashboard/internal/config"
)
func TestHandleDashboard_TabState(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
// Create a mock renderer that captures template name and writes real HTML
mock := &MockRenderer{
RenderFunc: func(w io.Writer, name string, data interface{}) error {
// Write minimal HTML to satisfy the test assertions
html := `<!DOCTYPE html>
<html>
<head></head>
<body>
<button class="tab-button tab-button-active" hx-get="/tabs/tasks">Tasks</button>
<button class="tab-button tab-button-active" hx-get="/tabs/planning">Planning</button>
<button class="tab-button tab-button-active" hx-get="/tabs/meals">Meals</button>
</body>
</html>`
_, err := w.Write([]byte(html))
return err
},
}
h := &Handler{
store: db,
renderer: mock,
todoistClient: &mockTodoistClient{},
trelloClient: &mockTrelloClient{},
config: &config.Config{
Port: "8080",
CacheTTLMinutes: 5,
},
}
tests := []struct {
name string
url string
expectedTab string
expectedActive string
expectedHxGet string
}{
{
name: "default to tasks tab",
url: "/",
expectedTab: "tasks",
expectedActive: `class="tab-button tab-button-active"`,
expectedHxGet: `hx-get="/tabs/tasks"`,
},
{
name: "planning tab from query param",
url: "/?tab=planning",
expectedTab: "planning",
expectedActive: `class="tab-button tab-button-active"`,
expectedHxGet: `hx-get="/tabs/planning"`,
},
{
name: "meals tab from query param",
url: "/?tab=meals",
expectedTab: "meals",
expectedActive: `class="tab-button tab-button-active"`,
expectedHxGet: `hx-get="/tabs/meals"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.url, nil)
w := httptest.NewRecorder()
h.HandleDashboard(w, req)
resp := w.Result()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
return
}
body := w.Body.String()
// Check that the expected tab has the active class
// We need to find the button with the expected tab name and check if it has the active class
if !strings.Contains(body, tt.expectedActive) {
t.Errorf("Expected active class in response body")
}
// Check that the content div loads the correct tab
if !strings.Contains(body, tt.expectedHxGet) {
t.Errorf("Expected hx-get=\"/tabs/%s\" in response body, got: %s", tt.expectedTab, body)
}
})
}
}
|