-
Notifications
You must be signed in to change notification settings - Fork 314
[stable5.7] fix(drag-and-drop): track all droppable-mailbox directive instances independently #12669
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
Open
backportbot
wants to merge
3
commits into
stable5.7
Choose a base branch
from
backport/12650/stable5.7
base: stable5.7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−25
Open
[stable5.7] fix(drag-and-drop): track all droppable-mailbox directive instances independently #12669
Changes from all commits
Commits
Show all changes
3 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
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
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
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,168 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { DroppableMailboxDirective } from '../../../directives/drag-and-drop/droppable-mailbox/index.js' | ||
| import dragEventBus from '../../../directives/drag-and-drop/util/dragEventBus.js' | ||
|
|
||
| /** | ||
| * Creates a mock DOM element with a firstChild for the directive. | ||
| * | ||
| * @param {string} id - Identifier for the element | ||
| * @return {object} Mock element | ||
| */ | ||
| function createMockEl(id) { | ||
| const firstChild = { | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| } | ||
| return { id, firstChild, setAttribute: vi.fn() } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a mock binding value for the directive. | ||
| * | ||
| * @param {object} overrides - Override default options | ||
| * @return {object} Mock binding | ||
| */ | ||
| function createBinding(overrides = {}) { | ||
| return { | ||
| value: { | ||
| mainStore: {}, | ||
| mailboxId: 1, | ||
| accountId: 1, | ||
| isValidDropTarget: true, | ||
| ...overrides, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| describe('DroppableMailboxDirective', () => { | ||
| // Use the Vue 2 hooks since the project is on Vue 2.7 | ||
| const { bind, componentUpdated, unbind } = DroppableMailboxDirective | ||
|
|
||
| let boundEls = [] | ||
|
|
||
| afterEach(() => { | ||
| boundEls.forEach((el) => unbind(el)) | ||
| boundEls = [] | ||
| }) | ||
|
|
||
| it('tracks multiple instances independently', () => { | ||
| const el1 = createMockEl('el1') | ||
| const el2 = createMockEl('el2') | ||
| const el3 = createMockEl('el3') | ||
|
|
||
| bind(el1, createBinding({ mailboxId: 1 })) | ||
| bind(el2, createBinding({ mailboxId: 2 })) | ||
| bind(el3, createBinding({ mailboxId: 3 })) | ||
| boundEls.push(el1, el2, el3) | ||
|
|
||
| // All elements should have the expected event listeners registered | ||
| for (const el of [el1, el2, el3]) { | ||
| expect(el.firstChild.addEventListener).toHaveBeenCalledWith( | ||
| 'dragover', | ||
| expect.any(Function), | ||
| ) | ||
| expect(el.firstChild.addEventListener).toHaveBeenCalledWith( | ||
| 'dragleave', | ||
| expect.any(Function), | ||
| ) | ||
| expect(el.firstChild.addEventListener).toHaveBeenCalledWith( | ||
| 'drop', | ||
| expect.any(Function), | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| it('updates the correct instance on componentUpdated', () => { | ||
| const el1 = createMockEl('el1') | ||
| const el2 = createMockEl('el2') | ||
|
|
||
| bind(el1, createBinding({ mailboxId: 1, isValidDropTarget: true })) | ||
| bind(el2, createBinding({ mailboxId: 2, isValidDropTarget: true })) | ||
| boundEls.push(el1, el2) | ||
|
|
||
| // Update el1 to disable drop target | ||
| componentUpdated( | ||
| el1, | ||
| createBinding({ mailboxId: 1, isValidDropTarget: false }), | ||
| ) | ||
|
|
||
| // Clear setAttribute calls from bind so we only see drag-start effects | ||
| el1.setAttribute.mockClear() | ||
| el2.setAttribute.mockClear() | ||
|
|
||
| // Trigger drag-start on all instances via the event bus | ||
| dragEventBus.emit('drag-start', { accountId: 1, mailboxId: 99 }) | ||
|
|
||
| // el1 should be disabled because its isValidDropTarget was set to false | ||
| expect(el1.setAttribute).toHaveBeenCalledWith( | ||
| 'droppable-mailbox', | ||
| 'disabled', | ||
| ) | ||
|
|
||
| // el2 should NOT be disabled because its options were not changed | ||
| expect(el2.setAttribute).not.toHaveBeenCalledWith( | ||
| 'droppable-mailbox', | ||
| 'disabled', | ||
| ) | ||
| }) | ||
|
|
||
| it('removes listeners and instance on unbind', () => { | ||
| const el1 = createMockEl('el1') | ||
| const el2 = createMockEl('el2') | ||
|
|
||
| bind(el1, createBinding({ mailboxId: 1 })) | ||
| bind(el2, createBinding({ mailboxId: 2 })) | ||
| boundEls.push(el2) | ||
|
|
||
| unbind(el1) | ||
|
|
||
| // el1 should have had removeEventListener called for each event | ||
| expect(el1.firstChild.removeEventListener).toHaveBeenCalledWith( | ||
| 'dragover', | ||
| expect.any(Function), | ||
| ) | ||
| expect(el1.firstChild.removeEventListener).toHaveBeenCalledWith( | ||
| 'dragleave', | ||
| expect.any(Function), | ||
| ) | ||
| expect(el1.firstChild.removeEventListener).toHaveBeenCalledWith( | ||
| 'drop', | ||
| expect.any(Function), | ||
| ) | ||
|
|
||
| // el2 should NOT have had removeEventListener called | ||
| expect(el2.firstChild.removeEventListener).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('removes the same listener references that were added', () => { | ||
| const el = createMockEl('el1') | ||
|
|
||
| bind(el, createBinding({ mailboxId: 1 })) | ||
| unbind(el) | ||
|
|
||
| // Each function passed to removeEventListener should be the same | ||
| // reference that was passed to addEventListener. If .bind() is called | ||
| // twice instead of storing the reference, these will be different | ||
| // functions and the listener will leak. | ||
| const added = el.firstChild.addEventListener.mock.calls | ||
| const removed = el.firstChild.removeEventListener.mock.calls | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| const [addEvent, addFn] = added[i] | ||
| const [removeEvent, removeFn] = removed[i] | ||
| expect(removeEvent).toBe(addEvent) | ||
| expect(removeFn).toBe(addFn) | ||
| } | ||
| }) | ||
|
|
||
| it('handles unbind of non-existent element gracefully', () => { | ||
| const el = createMockEl('unknown') | ||
|
|
||
| // Should not throw | ||
| expect(() => unbind(el)).not.toThrow() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
DroppableMailboxis constructed here with only(el, binding.value), but the class constructor signature isconstructor(el, componentInstance, options). This meansoptionsbecomesundefinedandoptions.mainStorewill throw at runtime. Passbinding.valueas the third argument (and optionally provide the Vue component instance as the second arg for Vue 2/3), or update theDroppableMailboxconstructor to match the new call site.