-
Notifications
You must be signed in to change notification settings - Fork 31
/
reaction_test.go
112 lines (90 loc) · 2.93 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
package stream_chat
import (
"context"
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleClient_SendReaction() {
client := &Client{}
msgID := "123"
userID := "bob-1"
ctx := context.Background()
reaction := &Reaction{
Type: "love",
ExtraData: map[string]interface{}{"my_custom_field": 123},
}
_, err := client.SendReaction(ctx, 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)
user := randomUser(t, c)
ctx := context.Background()
msg := &Message{
Text: "test message",
User: user,
}
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
reaction := Reaction{Type: "love"}
reactionResp, err := c.SendReaction(ctx, &reaction, resp.Message.ID, user.ID)
require.NoError(t, err, "send reaction")
assert.Equal(t, 1, reactionResp.Message.ReactionCounts[reaction.Type], "reaction count", reaction)
assert.Condition(t, reactionExistsCondition(reactionResp.Message.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 TestClient_DeleteReaction(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
user := randomUser(t, c)
ctx := context.Background()
msg := &Message{
Text: "test message",
User: user,
}
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
reaction := Reaction{Type: "love"}
reactionResp, err := c.SendReaction(ctx, &reaction, resp.Message.ID, user.ID)
require.NoError(t, err, "send reaction")
reactionResp, err = c.DeleteReaction(ctx, reactionResp.Message.ID, reaction.Type, user.ID)
require.NoError(t, err, "delete reaction")
assert.Equal(t, 0, reactionResp.Message.ReactionCounts[reaction.Type], "reaction count")
assert.Empty(t, reactionResp.Message.LatestReactions, "latest reactions empty")
}
func TestClient_GetReactions(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
user := randomUser(t, c)
ctx := context.Background()
msg := &Message{
Text: "test message",
User: user,
}
resp, err := ch.SendMessage(ctx, msg, user.ID)
require.NoError(t, err, "send message")
msg = resp.Message
reactionsResp, err := c.GetReactions(ctx, msg.ID, nil)
require.NoError(t, err, "get reactions")
assert.Empty(t, reactionsResp.Reactions, "reactions empty")
reaction := Reaction{Type: "love"}
reactionResp, err := c.SendReaction(ctx, &reaction, msg.ID, user.ID)
require.NoError(t, err, "send reaction")
reactionsResp, err = c.GetReactions(ctx, reactionResp.Message.ID, nil)
require.NoError(t, err, "get reactions")
assert.Condition(t, reactionExistsCondition(reactionsResp.Reactions, reaction.Type), "reaction exists")
}