Skip to content

Commit 2e77662

Browse files
committed
Added initial tests for actions
1 parent 1c62572 commit 2e77662

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

internal/app/action/layout.go.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@
4949
</div>
5050
</div>
5151

52-
<div id="error_div" class="justify-center font-bold text-red-500">
53-
{{ if .Data.error }}
54-
Error : {{ .Data.error }}
55-
{{ end }}
56-
</div>
57-
5852
{{ end }} <!-- End of "header" block -->
5953

6054

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package app_test
2+
3+
import (
4+
"net/http/httptest"
5+
"path"
6+
"testing"
7+
8+
"github.com/claceio/clace/internal/app"
9+
"github.com/claceio/clace/internal/testutil"
10+
)
11+
12+
func actionTester(t *testing.T, rootPath bool, actionPath string) {
13+
t.Helper()
14+
logger := testutil.TestLogger()
15+
fileData := map[string]string{
16+
"app.star": `
17+
def handler(dry_run, args):
18+
return ace.result(status="done", values=["a", "b"])
19+
20+
app = ace.app("testApp",
21+
actions=[ace.action("testAction", "` + actionPath + `", handler)])
22+
23+
`,
24+
"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")`,
25+
}
26+
var a *app.App
27+
var err error
28+
if rootPath {
29+
a, _, err = CreateTestAppRoot(logger, fileData)
30+
} else {
31+
a, _, err = CreateTestApp(logger, fileData)
32+
}
33+
if err != nil {
34+
t.Fatalf("Error %s", err)
35+
}
36+
37+
var reqPath string
38+
if rootPath {
39+
reqPath = actionPath
40+
} else {
41+
reqPath = path.Join("/test", actionPath)
42+
}
43+
44+
request := httptest.NewRequest("GET", reqPath, nil)
45+
response := httptest.NewRecorder()
46+
a.ServeHTTP(response, request)
47+
48+
testutil.AssertEqualsInt(t, "code", 200, response.Code)
49+
testutil.AssertStringContains(t, response.Body.String(), "<title>testAction</title>")
50+
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)
51+
52+
request = httptest.NewRequest("POST", reqPath, nil)
53+
response = httptest.NewRecorder()
54+
a.ServeHTTP(response, request)
55+
testutil.AssertEqualsInt(t, "code", 200, response.Code)
56+
testutil.AssertStringMatch(t, "match response", response.Body.String(), `
57+
<div class="text-lg text-bold">
58+
done
59+
</div>
60+
61+
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
62+
<div class="divider text-lg text-secondary">Output</div>
63+
<textarea
64+
rows="20"
65+
class="textarea textarea-success w-full font-mono"
66+
readonly>a
67+
b
68+
</textarea
69+
>
70+
</div>
71+
`)
72+
73+
}
74+
75+
func TestRootAppRootAction(t *testing.T) {
76+
actionTester(t, true, "/")
77+
}
78+
func TestRootApp(t *testing.T) {
79+
actionTester(t, true, "/abc")
80+
}
81+
82+
func TestNonRootAppRootAction(t *testing.T) {
83+
actionTester(t, false, "/")
84+
}
85+
func TestNonRootApp(t *testing.T) {
86+
actionTester(t, false, "/abc")
87+
}

0 commit comments

Comments
 (0)