posts_test.gno

1.89 Kb ยท 63 lines
 1package minisocial
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils" // Provides testing utilities
 8	"gno.land/p/nt/uassert"
 9)
10
11func TestCreatePostSingle(t *testing.T) {
12	// Get a test address for alice
13	aliceAddr := testutils.TestAddress("alice")
14	// TestSetRealm sets the realm caller, in this case Alice
15	testing.SetRealm(testing.NewUserRealm(aliceAddr))
16
17	text1 := "Hello World!"
18	err := CreatePost(cross, text1)
19	uassert.True(t, err == nil, "expected no error")
20
21	// Get the rendered page
22	got := Render("")
23
24	// Content should have the text and alice's address in it
25	condition := strings.Contains(got, text1) && strings.Contains(got, aliceAddr.String())
26	uassert.True(t, condition, "expected render to contain text & alice's address")
27}
28
29func TestCreatePostMultiple(t *testing.T) {
30	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice")))
31
32	// Initialize a slice to hold the test posts and their authors
33	posts := []struct {
34		text   string
35		author string
36	}{
37		{"Hello World!", "alice"},
38		{"This is some new text!", "bob"},
39		{"Another post by alice", "alice"},
40		{"A post by charlie!", "charlie"},
41	}
42
43	for _, p := range posts {
44		// Set the appropriate caller realm based on the author
45		authorAddr := testutils.TestAddress(p.author)
46		testing.SetRealm(testing.NewUserRealm(authorAddr))
47
48		// Create the post
49		err := CreatePost(cross, p.text)
50		uassert.True(t, err == nil, "expected no error for post "+p.text)
51	}
52
53	// Get the rendered page
54	got := Render("")
55
56	// Check that all posts and their authors are present in the rendered output
57	for _, p := range posts {
58		expectedText := p.text
59		expectedAuthor := testutils.TestAddress(p.author).String() // Get the address for the author
60		condition := strings.Contains(got, expectedText) && strings.Contains(got, expectedAuthor)
61		uassert.True(t, condition, "expected render to contain text '"+expectedText+"' and address '"+expectedAuthor+"'")
62	}
63}