-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathsend_handler.test.tsx
242 lines (205 loc) · 7.87 KB
/
send_handler.test.tsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {act, fireEvent, waitFor} from '@testing-library/react-native';
import React from 'react';
import {removeDraft} from '@actions/local/draft';
import {General} from '@constants';
import {DRAFT_TYPE_DRAFT, DRAFT_TYPE_SCHEDULED, type DraftType} from '@screens/global_drafts/constants';
import {renderWithEverything} from '@test/intl-test-helper';
import TestHelper from '@test/test_helper';
import {sendMessageWithAlert} from '@utils/post';
import SendHandler from './send_handler';
import type {Database} from '@nozbe/watermelondb';
jest.mock('@actions/remote/channel', () => ({
getChannelTimezones: jest.fn().mockResolvedValue({channelTimezones: []}),
}));
jest.mock('@utils/post', () => ({
sendMessageWithAlert: jest.fn(),
persistentNotificationsConfirmation: jest.fn(),
}));
jest.mock('@screens/navigation', () => ({
dismissBottomSheet: jest.fn(),
}));
jest.mock('@actions/local/draft', () => ({
removeDraft: jest.fn(),
}));
describe('components/post_draft/send_handler/SendHandler', () => {
let database: Database;
const baseProps = {
testID: 'test_send_handler',
channelId: 'channel-id',
channelType: General.OPEN_CHANNEL,
channelName: 'test-channel',
rootId: '',
currentUserId: 'current-user-id',
cursorPosition: 0,
enableConfirmNotificationsToChannel: true,
maxMessageLength: 4000,
membersCount: 3,
useChannelMentions: true,
userIsOutOfOffice: false,
customEmojis: [],
value: '',
files: [],
clearDraft: jest.fn(),
updateValue: jest.fn(),
updateCursorPosition: jest.fn(),
updatePostInputTop: jest.fn(),
addFiles: jest.fn(),
uploadFileError: null,
setIsFocused: jest.fn(),
persistentNotificationInterval: 0,
persistentNotificationMaxRecipients: 5,
postPriority: {
priority: 'urgent',
requested_ack: false,
persistent_notifications: false,
} as PostPriority,
};
beforeAll(async () => {
const server = await TestHelper.setupServerDatabase();
database = server.database;
});
beforeEach(() => {
jest.clearAllMocks();
});
it('should render DraftInput when not from draft view', () => {
const wrapper = renderWithEverything(
<SendHandler {...baseProps}/>, {database},
);
expect(wrapper.getByTestId('test_send_handler')).toBeTruthy();
});
it('should render SendDraft when from draft view', () => {
const props = {
...baseProps,
isFromDraftView: true,
draftType: DRAFT_TYPE_DRAFT as DraftType,
};
const wrapper = renderWithEverything(
<SendHandler {...props}/>, {database},
);
expect(wrapper.getByTestId('send_draft_button')).toBeTruthy();
expect(wrapper.getByText('Send draft')).toBeTruthy();
});
it('should render Send text when draft type is scheduled', () => {
const props = {
...baseProps,
isFromDraftView: true,
draftType: DRAFT_TYPE_SCHEDULED as DraftType,
};
const wrapper = renderWithEverything(
<SendHandler {...props}/>, {database},
);
expect(wrapper.getByTestId('send_draft_button')).toBeTruthy();
expect(wrapper.getByText('Send')).toBeTruthy();
});
it('should show correct post priority', async () => {
const wrapper = renderWithEverything(
<SendHandler
{...baseProps}
canShowPostPriority={true}
/>, {database},
);
const draftInput = wrapper.getByTestId('test_send_handler');
expect(draftInput).toBeTruthy();
const priority = wrapper.getByText('URGENT');
expect(priority).toBeTruthy();
});
it('should pass correct props to SendDraft component when in draft view', async () => {
const props = {
...baseProps,
isFromDraftView: true,
draftType: DRAFT_TYPE_DRAFT as DraftType,
channelDisplayName: 'Test Channel',
draftReceiverUserName: 'test-user',
postId: 'test-post-id',
value: 'test message',
};
const wrapper = renderWithEverything(
<SendHandler {...props}/>, {database},
);
// Verify the SendDraft button is rendered with correct text
const sendDraftButton = wrapper.getByTestId('send_draft_button');
expect(sendDraftButton).toBeTruthy();
expect(wrapper.getByText('Send draft')).toBeTruthy();
// Verify the button is enabled when there's a message (should be pressable)
fireEvent.press(sendDraftButton);
await waitFor(() => expect(sendMessageWithAlert).toHaveBeenCalled());
// Reset the mock for the next test
jest.clearAllMocks();
// Test with empty message to verify disabled state
const emptyProps = {
...props,
value: '',
files: [],
};
wrapper.rerender(
<SendHandler {...emptyProps}/>,
);
// Button should still exist but pressing it should not trigger send when empty
const emptyButton = wrapper.getByTestId('send_draft_button');
expect(emptyButton).toBeTruthy();
fireEvent.press(emptyButton);
expect(sendMessageWithAlert).not.toHaveBeenCalled();
});
it('should call sendMessageWithAlert with correct params when Send button clicked', async () => {
const props = {
...baseProps,
isFromDraftView: true,
draftType: DRAFT_TYPE_DRAFT as DraftType,
channelName: 'test-channel',
value: 'test message',
postPriority: {
persistent_notifications: false,
} as PostPriority,
};
const wrapper = renderWithEverything(
<SendHandler {...props}/>, {database},
);
const sendButton = wrapper.getByTestId('send_draft_button');
expect(sendButton).toBeTruthy();
await act(async () => {
fireEvent.press(sendButton);
});
await waitFor(() => {
expect(sendMessageWithAlert).toHaveBeenCalledWith(expect.objectContaining({
channelName: 'test-channel',
title: 'Send message now',
intl: expect.any(Object),
sendMessageHandler: expect.any(Function),
}));
});
});
it('should execute sendMessageHandler when send_draft_button is clicked', async () => {
// Mock implementation to capture the sendMessageHandler
let capturedHandler: Function;
jest.mocked(sendMessageWithAlert).mockImplementation((params) => {
capturedHandler = params.sendMessageHandler;
return Promise.resolve();
});
const props = {
...baseProps,
isFromDraftView: true,
draftType: DRAFT_TYPE_DRAFT as DraftType,
value: 'test message',
};
const wrapper = renderWithEverything(
<SendHandler {...props}/>, {database},
);
// Find and press the send button
const sendButton = wrapper.getByTestId('send_draft_button');
await act(async () => {
fireEvent.press(sendButton);
});
// Verify sendMessageWithAlert was called
expect(sendMessageWithAlert).toHaveBeenCalledWith(expect.objectContaining({
sendMessageHandler: expect.any(Function),
}));
// Now execute the captured handler to simulate user confirming the send
await act(async () => {
await capturedHandler();
});
// Varify removeDraft function is been called.
expect(removeDraft).toHaveBeenCalled();
});
});