Skip to content

Commit

Permalink
Added initial tests for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Oct 19, 2024
1 parent 1c62572 commit 2e77662
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
6 changes: 0 additions & 6 deletions internal/app/action/layout.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
</div>
</div>

<div id="error_div" class="justify-center font-bold text-red-500">
{{ if .Data.error }}
Error : {{ .Data.error }}
{{ end }}
</div>

{{ end }} <!-- End of "header" block -->


Expand Down
87 changes: 87 additions & 0 deletions internal/app/tests/appaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package app_test

import (
"net/http/httptest"
"path"
"testing"

"github.com/claceio/clace/internal/app"
"github.com/claceio/clace/internal/testutil"
)

func actionTester(t *testing.T, rootPath bool, actionPath string) {
t.Helper()
logger := testutil.TestLogger()
fileData := map[string]string{
"app.star": `
def handler(dry_run, args):
return ace.result(status="done", values=["a", "b"])
app = ace.app("testApp",
actions=[ace.action("testAction", "` + actionPath + `", handler)])
`,
"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")`,
}
var a *app.App
var err error
if rootPath {
a, _, err = CreateTestAppRoot(logger, fileData)
} else {
a, _, err = CreateTestApp(logger, fileData)
}
if err != nil {
t.Fatalf("Error %s", err)
}

var reqPath string
if rootPath {
reqPath = actionPath
} else {
reqPath = path.Join("/test", actionPath)
}

request := httptest.NewRequest("GET", reqPath, nil)
response := httptest.NewRecorder()
a.ServeHTTP(response, request)

testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringContains(t, response.Body.String(), "<title>testAction</title>")
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`)

request = httptest.NewRequest("POST", reqPath, nil)
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringMatch(t, "match response", response.Body.String(), `
<div class="text-lg text-bold">
done
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Output</div>
<textarea
rows="20"
class="textarea textarea-success w-full font-mono"
readonly>a
b
</textarea
>
</div>
`)

}

func TestRootAppRootAction(t *testing.T) {
actionTester(t, true, "/")
}
func TestRootApp(t *testing.T) {
actionTester(t, true, "/abc")
}

func TestNonRootAppRootAction(t *testing.T) {
actionTester(t, false, "/")
}
func TestNonRootApp(t *testing.T) {
actionTester(t, false, "/abc")
}

0 comments on commit 2e77662

Please sign in to comment.