-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathPollActions.test.js
More file actions
286 lines (263 loc) Β· 10.5 KB
/
PollActions.test.js
File metadata and controls
286 lines (263 loc) Β· 10.5 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React from 'react';
import { Poll } from 'stream-chat';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { PollActions } from '../PollActions';
import {
ChannelStateProvider,
ChatProvider,
MessageProvider,
PollProvider,
TranslationProvider,
} from '../../../context';
import {
generateMessage,
generatePoll,
generateUser,
getTestClientWithUser,
} from '../../../mock-builders';
import { MAX_OPTIONS_DISPLAYED } from '../constants';
const SEE_ALL_OPTIONS_ACTION_TEXT = 'See all options ({{count}})';
const SUGGEST_OPTION_ACTION_TEXT = 'Suggest an option';
const UPDATE_COMMENT_ACTION_TEXT = 'Update your comment';
const VIEW_COMMENTS_ACTION_TEXT = 'View {{count}} comments';
const VIEW_RESULTS_ACTION_TEXT = 'View results';
const END_VOTE_ACTION_TEXT = 'End vote';
const t = (v) => v;
const defaultChannelStateContext = {
channelCapabilities: { 'cast-poll-vote': true, 'query-poll-votes': true },
};
const defaultMessageContext = {
message: generateMessage(),
};
const renderComponent = async ({
channelStateContext,
client: customClient,
messageContext,
poll,
props,
}) => {
const client = customClient ?? (await getTestClientWithUser());
return render(
<ChatProvider value={{ client }}>
<TranslationProvider value={{ t }}>
<ChannelStateProvider
value={{ ...defaultChannelStateContext, ...channelStateContext }}
>
<MessageProvider value={{ ...defaultMessageContext, ...messageContext }}>
<PollProvider poll={poll}>
<PollActions {...props} />
</PollProvider>
</MessageProvider>
</ChannelStateProvider>
</TranslationProvider>
</ChatProvider>,
);
};
describe('PollActions', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('shows "See all options ({{count}})" action if option count is above MAX_OPTIONS_DISPLAYED', async () => {
const pollData = generatePoll({
options: Array.from({ length: MAX_OPTIONS_DISPLAYED + 1 }, (_, i) => ({
id: i.toString(),
text: i.toString(),
})),
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.getByText(SEE_ALL_OPTIONS_ACTION_TEXT)).toBeInTheDocument();
});
it('hides "See all options ({{count}})" action if option count is below MAX_OPTIONS_DISPLAYED', async () => {
const pollData = generatePoll({
options: Array.from({ length: MAX_OPTIONS_DISPLAYED }, (_, i) => ({
id: i.toString(),
text: i.toString(),
})),
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(SEE_ALL_OPTIONS_ACTION_TEXT)).not.toBeInTheDocument();
});
it('does not show "Suggest an option" action if poll is not closed and suggestions are allowed but user does not have permission to cast vote', async () => {
const pollData = generatePoll({
allow_user_suggested_options: true,
is_closed: false,
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({
channelStateContext: { channelCapabilities: { 'cast-poll-vote': false } },
poll,
});
expect(screen.queryByText(SUGGEST_OPTION_ACTION_TEXT)).not.toBeInTheDocument();
});
it('shows "Suggest an option" action if poll is not closed and suggestions are allowed and user has permission to cast votes', async () => {
const pollData = generatePoll({
allow_user_suggested_options: true,
is_closed: false,
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.getByText(SUGGEST_OPTION_ACTION_TEXT)).toBeInTheDocument();
});
it('hides "Suggest an option" action if poll is closed', async () => {
const pollData = generatePoll({
allow_user_suggested_options: true,
is_closed: true,
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(SUGGEST_OPTION_ACTION_TEXT)).not.toBeInTheDocument();
});
it('hides "Suggest an option" action if suggestions are not allowed', async () => {
const pollData = generatePoll({
allow_user_suggested_options: false,
is_closed: false,
});
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(SUGGEST_OPTION_ACTION_TEXT)).not.toBeInTheDocument();
});
it('shows "Update your comment" action', async () => {
const pollData = generatePoll({ allow_answers: true, is_closed: false });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.getByText(UPDATE_COMMENT_ACTION_TEXT)).toBeInTheDocument();
});
it('hides "Update your comment" action if poll is closed', async () => {
const pollData = generatePoll({ allow_answers: true, is_closed: true });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(UPDATE_COMMENT_ACTION_TEXT)).not.toBeInTheDocument();
});
it('hides "Update your comment" action if answers are not allowed', async () => {
const pollData = generatePoll({ allow_answers: false, is_closed: false });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(UPDATE_COMMENT_ACTION_TEXT)).not.toBeInTheDocument();
});
it('shows "View {{count}} comments" action if answers exist and query-poll-votes permission is granted', async () => {
const pollData = generatePoll({ answers_count: 1 });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.getByText(VIEW_COMMENTS_ACTION_TEXT)).toBeInTheDocument();
});
it('hides "View {{count}} comments" action if there are no answers', async () => {
const pollData = generatePoll({ answers_count: 0 });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.queryByText(VIEW_COMMENTS_ACTION_TEXT)).not.toBeInTheDocument();
});
it('hides "View {{count}} comments" action if the query-poll-votes permission is not granted', async () => {
const pollData = generatePoll({ answers_count: 1 });
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({
channelStateContext: { channelCapabilities: { 'query-poll-votes': false } },
poll,
});
expect(screen.queryByText(VIEW_COMMENTS_ACTION_TEXT)).not.toBeInTheDocument();
});
it('shows "View results" action', async () => {
const pollData = generatePoll();
const poll = new Poll({ client: {}, poll: pollData });
await renderComponent({ poll });
expect(screen.getByText(VIEW_RESULTS_ACTION_TEXT)).toBeInTheDocument();
});
it('shows "End vote" action if not closed already and the poll is own', async () => {
const user = generateUser();
const client = await getTestClientWithUser(user);
const pollData = generatePoll({ created_by_id: user.id, is_closed: false });
const poll = new Poll({ client, poll: pollData });
await renderComponent({ client, poll });
expect(screen.getByText(END_VOTE_ACTION_TEXT)).toBeInTheDocument();
});
it('hides "End vote" action if poll is closed', async () => {
const user = generateUser();
const client = await getTestClientWithUser(user);
const pollData = generatePoll({ created_by_id: user.id, is_closed: true });
const poll = new Poll({ client, poll: pollData });
await renderComponent({ client, poll });
expect(screen.queryByText(END_VOTE_ACTION_TEXT)).not.toBeInTheDocument();
});
it('hides "End vote" action if the poll is not own', async () => {
const user = generateUser();
const client = await getTestClientWithUser(user);
const pollData = generatePoll({ is_closed: false });
const poll = new Poll({ client, poll: pollData });
await renderComponent({ client, poll });
expect(screen.queryByText(END_VOTE_ACTION_TEXT)).not.toBeInTheDocument();
});
it('allows custom actions contents overrides', async () => {
const user = generateUser();
const client = await getTestClientWithUser(user);
const pollData = generatePoll({
allow_answers: true,
allow_user_suggested_options: true,
answers_count: 1,
created_by_id: user.id,
is_closed: false,
options: Array.from({ length: MAX_OPTIONS_DISPLAYED + 1 }, (_, i) => ({
id: i.toString(),
text: i.toString(),
})),
});
const poll = new Poll({ client, poll: pollData });
const PollOptionsFullList = () => <div data-testid='poll-options-full-list-custom' />;
const SuggestPollOptionForm = () => (
<div data-testid='suggest-poll-option-form-custom' />
);
const AddCommentForm = () => <div data-testid='add-comment-form-custom' />;
const PollAnswerList = () => <div data-testid='poll-answer-list-custom' />;
const PollResults = () => <div data-testid='poll-results-custom' />;
const EndPollDialog = () => <div data-testid='end-poll-dialog-custom' />;
await renderComponent({
client,
poll,
props: {
AddCommentForm,
EndPollDialog,
PollAnswerList,
PollOptionsFullList,
PollResults,
SuggestPollOptionForm,
},
});
act(() => {
fireEvent.click(screen.getByText(SEE_ALL_OPTIONS_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('poll-options-full-list-custom')).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(SUGGEST_OPTION_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('suggest-poll-option-form-custom')).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(UPDATE_COMMENT_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('add-comment-form-custom')).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(VIEW_COMMENTS_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('poll-answer-list-custom')).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(VIEW_RESULTS_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('poll-results-custom')).toBeInTheDocument();
});
act(() => {
fireEvent.click(screen.getByText(END_VOTE_ACTION_TEXT));
});
await waitFor(() => {
expect(screen.getByTestId('end-poll-dialog-custom')).toBeInTheDocument();
});
});
});