Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

reactions_test.gno

2.46 Kb Β· 91 lines
 1package reactions
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils/v0"
 8	"gno.land/p/nt/uassert/v0"
 9)
10
11func TestReactAndRender(cur realm, t *testing.T) {
12	alice := testutils.TestAddress("alice")
13	bob := testutils.TestAddress("bob")
14
15	testing.SetRealm(testing.NewUserRealm(alice))
16	React(cross(cur),"launch", "πŸ‘")
17
18	testing.SetRealm(testing.NewUserRealm(bob))
19	React(cross(cur),"launch", "❀️")
20
21	if got := TopicCount(); got != 1 {
22		t.Fatalf("expected 1 topic, got %d", got)
23	}
24
25	out := Render("launch")
26	if !strings.Contains(out, "πŸ‘ 1") {
27		t.Fatalf("expected 'πŸ‘ 1' in render, got:\n%s", out)
28	}
29	if !strings.Contains(out, "❀️ 1") {
30		t.Fatalf("expected '❀️ 1' in render, got:\n%s", out)
31	}
32	if !strings.Contains(out, "Total reactions:** 2") {
33		t.Fatalf("expected total 2 in render, got:\n%s", out)
34	}
35}
36
37func TestOneReactionPerCaller(cur realm, t *testing.T) {
38	alice := testutils.TestAddress("alice")
39
40	testing.SetRealm(testing.NewUserRealm(alice))
41	React(cross(cur),"topicA", "πŸŽ‰")
42
43	// Second reaction by same caller to same topic must abort.
44	uassert.AbortsWithMessage(t, cur, "caller already reacted to this topic: topicA", func() {
45		React(cross(cur),"topicA", "πŸ˜€")
46	})
47}
48
49func TestCreateTopicAndDuplicate(cur realm, t *testing.T) {
50	dave := testutils.TestAddress("dave")
51	testing.SetRealm(testing.NewUserRealm(dave))
52
53	CreateTopic(cross(cur),"poll")
54	if !topics.Has("poll") {
55		t.Fatalf("expected topic 'poll' to exist")
56	}
57
58	uassert.AbortsWithMessage(t, cur, "topic already exists: poll", func() {
59		CreateTopic(cross(cur),"poll")
60	})
61
62	// A freshly created topic renders as having no reactions.
63	out := Render("poll")
64	if !strings.Contains(out, "no reactions yet") {
65		t.Fatalf("expected empty-topic notice, got:\n%s", out)
66	}
67}
68
69func TestTallyRowOrdering(cur realm, t *testing.T) {
70	a := testutils.TestAddress("u1")
71	b := testutils.TestAddress("u2")
72	c := testutils.TestAddress("u3")
73
74	testing.SetRealm(testing.NewUserRealm(a))
75	React(cross(cur),"party", "πŸŽ‰")
76	testing.SetRealm(testing.NewUserRealm(b))
77	React(cross(cur),"party", "πŸŽ‰")
78	testing.SetRealm(testing.NewUserRealm(c))
79	React(cross(cur),"party", "πŸ‘")
80
81	out := Render("party")
82	// πŸŽ‰ has 2, πŸ‘ has 1 β€” πŸŽ‰ must come first (count desc).
83	iParty := strings.Index(out, "πŸŽ‰ 2")
84	iThumb := strings.Index(out, "πŸ‘ 1")
85	if iParty < 0 || iThumb < 0 {
86		t.Fatalf("expected both tallies in render, got:\n%s", out)
87	}
88	if iParty > iThumb {
89		t.Fatalf("expected πŸŽ‰ before πŸ‘ by count, got:\n%s", out)
90	}
91}