-
Notifications
You must be signed in to change notification settings - Fork 5
Wave mention bug #2015
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
Merged
Wave mention bug #2015
Changes from 2 commits
Commits
Show all changes
4 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
119 changes: 119 additions & 0 deletions
119
__tests__/components/drops/create/lexical/plugins/waves/WaveMentionsPlugin.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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import React, { createRef } from "react"; | ||
| import { act, render } from "@testing-library/react"; | ||
| import NewWaveMentionsPlugin, { | ||
| WaveMentionTypeaheadOption, | ||
| } from "@/components/drops/create/lexical/plugins/waves/WaveMentionsPlugin"; | ||
|
|
||
| jest.mock("@lexical/react/LexicalComposerContext", () => ({ | ||
| useLexicalComposerContext: () => [{ update: (fn: () => void) => fn() }], | ||
| })); | ||
|
|
||
| let capturedProps: any; | ||
| jest.mock("@lexical/react/LexicalTypeaheadMenuPlugin", () => ({ | ||
| LexicalTypeaheadMenuPlugin: (props: any) => { | ||
| capturedProps = props; | ||
| return <div data-testid="typeahead" />; | ||
| }, | ||
| MenuOption: class { | ||
| key: string; | ||
| ref: { current: HTMLElement | null }; | ||
|
|
||
| constructor(key: string) { | ||
| this.key = key; | ||
| this.ref = { current: null }; | ||
| } | ||
|
|
||
| setRefElement = (element: HTMLElement | null) => { | ||
| this.ref.current = element; | ||
| }; | ||
| }, | ||
| useBasicTypeaheadTriggerMatch: () => jest.fn(() => null), | ||
| })); | ||
|
|
||
| jest.mock("@/hooks/useWavesSearch", () => ({ | ||
| useWavesSearch: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock("@/components/drops/create/lexical/nodes/WaveMentionNode", () => ({ | ||
| $createWaveMentionNode: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock( | ||
| "@/components/drops/create/lexical/utils/codeContextDetection", | ||
| () => ({ | ||
| isInCodeContext: jest.fn(() => false), | ||
| }) | ||
| ); | ||
|
|
||
| const { useWavesSearch } = require("@/hooks/useWavesSearch"); | ||
| const { | ||
| $createWaveMentionNode, | ||
| } = require("@/components/drops/create/lexical/nodes/WaveMentionNode"); | ||
|
|
||
| describe("WaveMentionsPlugin", () => { | ||
| beforeEach(() => { | ||
| capturedProps = null; | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("builds options from waves and exposes open state", () => { | ||
| (useWavesSearch as jest.Mock).mockReturnValue({ | ||
| waves: [ | ||
| { id: "wave-1", name: "Wave Alpha", picture: null }, | ||
| { id: "wave-2", name: "Wave Beta", picture: null }, | ||
| ], | ||
| }); | ||
|
|
||
| const ref = createRef<any>(); | ||
| render(<NewWaveMentionsPlugin onSelect={jest.fn()} ref={ref} />); | ||
|
|
||
| expect(capturedProps.options).toHaveLength(2); | ||
| expect(capturedProps.options[0]).toBeInstanceOf(WaveMentionTypeaheadOption); | ||
|
|
||
| act(() => { | ||
| capturedProps.onOpen(); | ||
| }); | ||
| expect(ref.current.isWaveMentionsOpen()).toBe(true); | ||
|
|
||
| act(() => { | ||
| capturedProps.onClose(); | ||
| }); | ||
| expect(ref.current.isWaveMentionsOpen()).toBe(false); | ||
| }); | ||
|
|
||
| it("creates wave mention node and emits sanitized mention payload", () => { | ||
| (useWavesSearch as jest.Mock).mockReturnValue({ | ||
| waves: [{ id: "wave-1", name: "Brack]et Wave", picture: null }], | ||
| }); | ||
|
|
||
| const mentionNode = { | ||
| select: jest.fn(), | ||
| }; | ||
| ($createWaveMentionNode as jest.Mock).mockReturnValue(mentionNode); | ||
|
|
||
| const onSelect = jest.fn(); | ||
| render(<NewWaveMentionsPlugin onSelect={onSelect} ref={createRef()} />); | ||
|
|
||
| const closeMenu = jest.fn(); | ||
| const nodeToReplace = { | ||
| replace: jest.fn(), | ||
| }; | ||
|
|
||
| act(() => { | ||
| capturedProps.onSelectOption( | ||
| capturedProps.options[0], | ||
| nodeToReplace, | ||
| closeMenu | ||
| ); | ||
| }); | ||
|
|
||
| expect($createWaveMentionNode).toHaveBeenCalledWith("#Bracket Wave"); | ||
| expect(nodeToReplace.replace).toHaveBeenCalledWith(mentionNode); | ||
| expect(mentionNode.select).toHaveBeenCalled(); | ||
| expect(onSelect).toHaveBeenCalledWith({ | ||
| wave_id: "wave-1", | ||
| wave_name_in_content: "Bracket Wave", | ||
| }); | ||
| expect(closeMenu).toHaveBeenCalled(); | ||
| }); | ||
| }); |
104 changes: 104 additions & 0 deletions
104
__tests__/components/drops/create/lexical/plugins/waves/WaveMentionsTypeaheadMenu.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 |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import WaveMentionsTypeaheadMenu from "@/components/drops/create/lexical/plugins/waves/WaveMentionsTypeaheadMenu"; | ||
|
|
||
| function createOption(name: string) { | ||
| return { | ||
| key: `option-${name}`, | ||
| name, | ||
| picture: null, | ||
| setRefElement: jest.fn(), | ||
| }; | ||
| } | ||
|
|
||
| describe("WaveMentionsTypeaheadMenu", () => { | ||
| const baseRect = { | ||
| x: 0, | ||
| y: 0, | ||
| width: 320, | ||
| height: 120, | ||
| top: 100, | ||
| right: 320, | ||
| bottom: 220, | ||
| left: 0, | ||
| toJSON: () => "", | ||
| }; | ||
|
|
||
| let getBoundingClientRectMock: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| getBoundingClientRectMock = jest | ||
| .spyOn(HTMLElement.prototype, "getBoundingClientRect") | ||
| .mockReturnValue(baseRect as DOMRect); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| getBoundingClientRectMock.mockRestore(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("renders with bottom positioning when there is more space below", () => { | ||
| const options = [createOption("Wave Alpha"), createOption("Wave Beta")]; | ||
| const { container } = render( | ||
| <WaveMentionsTypeaheadMenu | ||
| selectedIndex={0} | ||
| options={options as any} | ||
| setHighlightedIndex={jest.fn()} | ||
| selectOptionAndCleanUp={jest.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveClass("tw-top-full"); | ||
| expect(container.firstChild).toHaveClass("tw-mt-1"); | ||
| }); | ||
|
|
||
| it("switches to top positioning when there is more space above", async () => { | ||
| getBoundingClientRectMock.mockReturnValue({ | ||
| ...baseRect, | ||
| top: 700, | ||
| bottom: 790, | ||
| } as DOMRect); | ||
|
|
||
| const options = [createOption("Wave Alpha")]; | ||
| const { container } = render( | ||
| <WaveMentionsTypeaheadMenu | ||
| selectedIndex={0} | ||
| options={options as any} | ||
| setHighlightedIndex={jest.fn()} | ||
| selectOptionAndCleanUp={jest.fn()} | ||
| /> | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(container.firstChild).toHaveClass("tw-bottom-full"); | ||
| expect(container.firstChild).toHaveClass("tw-mb-1"); | ||
| }); | ||
| }); | ||
|
|
||
| it("selects clicked option and remains stable across repeated resize events", () => { | ||
| const options = [createOption("Wave Alpha"), createOption("Wave Beta")]; | ||
| const setHighlightedIndex = jest.fn(); | ||
| const selectOptionAndCleanUp = jest.fn(); | ||
|
|
||
| render( | ||
| <WaveMentionsTypeaheadMenu | ||
| selectedIndex={0} | ||
| options={options as any} | ||
| setHighlightedIndex={setHighlightedIndex} | ||
| selectOptionAndCleanUp={selectOptionAndCleanUp} | ||
| /> | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: "Wave Alpha" })); | ||
|
|
||
| expect(setHighlightedIndex).toHaveBeenCalledWith(0); | ||
| expect(selectOptionAndCleanUp).toHaveBeenCalledWith(options[0]); | ||
|
|
||
| for (let i = 0; i < 5; i += 1) { | ||
| fireEvent(window, new Event("resize")); | ||
| } | ||
|
|
||
| expect( | ||
| screen.getByRole("button", { name: "Wave Alpha" }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.