Skip to content

Commit dca5190

Browse files
committed
fix: guard malformed frequent emoji history
1 parent 9c84039 commit dca5190

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/libs/EmojiUtils.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ const sortByName = (emoji: Emoji, emojiData: RegExpMatchArray) => {
123123
};
124124

125125
const processFrequentlyUsedEmojis = (emojiList?: FrequentlyUsedEmoji[]) => {
126-
if (!emojiList) {
126+
// Persisted emoji history can contain non-array values despite its declared Onyx type.
127+
if (!Array.isArray(emojiList)) {
127128
return [];
128129
}
129130

‎tests/unit/libs/EmojiUtilsTest.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ jest.mock('@assets/emojis', () => ({
6767
}));
6868

6969
describe('processFrequentlyUsedEmojis', () => {
70+
it.each([
71+
{description: 'an empty object', value: {}},
72+
{description: 'an object with numeric keys', value: Object.fromEntries([['0', {name: 'grinning_face', count: 1, lastUpdatedAt: 1000}]])},
73+
{description: 'a boolean', value: true},
74+
{description: 'a number', value: 42},
75+
{description: 'a string', value: 'invalid emoji history'},
76+
])('should return an empty array when stored emoji history is $description', ({value}) => {
77+
// Given persisted emoji history can violate its declared array type at runtime.
78+
const invalidEmojiList = value;
79+
80+
// When the emoji picker processes that history during render.
81+
// @ts-expect-error Malformed persisted data deliberately violates the declared array type.
82+
const result = processFrequentlyUsedEmojis(invalidEmojiList);
83+
84+
// Then invalid history must not crash the picker or create frequent emoji entries.
85+
expect(result).toEqual([]);
86+
});
87+
7088
it('should return empty array when input is undefined', () => {
7189
const result = processFrequentlyUsedEmojis(undefined);
7290
expect(result).toEqual([]);

0 commit comments

Comments
 (0)