docbrown/app/javascript/utilities/__tests__/textAreaUtils.test.js
Suzanne Aitchison 5069cd681a
Markdown editor toolbar (#14876)
* rough starting point, roving tabindex in toolbar, bold and italic buttons

* refactor, add link

* add ul

* add ordered list

* core formatters in place, wip

* overflow options

* add keyboard shortcuts

* tidy up some dodgy classes

* add mocks for runtime in test and storybook, use correct modifier key for tooltip

* style tweaks

* refactor tooltips

* add markdown formatters tests

* add tests for toolbar component, fix mistake in overflow menu tooltips

* undo change no longer needed to button

* fix issue accessing runtime in formatters file

* only show darkened buttons and tooltips when focus-visible is true

* mobile view

* fix for responsive buttons & roving tabindex

* tweaks from PR review

* update cursor position on link insertion

* add a new line after block selection formatting

* align icons in center

* tidy up overflow menu listeners

* small refactors

* test for new text area util

* tidy up new lines after syntaxes

* fix logic in cursor offsets for links

* prevent scroll jumps after inserting syntax

* some style tweaks

* insert level 2 heading with new lines above and below

* update icons

* use margin instead of gap
2021-10-19 09:22:54 +01:00

77 lines
2.2 KiB
JavaScript

import { getMentionWordData, getIndexOfLineStart } 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('getIndexOfLineStart', () => {
it('returns 0 for empty text', () => {
expect(getIndexOfLineStart('', 0)).toEqual(0);
});
it('returns start index of 0 for a single line of text', () => {
expect(getIndexOfLineStart('something', 5)).toEqual(0);
});
it('returns start index of line for a multi-line text', () => {
expect(getIndexOfLineStart('one\ntwo', 6)).toEqual(4);
});
});