Skip to content

Commit 2a6f22b

Browse files
committed
fix: Messages containing only space characters can't be sent #253
1 parent 14ef557 commit 2a6f22b

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

projects/stream-chat-angular/src/lib/message-input/message-input.component.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,36 @@ describe('MessageInputComponent', () => {
441441
expect(sendMessageSpy).not.toHaveBeenCalled();
442442
});
443443

444+
it(`shouldn't send messages with only spaces`, () => {
445+
const textarea = queryTextarea();
446+
component.textareaValue = ' '; // single space
447+
textarea?.send.next();
448+
fixture.detectChanges();
449+
450+
expect(sendMessageSpy).not.toHaveBeenCalled();
451+
});
452+
453+
it(`should remove text if attachments are uploaded, but text contains only space characters`, () => {
454+
attachmentService.mapToAttachments.and.returnValue([
455+
{
456+
type: 'image',
457+
img_url: 'url',
458+
},
459+
]);
460+
const textarea = queryTextarea();
461+
component.textareaValue = ' '; // multiple spaces
462+
textarea?.send.next();
463+
fixture.detectChanges();
464+
465+
expect(sendMessageSpy).toHaveBeenCalledWith(
466+
'',
467+
jasmine.any(Array),
468+
jasmine.any(Object),
469+
undefined,
470+
undefined
471+
);
472+
});
473+
444474
it('should apply CSS class if attachments are present', () => {
445475
const cssClass = 'str-chat__input-flat-has-attachments';
446476

projects/stream-chat-angular/src/lib/message-input/message-input.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,17 @@ export class MessageInputComponent
290290
return;
291291
}
292292
const attachments = this.attachmentService.mapToAttachments();
293-
const text = this.textareaValue;
294-
if (!text && (!attachments || attachments.length === 0)) {
293+
let text = this.textareaValue;
294+
const textContainsOnlySpaceChars = !text.replace(/ /g, ''); //spcae
295+
if (
296+
(!text || textContainsOnlySpaceChars) &&
297+
(!attachments || attachments.length === 0)
298+
) {
295299
return;
296300
}
301+
if (textContainsOnlySpaceChars) {
302+
text = '';
303+
}
297304
if (this.containsLinks && !this.canSendLinks) {
298305
this.notificationService.addTemporaryNotification(
299306
'streamChat.Sending links is not allowed in this conversation'

0 commit comments

Comments
 (0)