forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaction_test.go
117 lines (93 loc) · 2.82 KB
/
reaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package stream_chat // nolint: golint
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleChannel_SendReaction() {
channel := &Channel{}
msgID := "123"
userID := "bob-1"
reaction := &Reaction{
Type: "love",
ExtraData: map[string]interface{}{"my_custom_field": 123},
}
_, err := channel.SendReaction(reaction, msgID, userID)
if err != nil {
log.Fatalf("Found Error: %v", err)
}
}
func TestChannel_SendReaction(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
require.NoError(t, ch.Delete(), "delete channel")
}()
user := randomUser()
msg := &Message{
Text: "test message",
User: user,
}
msg, err := ch.SendMessage(msg, serverUser.ID)
require.NoError(t, err, "send message")
reaction := Reaction{Type: "love"}
msg, err = ch.SendReaction(&reaction, msg.ID, serverUser.ID)
require.NoError(t, err, "send reaction")
assert.Equal(t, 1, msg.ReactionCounts[reaction.Type], "reaction count", reaction)
assert.Condition(t, reactionExistsCondition(msg.LatestReactions, reaction.Type), "latest reaction exists")
}
func reactionExistsCondition(reactions []*Reaction, searchType string) func() bool {
return func() bool {
for _, r := range reactions {
if r.Type == searchType {
return true
}
}
return false
}
}
func TestChannel_DeleteReaction(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
require.NoError(t, ch.Delete(), "delete channel")
}()
user := randomUser()
msg := &Message{
Text: "test message",
User: user,
}
msg, err := ch.SendMessage(msg, serverUser.ID)
require.NoError(t, err, "send message")
reaction := Reaction{Type: "love"}
msg, err = ch.SendReaction(&reaction, msg.ID, serverUser.ID)
require.NoError(t, err, "send reaction")
msg, err = ch.DeleteReaction(msg.ID, reaction.Type, serverUser.ID)
require.NoError(t, err, "delete reaction")
assert.Equal(t, 0, msg.ReactionCounts[reaction.Type], "reaction count")
assert.Empty(t, msg.LatestReactions, "latest reactions empty")
}
func TestChannel_GetReactions(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
defer func() {
require.NoError(t, ch.Delete(), "delete channel")
}()
user := randomUser()
msg := &Message{
Text: "test message",
User: user,
}
msg, err := ch.SendMessage(msg, serverUser.ID)
require.NoError(t, err, "send message")
reactions, err := ch.GetReactions(msg.ID, nil)
require.NoError(t, err, "get reactions")
assert.Empty(t, reactions, "reactions empty")
reaction := Reaction{Type: "love"}
msg, err = ch.SendReaction(&reaction, msg.ID, serverUser.ID)
require.NoError(t, err, "send reaction")
reactions, err = ch.GetReactions(msg.ID, nil)
require.NoError(t, err, "get reactions")
assert.Condition(t, reactionExistsCondition(reactions, reaction.Type), "reaction exists")
}