forms_test.gno

1.55 Kb ยท 47 lines
 1package forms
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/demo/testutils"
 8	"gno.land/p/demo/urequire"
 9)
10
11func TestGetFormByID(t *testing.T) {
12	alice := testutils.TestAddress("alice")
13	testing.SetOriginCaller(alice)
14	title := "Simple Form"
15	description := "This is a form"
16	openAt := "2021-01-01T00:00:00Z"
17	closeAt := "2021-01-02T00:00:00Z"
18	data := `[{"label":"Name","fieldType":"string","required":true},{"label":"Age","fieldType":"number","required":false},{"label":"Is this a test?","fieldType":"boolean","required":false},{"label":"Favorite Food","fieldType":"['Pizza', 'Schnitzel', 'Burger']","required":true},{"label":"Favorite Foods","fieldType":"{'Pizza', 'Schnitzel', 'Burger'}","required":true}]`
19
20	urequire.NotPanics(t, func() {
21		id := CreateForm(title, description, openAt, closeAt, data)
22
23		form := GetFormByID(id)
24
25		urequire.True(t, strings.Contains(form, data), "Form JSON was not rebuilt properly")
26	})
27}
28
29func TestGetForms(t *testing.T) {
30	alice := testutils.TestAddress("alice")
31	testing.SetOriginCaller(alice)
32	description := "This is a form"
33	openAt := "2021-01-01T00:00:00Z"
34	closeAt := "2021-01-02T00:00:00Z"
35
36	urequire.NotPanics(t, func() {
37		data1 := `[{"label":"Name","fieldType":"string","required":true}]`
38		CreateForm("NameForm", description, openAt, closeAt, data1)
39		data2 := `[{"label":"Age","fieldType":"number","required":false}]`
40		CreateForm("AgeForm", description, openAt, closeAt, data2)
41
42		forms := GetForms()
43
44		urequire.True(t, strings.Contains(forms, data1) && strings.Contains(forms, data2), "Forms JSON were not rebuilt properly")
45	})
46
47}