2024-06-09 18:39:04 -07:00
|
|
|
package form
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/context"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
|
2024-06-09 18:39:04 -07:00
|
|
|
)
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
type mockForm struct {
|
|
|
|
called bool
|
|
|
|
Submission
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockForm) Submit(_ echo.Context, _ any) error {
|
|
|
|
m.called = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmit(t *testing.T) {
|
|
|
|
m := mockForm{}
|
|
|
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
|
|
|
err := Submit(ctx, &m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, m.called)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetClear(t *testing.T) {
|
2024-06-09 18:39:04 -07:00
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
type example struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("get empty context", func(t *testing.T) {
|
|
|
|
// Empty context, still return a form
|
|
|
|
ctx, _ := tests.NewContext(e, "/")
|
|
|
|
form := Get[example](ctx)
|
|
|
|
assert.NotNil(t, form)
|
|
|
|
})
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
t.Run("get non-empty context", func(t *testing.T) {
|
|
|
|
form := example{
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
ctx, _ := tests.NewContext(e, "/")
|
|
|
|
ctx.Set(context.FormKey, &form)
|
2024-06-09 18:39:04 -07:00
|
|
|
|
|
|
|
// Get again and expect the values were stored
|
|
|
|
got := Get[example](ctx)
|
|
|
|
require.NotNil(t, got)
|
2024-06-14 09:35:35 -07:00
|
|
|
assert.Equal(t, "test", form.Name)
|
2024-06-09 18:39:04 -07:00
|
|
|
|
|
|
|
// Clear
|
|
|
|
Clear(ctx)
|
|
|
|
got = Get[example](ctx)
|
|
|
|
require.NotNil(t, got)
|
|
|
|
assert.Empty(t, got.Name)
|
|
|
|
})
|
|
|
|
}
|