Skip to content

Commit 6990445

Browse files
committed
fix: Empty messages can be sent #133
1 parent ea25886 commit 6990445

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"pretty-bytes": "^5.6.0",
8282
"rxjs": "~6.6.0",
8383
"stream-chat": "^4.3.0",
84-
"stream-chat-css": "^1.0.22",
84+
"stream-chat-css": "^1.0.23",
8585
"ts-node": "^10.2.1",
8686
"tslib": "^2.3.0",
8787
"uuidv4": "^6.2.12",

projects/stream-chat-angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@angular/core": "^12.2.0 || ^13.0.0",
1414
"@ngx-translate/core": "^13.0.0 || ^14.0.0",
1515
"stream-chat": "^4.3.0",
16-
"stream-chat-css": "^1.0.22"
16+
"stream-chat-css": "^1.0.23"
1717
},
1818
"dependencies": {
1919
"@ctrl/ngx-emoji-mart": "^6.0.1",

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<div class="str-chat__input-flat">
1+
<div
2+
class="str-chat__input-flat"
3+
[class.str-chat__input-flat-has-attachments]="
4+
(attachmentUploads$ | async)!.length > 0
5+
"
6+
>
27
<div class="str-chat__input-flat-wrapper">
38
<div class="str-chat__input-flat--textarea-wrapper">
49
<stream-attachment-preview-list

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ describe('MessageInputComponent', () => {
135135
const spy = jasmine.createSpy();
136136
component.messageUpdate.subscribe(spy);
137137
component.message = mockMessage();
138+
fixture.detectChanges();
138139
updateMessageSpy.and.rejectWith(new Error('Error'));
139140
await component.messageSent(new KeyboardEvent('keydown', { key: 'Enter' }));
140141

@@ -147,6 +148,7 @@ describe('MessageInputComponent', () => {
147148

148149
it('should emit #messageUpdate event if message update was successful', async () => {
149150
component.message = mockMessage();
151+
fixture.detectChanges();
150152
const spy = jasmine.createSpy();
151153
component.messageUpdate.subscribe(spy);
152154
await component.messageSent(new KeyboardEvent('keydown', { key: 'Enter' }));
@@ -265,13 +267,10 @@ describe('MessageInputComponent', () => {
265267
{ file, state: 'success', url: 'url/to/image' },
266268
]);
267269
const files = [file];
268-
attachmentService.resetAttachmentUploads.and.returnValue([]);
269-
attachmentService.mapToAttachments.and.returnValue([]);
270270
await component.filesSelected(files as any as FileList);
271271
await component.messageSent();
272-
await component.messageSent();
273272

274-
expect(sendMessageSpy).toHaveBeenCalledWith(jasmine.any(String), []);
273+
expect(attachmentService.resetAttachmentUploads).toHaveBeenCalledWith();
275274
});
276275

277276
it(`shouldn't send message, if file uploads are in progress`, async () => {
@@ -372,4 +371,26 @@ describe('MessageInputComponent', () => {
372371
attachments
373372
);
374373
});
374+
375+
it(`shouldn't send empty message`, () => {
376+
const textarea = queryTextarea();
377+
const event = new KeyboardEvent('keydown', { key: 'Enter' });
378+
spyOn(event, 'preventDefault');
379+
textarea?.dispatchEvent(event);
380+
fixture.detectChanges();
381+
382+
expect(sendMessageSpy).not.toHaveBeenCalled();
383+
expect(event.preventDefault).toHaveBeenCalledWith();
384+
});
385+
386+
it('should apply CSS class if attachments are present', () => {
387+
const cssClass = 'str-chat__input-flat-has-attachments';
388+
389+
expect(nativeElement.querySelector(`.${cssClass}`)).toBeNull();
390+
391+
attachmentService.attachmentUploads$.next([{} as any as AttachmentUpload]);
392+
fixture.detectChanges();
393+
394+
expect(nativeElement.querySelector(`.${cssClass}`)).not.toBeNull();
395+
});
375396
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ export class MessageInputComponent implements OnChanges, OnDestroy {
111111
}
112112
return;
113113
}
114-
const text = this.messageInput.nativeElement.value;
115114
const attachments = this.attachmentService.mapToAttachments();
115+
const text = this.messageInput.nativeElement.value;
116+
if (!text && (!attachments || attachments.length === 0)) {
117+
return;
118+
}
116119
if (!this.isUpdate) {
117120
this.messageInput.nativeElement.value = '';
118121
}

0 commit comments

Comments
 (0)