Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<textarea
class="new-reply-input"
[(ngModel)]="response.replyText"
(keyup)="replyEntered($event)"
(keydown)="replyEntered($event)"
placeholder="Add Comment"
i18n-placeholder
aria-label="Add Comment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('ClassResponseComponent', () => {
injectLinks();
showLastReply();
showAllReplies();
replyEntered();
});

function createComponentState(responseText: string = '', replies: any[] = []): any {
Expand Down Expand Up @@ -84,3 +85,62 @@ function showAllReplies() {
});
});
}

function replyEntered() {
describe('replyEntered', () => {
let textarea: HTMLTextAreaElement;
beforeEach(() => {
component.mode = 'student';
component.response.replyText = '我同意你的看法';
fixture.detectChanges();
textarea = fixture.nativeElement.querySelector('.new-reply-input');
spyOn(component.submitButtonClicked, 'emit');
});

it('should not submit when Enter confirms an IME composition', () => {
pressKey(textarea, 'keydown', { key: 'Process', isComposing: true }, 229);
pressKey(textarea, 'keyup', { key: 'Enter' }, 13);
expect(component.submitButtonClicked.emit).not.toHaveBeenCalled();
expect(component.response.replyText).toEqual('我同意你的看法');
});

it('should not submit when the Enter keydown is reported as composing', () => {
pressKey(textarea, 'keydown', { key: 'Enter', isComposing: true }, 13);
expect(component.submitButtonClicked.emit).not.toHaveBeenCalled();
expect(component.response.replyText).toEqual('我同意你的看法');
});

it('should not submit when the Enter keydown has key code 229 after composition ends', () => {
pressKey(textarea, 'keydown', { key: 'Enter', isComposing: false }, 229);
expect(component.submitButtonClicked.emit).not.toHaveBeenCalled();
expect(component.response.replyText).toEqual('我同意你的看法');
});

it('should submit the reply exactly as typed when Enter is pressed', () => {
const keydown = pressKey(textarea, 'keydown', { key: 'Enter' }, 13);
pressKey(textarea, 'keyup', { key: 'Enter' }, 13);
expect(keydown.defaultPrevented).toBeTrue();
expect(component.submitButtonClicked.emit).toHaveBeenCalledTimes(1);
expect(component.response.replyText).toEqual('我同意你的看法');
});

it('should not submit when Shift+Enter is pressed', () => {
pressKey(textarea, 'keydown', { key: 'Enter', shiftKey: true }, 13);
pressKey(textarea, 'keyup', { key: 'Enter', shiftKey: true }, 13);
expect(component.submitButtonClicked.emit).not.toHaveBeenCalled();
expect(component.response.replyText).toEqual('我同意你的看法');
});
});
}

function pressKey(
target: HTMLElement,
type: string,
init: KeyboardEventInit,
keyCode: number
): KeyboardEvent {
const event = new KeyboardEvent(type, { bubbles: true, cancelable: true, ...init });
Object.defineProperty(event, 'keyCode', { get: () => keyCode });
target.dispatchEvent(event);
return event;
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,18 @@ export class ClassResponse {
return this.configService.convertToClientTimestamp(time);
}

protected replyEntered($event: any): void {
if (this.isEnterKeyEvent($event)) {
$event.preventDefault();
this.response.replyText = this.removeLastChar(this.response.replyText);
this.expandAndShowAllReplies();
this.submitButtonClicked.emit(this.response);
protected replyEntered(event: KeyboardEvent): void {
if (this.isEnterKeyEvent(event)) {
event.preventDefault();
if (this.response.replyText) {
this.expandAndShowAllReplies();
this.submitButtonClicked.emit(this.response);
}
}
}

protected isEnterKeyEvent(event: any): boolean {
return event.keyCode == 13 && !event.shiftKey && this.response.replyText;
}

private removeLastChar(responseText: string): string {
return responseText.substring(0, responseText.length - 1);
protected isEnterKeyEvent(event: KeyboardEvent): boolean {
return event.key === 'Enter' && !event.shiftKey && !event.isComposing && event.keyCode !== 229;
}

protected toggleExpanded(): void {
Expand Down
Loading