-
Notifications
You must be signed in to change notification settings - Fork 132
fix: remove toolbar window listeners on unmount #2783
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
caio-pizzol
merged 5 commits into
superdoc-dev:main
from
ArturQuirino:fix/toolbar-unmount-window-listeners
Apr 20, 2026
+142
−12
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9dcea3f
fix(super-editor): remove toolbar window listeners on unmount
ArturQuirino 1b0bb80
fix: setup window listeners on onMounted and onActivated
ArturQuirino 3e53c75
test: assert Toolbar window listener cleanup
ArturQuirino 753408c
fix(super-editor): unmount toolbar Vue app on SuperToolbar.destroy
caio-pizzol 8d9641a
Merge branch 'main' into fix/toolbar-unmount-window-listeners
caio-pizzol 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
114 changes: 114 additions & 0 deletions
114
packages/super-editor/src/editors/v1/components/toolbar/Toolbar.test.js
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,114 @@ | ||
| import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
| import { mount } from '@vue/test-utils'; | ||
| import { defineComponent, ref, KeepAlive } from 'vue'; | ||
| import Toolbar from './Toolbar.vue'; | ||
|
|
||
| const ToolbarKeepAliveHost = defineComponent({ | ||
| components: { KeepAlive, Toolbar }, | ||
| setup() { | ||
| const visible = ref(true); | ||
| return { visible }; | ||
| }, | ||
| template: '<KeepAlive><Toolbar v-if="visible" /></KeepAlive>', | ||
| }); | ||
|
|
||
| function createMockToolbar() { | ||
| return { | ||
| config: { | ||
| toolbarGroups: ['left', 'center', 'right'], | ||
| toolbarButtonsExclude: [], | ||
| }, | ||
| getToolbarItemByGroup: () => [], | ||
| getToolbarItemByName: () => null, | ||
| onToolbarResize: vi.fn(), | ||
| emitCommand: vi.fn(), | ||
| overflowItems: [], | ||
| activeEditor: null, | ||
| }; | ||
| } | ||
|
|
||
| describe('Toolbar', () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('removes resize and keydown listeners on unmount (not only on KeepAlive deactivate)', () => { | ||
| const mockToolbar = createMockToolbar(); | ||
| const addSpy = vi.spyOn(window, 'addEventListener'); | ||
| const removeSpy = vi.spyOn(window, 'removeEventListener'); | ||
|
|
||
| const wrapper = mount(Toolbar, { | ||
| global: { | ||
| stubs: { ButtonGroup: true }, | ||
| plugins: [ | ||
| (app) => { | ||
| app.config.globalProperties.$toolbar = mockToolbar; | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const resizeHandler = addSpy.mock.calls.find((c) => c[0] === 'resize')?.[1]; | ||
| const keydownHandler = addSpy.mock.calls.find((c) => c[0] === 'keydown')?.[1]; | ||
| expect(resizeHandler).toBeTypeOf('function'); | ||
| expect(keydownHandler).toBeTypeOf('function'); | ||
|
|
||
| removeSpy.mockClear(); | ||
| wrapper.unmount(); | ||
|
|
||
| expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler); | ||
| expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler); | ||
|
|
||
| addSpy.mockRestore(); | ||
| removeSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('removes window listeners on KeepAlive deactivate and restores them on activate', async () => { | ||
| const mockToolbar = createMockToolbar(); | ||
| const addSpy = vi.spyOn(window, 'addEventListener'); | ||
| const removeSpy = vi.spyOn(window, 'removeEventListener'); | ||
|
|
||
| const wrapper = mount(ToolbarKeepAliveHost, { | ||
| global: { | ||
| stubs: { ButtonGroup: true }, | ||
| plugins: [ | ||
| (app) => { | ||
| app.config.globalProperties.$toolbar = mockToolbar; | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const resizeHandler = addSpy.mock.calls.find((c) => c[0] === 'resize')?.[1]; | ||
| const keydownHandler = addSpy.mock.calls.find((c) => c[0] === 'keydown')?.[1]; | ||
| expect(resizeHandler).toBeTypeOf('function'); | ||
| expect(keydownHandler).toBeTypeOf('function'); | ||
|
|
||
| addSpy.mockClear(); | ||
| removeSpy.mockClear(); | ||
|
|
||
| wrapper.vm.visible = false; | ||
| await wrapper.vm.$nextTick(); | ||
|
|
||
| expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler); | ||
| expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler); | ||
|
|
||
| addSpy.mockClear(); | ||
| removeSpy.mockClear(); | ||
|
|
||
| wrapper.vm.visible = true; | ||
| await wrapper.vm.$nextTick(); | ||
|
|
||
| expect(addSpy).toHaveBeenCalledWith('resize', resizeHandler); | ||
| expect(addSpy).toHaveBeenCalledWith('keydown', keydownHandler); | ||
|
|
||
| removeSpy.mockClear(); | ||
| wrapper.unmount(); | ||
|
|
||
| expect(removeSpy).toHaveBeenCalledWith('resize', resizeHandler); | ||
| expect(removeSpy).toHaveBeenCalledWith('keydown', keydownHandler); | ||
|
|
||
| addSpy.mockRestore(); | ||
| removeSpy.mockRestore(); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.