added tests on autosuggestion replacement

This commit is contained in:
Nicolas Constant 2020-06-03 18:23:13 -04:00
parent 305833ddc2
commit b0b3c4ec21
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 86 additions and 9 deletions

View File

@ -223,4 +223,60 @@ describe('CreateStatusComponent', () => {
const result = <string>(<any>component).tranformHtmlRepliesToReplies(pleromaMention);
expect(result).toBe('<p>test @sengi_app@pleroma.site qsdqds qsd qsd qsd q @test@pleroma.site <span class="h-card"><a href="https://pleroma.site/users/no" class="u-url">@<span>no</span></a></span></p>');
});
it('should autocomplete - at the end', () => {
let text = 'data @sengi';
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe('data @sengi@mastodon.social ');
});
it('should autocomplete - at the start', () => {
let text = '@sengi data';
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe('@sengi@mastodon.social data');
});
it('should autocomplete - at the middle', () => {
let text = 'data @sengi data';
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe('data @sengi@mastodon.social data');
});
it('should autocomplete - duplicate', () => {
let text = 'data @sengi @sengi2 data';
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe('data @sengi@mastodon.social @sengi2 data');
});
it('should autocomplete - duplicate 2', () => {
let text = 'data @sengi2 @sengi data';
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe('data @sengi2 @sengi@mastodon.social data');
});
it('should autocomplete - new lines', () => {
const newLine = String.fromCharCode(13, 10);
let text = `@sengi${newLine}${newLine}data`;
let pattern = '@sengi';
let autosuggest = '@sengi@mastodon.social';
const result = <string>(<any>component).replacePatternWithAutosuggest(text, pattern, autosuggest);
expect(result).toBe(`@sengi@mastodon.social${newLine}${newLine}data`);
});
});

View File

@ -222,7 +222,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
// if (state && state !== '') {
// this.status = state;
// } else {
if(!this.status || this.status === '') {
if (!this.status || this.status === '') {
const uniqueMentions = this.getMentions(this.statusReplyingTo, this.statusReplyingToWrapper.provider);
for (const mention of uniqueMentions) {
this.status += `@${mention} `;
@ -242,8 +242,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
this.innerHeight = window.innerHeight;
}
ngOnDestroy() {
if(this.isRedrafting){
ngOnDestroy() {
if (this.isRedrafting) {
this.statusStateService.resetStatusContent(null);
}
@ -577,7 +577,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
this.scheduledStatusService.statusAdded(acc);
}
if(this.isRedrafting){
if (this.isRedrafting) {
this.statusStateService.resetStatusContent(null);
} else {
this.statusStateService.resetStatusContent(this.statusReplyingToWrapper);
@ -685,12 +685,17 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
suggestionSelected(selection: AutosuggestSelection) {
if (this.status.includes(selection.pattern)) {
// let transformedStatus = this.status;
// transformedStatus = transformedStatus.replace(new RegExp(` ${selection.pattern} `), ` ${selection.autosuggest} `).replace(' ', ' ');
// transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern} `), `${selection.autosuggest} `).replace(' ', ' ');
// transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}$`), `${selection.autosuggest} `).replace(' ', ' ');
let transformedStatus = this.status;
transformedStatus = transformedStatus.replace(new RegExp(` ${selection.pattern} `), ` ${selection.autosuggest} `).replace(' ', ' ');
transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern} `), `${selection.autosuggest} `).replace(' ', ' ');
transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}$`), `${selection.autosuggest} `).replace(' ', ' ');
this.status = transformedStatus;
// const newLine = String.fromCharCode(13, 10);
// transformedStatus = transformedStatus.replace(new RegExp(`${selection.pattern}${newLine}`), `${selection.autosuggest}${newLine}`).replace(' ', ' ');
// this.status = transformedStatus;
this.status = this.replacePatternWithAutosuggest(this.status, selection.pattern, selection.autosuggest);
let newCaretPosition = this.status.indexOf(`${selection.autosuggest} `) + selection.autosuggest.length + 1;
if (newCaretPosition > this.status.length) newCaretPosition = this.status.length;
@ -708,6 +713,22 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
}
}
private replacePatternWithAutosuggest(status: string, pattern: string, autosuggest: string): string {
status = status.replace(new RegExp(` ${pattern} `), ` ${autosuggest} `).replace(' ', ' ');
status = status.replace(new RegExp(`${pattern} `), `${autosuggest} `).replace(' ', ' ');
status = status.replace(new RegExp(`${pattern}$`), `${autosuggest} `).replace(' ', ' ');
//status = status.replace(/$pattern$/, `${autosuggest} `).replace(' ', ' ');
// const newLine = String.fromCharCode(13, 10);
// status = status.replace(new RegExp(`${pattern}${newLine}`), `${autosuggest}${newLine}`).replace(' ', ' ');
//status = status.replace(new RegExp(`${pattern}$`, 'i'), `${autosuggest} `).replace(' ', ' ');
//status = status.replace(new RegExp(`/^${pattern}$/`, 'i'), `${autosuggest} `).replace(' ', ' ');
//status = status.replace(new RegExp(pattern, 'i'), `${autosuggest} `).replace(' ', ' ');
return status;
}
suggestionsChanged(hasSuggestions: boolean) {
this.hasSuggestions = hasSuggestions;
}