* spelling: access Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: additional Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: administrative Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: aggregate Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: assigns Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: attributes Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: autocomplete Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: because Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: between Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: bootstrap Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: calculating Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: captcha Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: character Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: chosen Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: commenter Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: competitor Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: componentize Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: contrast Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: corresponding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: description Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: destroyed Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: destroys Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: discussion Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: episode Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: escaped Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: evaluates Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: expired Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: explicitly Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: facebook Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: fragment Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: functionality Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: improper Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: incentive Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: interfere Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: latest Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: message Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: minimum Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: moderator Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: mouseover Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: mutual Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: nonexistent Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: notification Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occasionally Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occurrence Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occurs Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: octokit Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: offset Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: omitted Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: opacity Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: organization Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: organizations Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: overridden Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: override Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: overriding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: prefill Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: previous Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: profile Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: recycling Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: registered Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: repositories Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: rescuing Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: response Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: returns Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: second Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: separator Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: services Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: subscriber Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: subscription Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: success Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: successful Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: successfully Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: suppress Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: test Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: thought Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: uniqueness Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unknown Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unproductive Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unreachable Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unsuccessful Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: utilities Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: utility Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: valid Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: voluntarily Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: vomited Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: website Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: withholding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
286 lines
6.9 KiB
JavaScript
286 lines
6.9 KiB
JavaScript
import {
|
|
getMentionWordData,
|
|
getLastIndexOfCharacter,
|
|
getNextIndexOfCharacter,
|
|
getSelectionData,
|
|
getNumberOfNewLinesPrecedingSelection,
|
|
getNumberOfNewLinesFollowingSelection,
|
|
} from '../textAreaUtils';
|
|
|
|
describe('getMentionWordData', () => {
|
|
it('returns userMention false for cursor at start of input', () => {
|
|
const inputState = {
|
|
selectionStart: 0,
|
|
value: 'text with @mention',
|
|
};
|
|
|
|
const { isUserMention, indexOfMentionStart } =
|
|
getMentionWordData(inputState);
|
|
expect(isUserMention).toBe(false);
|
|
expect(indexOfMentionStart).toEqual(-1);
|
|
});
|
|
|
|
it('returns userMention false for empty input value', () => {
|
|
const inputState = {
|
|
selectionStart: 10,
|
|
value: '',
|
|
};
|
|
|
|
const { isUserMention, indexOfMentionStart } =
|
|
getMentionWordData(inputState);
|
|
expect(isUserMention).toBe(false);
|
|
expect(indexOfMentionStart).toEqual(-1);
|
|
});
|
|
|
|
it('returns userMention false if no @ symbol exists at start of word', () => {
|
|
const inputState = {
|
|
selectionStart: 13,
|
|
value: 'text with no mention',
|
|
};
|
|
|
|
const { isUserMention, indexOfMentionStart } =
|
|
getMentionWordData(inputState);
|
|
expect(isUserMention).toBe(false);
|
|
expect(indexOfMentionStart).toEqual(-1);
|
|
});
|
|
|
|
it('returns userMention true and correct index for an @ mention at beginning of input', () => {
|
|
const inputState = {
|
|
selectionStart: 3,
|
|
value: '@mention',
|
|
};
|
|
|
|
const { isUserMention, indexOfMentionStart } =
|
|
getMentionWordData(inputState);
|
|
expect(isUserMention).toBe(true);
|
|
expect(indexOfMentionStart).toEqual(0);
|
|
});
|
|
|
|
it('returns userMention true and correct index for @ mention in middle of input', () => {
|
|
const inputState = {
|
|
selectionStart: 13,
|
|
value: 'text with @mention',
|
|
};
|
|
|
|
const { isUserMention, indexOfMentionStart } =
|
|
getMentionWordData(inputState);
|
|
expect(isUserMention).toBe(true);
|
|
expect(indexOfMentionStart).toEqual(10);
|
|
});
|
|
});
|
|
|
|
describe('getLastIndexOfCharacter', () => {
|
|
it('returns -1 when content is empty', () => {
|
|
expect(
|
|
getLastIndexOfCharacter({
|
|
content: '',
|
|
selectionIndex: 0,
|
|
character: 'f',
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
|
|
it("returns -1 for a character that isn't present", () => {
|
|
expect(
|
|
getLastIndexOfCharacter({
|
|
content: 'abcde',
|
|
selectionIndex: 4,
|
|
character: 'f',
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
|
|
it('returns index of the last occurrence within a single word', () => {
|
|
expect(
|
|
getLastIndexOfCharacter({
|
|
content: 'abcde',
|
|
selectionIndex: 4,
|
|
character: 'b',
|
|
}),
|
|
).toEqual(1);
|
|
});
|
|
|
|
it('returns index of the last occurrence searching through multiple words', () => {
|
|
expect(
|
|
getLastIndexOfCharacter({
|
|
content: 'ab cd ef ghi',
|
|
selectionIndex: 10,
|
|
character: 'b',
|
|
}),
|
|
).toEqual(1);
|
|
});
|
|
|
|
it('halts the search when encountering a break character', () => {
|
|
expect(
|
|
getLastIndexOfCharacter({
|
|
content: 'ab cd ef ghi',
|
|
selectionIndex: 10,
|
|
character: 'b',
|
|
breakOnCharacters: [' '],
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
});
|
|
|
|
describe('getNextIndexOfCharacter', () => {
|
|
it('returns -1 when content is empty', () => {
|
|
expect(
|
|
getNextIndexOfCharacter({
|
|
content: '',
|
|
selectionIndex: 0,
|
|
character: 'f',
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
|
|
it("returns -1 for a character that isn't present", () => {
|
|
expect(
|
|
getNextIndexOfCharacter({
|
|
content: 'abcde',
|
|
selectionIndex: 1,
|
|
character: 'f',
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
|
|
it('returns index of the last occurrence within a single word', () => {
|
|
expect(
|
|
getNextIndexOfCharacter({
|
|
content: 'abcde',
|
|
selectionIndex: 0,
|
|
character: 'e',
|
|
}),
|
|
).toEqual(4);
|
|
});
|
|
|
|
it('returns index of the last occurrence searching through multiple words', () => {
|
|
expect(
|
|
getNextIndexOfCharacter({
|
|
content: 'ab cd ef ghi',
|
|
selectionIndex: 0,
|
|
character: 'f',
|
|
}),
|
|
).toEqual(7);
|
|
});
|
|
|
|
it('halts the search when encountering a break character', () => {
|
|
expect(
|
|
getNextIndexOfCharacter({
|
|
content: 'ab cd ef ghi',
|
|
selectionIndex: 0,
|
|
character: 'f',
|
|
breakOnCharacters: [' '],
|
|
}),
|
|
).toEqual(-1);
|
|
});
|
|
});
|
|
|
|
describe('getSelectionData', () => {
|
|
it('returns selection data for given inputs', () => {
|
|
expect(
|
|
getSelectionData({
|
|
selectionStart: 4,
|
|
selectionEnd: 7,
|
|
value: 'one two three four',
|
|
}),
|
|
).toEqual({
|
|
textBeforeSelection: 'one ',
|
|
textAfterSelection: ' three four',
|
|
selectedText: 'two',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getNumberOfNewLinesPrecedingSelection', () => {
|
|
it('returns 0 when selection start is 0', () => {
|
|
expect(
|
|
getNumberOfNewLinesPrecedingSelection({
|
|
selectionStart: 0,
|
|
value: 'some text',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('returns 0 if no new lines exist before selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesPrecedingSelection({
|
|
selectionStart: 9,
|
|
value: 'some text',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('returns count of new lines before selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesPrecedingSelection({
|
|
selectionStart: 6,
|
|
value: 'some\n\ntext',
|
|
}),
|
|
).toEqual(2);
|
|
});
|
|
|
|
it('only returns count of new lines immediately before selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesPrecedingSelection({
|
|
selectionStart: 7,
|
|
value: 'some\n\ntext',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('stops counting new lines as soon as any other character occurs', () => {
|
|
expect(
|
|
getNumberOfNewLinesPrecedingSelection({
|
|
selectionStart: 9,
|
|
value: '\n\n\nsome\n\ntext',
|
|
}),
|
|
).toEqual(2);
|
|
});
|
|
});
|
|
|
|
describe('getNumberOfNewLinesFollowingSelection', () => {
|
|
it('returns 0 when selection end is end of text area value', () => {
|
|
expect(
|
|
getNumberOfNewLinesFollowingSelection({
|
|
selectionEnd: 9,
|
|
value: 'some text',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('returns 0 if no new lines exist after selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesFollowingSelection({
|
|
selectionEnd: 1,
|
|
value: 'some text',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('returns count of new lines after selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesFollowingSelection({
|
|
selectionEnd: 4,
|
|
value: 'some\n\ntext',
|
|
}),
|
|
).toEqual(2);
|
|
});
|
|
|
|
it('only returns count of new lines immediately after selection', () => {
|
|
expect(
|
|
getNumberOfNewLinesFollowingSelection({
|
|
selectionEnd: 1,
|
|
value: 'some\n\ntext',
|
|
}),
|
|
).toEqual(0);
|
|
});
|
|
|
|
it('stops counting new lines as soon as any other character occurs', () => {
|
|
expect(
|
|
getNumberOfNewLinesFollowingSelection({
|
|
selectionEnd: 0,
|
|
value: '\n\n\nsome\n\ntext',
|
|
}),
|
|
).toEqual(3);
|
|
});
|
|
});
|