docbrown/app/javascript/utilities/__tests__/textAreaUtils.test.js
Suzanne Aitchison e3c97d18d6
Update storybook mention autocomplete component to take over whole textarea (#13044)
* Revert "Revert "Update storybook autocomplete component (#12980)" (#13027)"

This reverts commit f393dd2bbc.

* create helper function to replace node, add comments

* minor refactors
2021-03-18 15:04:59 +00:00

68 lines
1.8 KiB
JavaScript

import { getMentionWordData } 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);
});
});