-
Notifications
You must be signed in to change notification settings - Fork 4
fix colon jump bug #1492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fix colon jump bug #1492
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
__tests__/components/drops/create/lexical/plugins/emoji/EmojiPlugin.extra.test.tsx
This file was deleted.
Oops, something went wrong.
120 changes: 96 additions & 24 deletions
120
__tests__/components/drops/create/lexical/plugins/emoji/EmojiPlugin.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,125 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import EmojiPlugin, { EMOJI_MATCH_REGEX } from '../../../../../../../components/drops/create/lexical/plugins/emoji/EmojiPlugin'; | ||
| import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; | ||
| import { useEmoji } from '../../../../../../../contexts/EmojiContext'; | ||
|
|
||
| jest.mock('@lexical/react/LexicalComposerContext'); | ||
|
|
||
| // minimal lexical mocks | ||
| jest.mock('lexical', () => ({ | ||
| $getRoot: jest.fn(() => ({ getAllTextNodes: () => [] })), | ||
| $getSelection: jest.fn(() => null), | ||
| $isRangeSelection: jest.fn(() => false), | ||
| $createRangeSelection: jest.fn(() => ({ anchor: { set: jest.fn() }, focus: { set: jest.fn() } })), | ||
| $setSelection: jest.fn(), | ||
| TextNode: class { | ||
| private text: string; | ||
| constructor(text: string) { this.text = text; } | ||
| getTextContent() { return this.text; } | ||
| setTextContent(_t: string) { this.text = _t; } | ||
| insertAfter() {} | ||
| remove() {} | ||
| getKey() { return 'k'; } | ||
| }, | ||
| LexicalEditor: class {}, | ||
| jest.mock('@lexical/react/LexicalComposerContext', () => ({ | ||
| useLexicalComposerContext: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../../../../../components/drops/create/lexical/nodes/EmojiNode', () => ({ | ||
| EmojiNode: class {}, | ||
| })); | ||
|
|
||
| jest.mock('../../../../../../../contexts/EmojiContext', () => ({ | ||
| useEmoji: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('lexical', () => { | ||
| class MockTextNode { | ||
| private text: string; | ||
|
|
||
| constructor(text: string) { | ||
| this.text = text; | ||
| } | ||
|
|
||
| getTextContent() { | ||
| return this.text; | ||
| } | ||
|
|
||
| setTextContent(text: string) { | ||
| this.text = text; | ||
| } | ||
|
|
||
| insertAfter() {} | ||
|
|
||
| remove() {} | ||
|
|
||
| getKey() { | ||
| return 'key'; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| $getRoot: jest.fn(() => ({ getAllTextNodes: () => [] })), | ||
| $getSelection: jest.fn(() => null), | ||
| $isRangeSelection: jest.fn(() => false), | ||
| $createRangeSelection: jest.fn(() => ({ | ||
| anchor: { set: jest.fn() }, | ||
| focus: { set: jest.fn() }, | ||
| })), | ||
| $setSelection: jest.fn(), | ||
| TextNode: MockTextNode, | ||
| LexicalEditor: class {}, | ||
| }; | ||
| }); | ||
|
|
||
| const useContextMock = useLexicalComposerContext as jest.Mock; | ||
| const useEmojiMock = useEmoji as jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| useEmojiMock.mockReturnValue({ | ||
| emojiMap: [ | ||
| { | ||
| id: 'custom', | ||
| name: 'custom', | ||
| category: 'custom', | ||
| emojis: [{ id: 'smile', name: 'Smile', keywords: 'happy', skins: [{ src: '' }] }], | ||
| }, | ||
| ], | ||
| findNativeEmoji: jest.fn(() => null), | ||
| }); | ||
| }); | ||
|
|
||
| describe('EmojiPlugin', () => { | ||
| it('matches emoji regex correctly', () => { | ||
| const text = 'say :smile: and :joy:'; | ||
| const matches = Array.from(text.matchAll(EMOJI_MATCH_REGEX)).map(m => m[1]); | ||
| const matches = Array.from(text.matchAll(EMOJI_MATCH_REGEX)).map((match) => match[1]); | ||
| expect(matches).toEqual(['smile', 'joy']); | ||
| }); | ||
|
|
||
| it('calls update when emoji text detected', () => { | ||
| let listener: (t: string) => void = () => {}; | ||
| const update = jest.fn((cb: any) => cb()); | ||
| let listener: (text: string) => void = () => undefined; | ||
| const update = jest.fn((callback: () => void) => callback()); | ||
| const editor = { | ||
| update, | ||
| registerTextContentListener: jest.fn((cb: any) => { listener = cb; return () => {}; }) | ||
| registerTextContentListener: jest.fn((cb: typeof listener) => { | ||
| listener = cb; | ||
| return () => undefined; | ||
| }), | ||
| } as any; | ||
|
|
||
| useContextMock.mockReturnValue([editor]); | ||
| render(<EmojiPlugin />); | ||
|
|
||
| expect(update).toHaveBeenCalledTimes(1); | ||
|
|
||
| listener('hello :smile:'); | ||
| expect(update).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('does not update when listener text has no colon', () => { | ||
| const update = jest.fn((callback: () => void) => callback()); | ||
| const register = jest.fn(() => () => undefined); | ||
|
|
||
| useContextMock.mockReturnValue([ | ||
| { | ||
| update, | ||
| registerTextContentListener: register, | ||
| }, | ||
| ]); | ||
|
|
||
| render(<EmojiPlugin />); | ||
|
|
||
| const listener = register.mock.calls[0][0] as (text: string) => void; | ||
| listener('nothing'); | ||
|
|
||
| expect(update).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('regex captures id including surrounding colons', () => { | ||
| const match = ':smile:'.match(EMOJI_MATCH_REGEX); | ||
| expect(match?.[0]).toBe(':smile:'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't abort processing when encountering invalid emoji IDs
These new
returnstatements exittransformEmojiTextToNodeentirely as soon as the current text node lacks a valid emoji or the loop hits one invalid ID. In practice, a leading:foo:(unknown) completely prevents later:smile:matches—in the same node or subsequent nodes—from ever converting, so the colon-jump bug persists. We need to skip the offending segment/node but keep scanning the rest.