Skip to content

Commit

Permalink
tests of getStyledTextArray
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Mar 9, 2023
1 parent 8a5e8fa commit 2f1befd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
36 changes: 1 addition & 35 deletions src/components/EmojiSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as StyleUtils from '../styles/StyleUtils';
import * as EmojiUtils from '../libs/EmojiUtils';
import Text from './Text';
import CONST from '../CONST';
import getStyledTextArray from '../libs/GetStyledTextArray';

const propTypes = {
/** The index of the highlighted emoji */
Expand Down Expand Up @@ -62,41 +63,6 @@ const measureHeightOfEmojiRows = (numRows, isEmojiPickerLarge) => {
return numRows * CONST.EMOJI_SUGGESTER.ITEM_HEIGHT;
};

/**
* Render a suggestion menu item component.
* @param {String} name
* @param {String} prefix
* @returns {Array}
*/
const getStyledTextArray = (name, prefix) => {
const texts = [];
const prefixLocation = name.search(prefix);

if (prefixLocation === 0 && prefix.length === name.length) {
texts.push({text: prefix, isColored: true});
} else if (prefixLocation === 0 && prefix.length !== name.length) {
texts.push(
{text: name.slice(0, prefix.length), isColored: true},
{text: name.slice(prefix.length), isColored: false},
);
} else if (prefixLocation > 0 && prefix.length !== name.length) {
texts.push(
{text: name.slice(0, prefixLocation), isColored: false},
{
text: name.slice(prefixLocation, prefixLocation + prefix.length),
isColored: true,
},
{
text: name.slice(prefixLocation + prefix.length),
isColored: false,
},
);
} else {
texts.push({text: name, isColored: false});
}
return texts;
};

/**
* Create unique keys for each emoji item
* @param {Object} item
Expand Down
37 changes: 37 additions & 0 deletions src/libs/GetStyledTextArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

/**
* Render a suggestion menu item component.
* @param {String} name
* @param {String} prefix
* @returns {Array}
*/
const getStyledTextArray = (name, prefix) => {
const texts = [];
const prefixLocation = name.search(prefix);

if (prefixLocation === 0 && prefix.length === name.length) {
texts.push({text: prefix, isColored: true});
} else if (prefixLocation === 0 && prefix.length !== name.length) {
texts.push(
{text: name.slice(0, prefix.length), isColored: true},
{text: name.slice(prefix.length), isColored: false},
);
} else if (prefixLocation > 0 && prefix.length !== name.length) {
texts.push(
{text: name.slice(0, prefixLocation), isColored: false},
{
text: name.slice(prefixLocation, prefixLocation + prefix.length),
isColored: true,
},
{
text: name.slice(prefixLocation + prefix.length),
isColored: false,
},
);
} else {
texts.push({text: name, isColored: false});
}
return texts;
};

export default getStyledTextArray;
1 change: 0 additions & 1 deletion src/styles/StyleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,6 @@ function getEmojiReactionCounterTextStyle(hasUserReacted, sizeScale = 1) {
return sizeStyles;
}


export {
getAvatarSize,
getAvatarStyle,
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/getStyledArratTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import getStyledTextArray from '../../src/libs/GetStyledTextArray';

describe('getStyledTextArray', () => {
test('returns an array with a single object with isColored true when prefix matches entire name', () => {
const result = getStyledTextArray('sm', 'sm');
expect(result).toEqual([{text: 'sm', isColored: true}]);
});

test('returns an array with two objects, the first with isColored true, when prefix matches the beginning of name', () => {
const result = getStyledTextArray('smile', 'sm');
expect(result).toEqual([
{text: 'sm', isColored: true},
{text: 'ile', isColored: false},
]);
});

test('returns an array with three objects, the second with isColored true, when prefix matches in the middle of name', () => {
const result = getStyledTextArray('smile', 'il');
expect(result).toEqual([
{text: 'sm', isColored: false},
{text: 'il', isColored: true},
{text: 'e', isColored: false},
]);
});
});

0 comments on commit 2f1befd

Please sign in to comment.