posts_test.gno

2.58 Kb ยท 89 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	// Initialize a slice to hold the test posts and their authors
31	posts := []struct {
32		text   string
33		author string
34	}{
35		{"Hello World!", "alice"},
36		{"This is some new text!", "bob"},
37		{"Another post by alice", "alice"},
38		{"A post by charlie!", "charlie"},
39	}
40
41	for _, p := range posts {
42		// Set the appropriate caller realm based on the author
43		authorAddr := testutils.TestAddress(p.author)
44		testing.SetRealm(testing.NewUserRealm(authorAddr))
45
46		// Create the post
47		err := CreatePost(cross, p.text)
48		uassert.True(t, err == nil, "expected no error for post "+p.text)
49	}
50
51	// Get the rendered page
52	got := Render("")
53
54	// Check that all posts and their authors are present in the rendered output
55	for _, p := range posts {
56		expectedText := p.text
57		expectedAuthor := testutils.TestAddress(p.author).String() // Get the address for the author
58		condition := strings.Contains(got, expectedText) && strings.Contains(got, expectedAuthor)
59		uassert.True(t, condition, "expected render to contain text '"+expectedText+"' and address '"+expectedAuthor+"'")
60	}
61}
62
63func TestReset(t *testing.T) {
64	aliceAddr := testutils.TestAddress("alice")
65	testing.SetRealm(testing.NewUserRealm(aliceAddr))
66
67	text1 := "Hello World!"
68	_ = CreatePost(cross, text1)
69
70	got := Render("")
71	uassert.True(t, strings.Contains(got, text1), "expected render to contain text1")
72
73	// Set admin
74	testing.SetRealm(testing.NewUserRealm(Ownable.Owner()))
75	ResetPosts(cross)
76
77	got = Render("")
78	uassert.False(t, strings.Contains(got, text1), "expected render to not contain text1")
79
80	text2 := "Some other Text!!"
81	_ = CreatePost(cross, text2)
82
83	got = Render("")
84	uassert.False(t, strings.Contains(got, text1), "expected render to not contain text1")
85
86	uassert.True(t, strings.Contains(got, text2), "expected render to contain text2")
87}
88
89// TODO: Add tests for Update & Delete