Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)', () => {
Comment thread
caio-pizzol marked this conversation as resolved.
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();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<script setup>
import { ref, getCurrentInstance, onMounted, onDeactivated, nextTick, computed } from 'vue';
import {
ref,
getCurrentInstance,
onMounted,
onActivated,
onDeactivated,
onBeforeUnmount,
nextTick,
computed,
} from 'vue';
import { throttle } from './helpers.js';
import ButtonGroup from './ButtonGroup.vue';

Expand Down Expand Up @@ -40,16 +49,6 @@ const getFilteredItems = (position) => {
return proxy.$toolbar.getToolbarItemByGroup(position).filter((item) => !excludeButtonsList.includes(item.name.value));
};

onMounted(() => {
window.addEventListener('resize', onResizeThrottled);
window.addEventListener('keydown', onKeyDown);
});

onDeactivated(() => {
window.removeEventListener('resize', onResizeThrottled);
window.removeEventListener('keydown', onKeyDown);
});

const onKeyDown = async (e) => {
if (e.metaKey && e.key === 'f') {
const searchItem = proxy.$toolbar.getToolbarItemByName('search');
Expand All @@ -70,6 +69,21 @@ const onWindowResized = async () => {
};
const onResizeThrottled = throttle(onWindowResized, 300);

function teardownWindowListeners() {
window.removeEventListener('resize', onResizeThrottled);
window.removeEventListener('keydown', onKeyDown);
}

function setupWindowListeners() {
window.addEventListener('resize', onResizeThrottled);
window.addEventListener('keydown', onKeyDown);
}

onMounted(setupWindowListeners);
onActivated(setupWindowListeners);
onDeactivated(teardownWindowListeners);
Comment thread
caio-pizzol marked this conversation as resolved.
onBeforeUnmount(teardownWindowListeners);

const handleCommand = ({ item, argument, option }) => {
proxy.$toolbar.emitCommand({ item, argument, option });
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,8 @@ export class SuperToolbar extends EventEmitter {

/**
* Cleans up resources when the toolbar is destroyed.
* Clears any pending timeouts to prevent callbacks firing after unmount.
* Clears any pending timeouts and unmounts the Vue app so the Toolbar
* component's onBeforeUnmount hook runs and removes its window listeners.
* @returns {void}
*/
destroy() {
Expand All @@ -986,5 +987,6 @@ export class SuperToolbar extends EventEmitter {
}

this.destroyHeadlessToolbar();
this.app?.unmount();
}
}
Loading