Add unit tests for ActionToolbar component in gamemaster module

This commit is contained in:
2026-01-29 03:57:44 +00:00
parent f16d261476
commit 6f89686862

View File

@@ -0,0 +1,101 @@
/**
* ActionToolbar Component Tests
* Verifies action buttons behavior
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import { ref } from 'vue';
import ActionToolbar from '../../../../src/components/gamemaster/ActionToolbar.vue';
import { useLineSelection } from '../../../../src/composables/useLineSelection.js';
vi.mock('../../../../src/composables/useLineSelection.js', () => ({
useLineSelection: vi.fn()
}));
const createSelectionMock = overrides => ({
selectionCount: ref(0),
copySelected: vi.fn(),
copyAll: vi.fn(),
exportSelected: vi.fn(),
exportAll: vi.fn(),
shareUrl: vi.fn(),
...overrides
});
describe('ActionToolbar Component', () => {
let selectionMock;
beforeEach(() => {
selectionMock = createSelectionMock();
useLineSelection.mockReturnValue(selectionMock);
});
it('renders when file content exists', () => {
const wrapper = mount(ActionToolbar, {
props: {
fileContent: '{"test": true}',
displayLines: [],
selectedFile: 'pokemon'
}
});
expect(wrapper.find('.action-bar').exists()).toBe(true);
});
it('disables selection-dependent actions when no selection', () => {
const wrapper = mount(ActionToolbar, {
props: {
fileContent: '{"test": true}',
displayLines: [],
selectedFile: 'pokemon'
}
});
const buttons = wrapper.findAll('button.btn-action');
expect(buttons[0].attributes('disabled')).toBeDefined();
expect(buttons[2].attributes('disabled')).toBeDefined();
});
it('enables selection-dependent actions when selection exists', () => {
selectionMock.selectionCount.value = 3;
const wrapper = mount(ActionToolbar, {
props: {
fileContent: '{"test": true}',
displayLines: [],
selectedFile: 'pokemon'
}
});
const buttons = wrapper.findAll('button.btn-action');
expect(buttons[0].attributes('disabled')).toBeUndefined();
expect(buttons[2].attributes('disabled')).toBeUndefined();
expect(buttons[0].text()).toContain('3 lines');
});
it('triggers action handlers', async () => {
selectionMock.selectionCount.value = 2;
const wrapper = mount(ActionToolbar, {
props: {
fileContent: '{"test": true}',
displayLines: [],
selectedFile: 'pokemon'
}
});
const buttons = wrapper.findAll('button.btn-action');
await buttons[0].trigger('click');
await buttons[1].trigger('click');
await buttons[2].trigger('click');
await buttons[3].trigger('click');
await buttons[4].trigger('click');
expect(selectionMock.copySelected).toHaveBeenCalled();
expect(selectionMock.copyAll).toHaveBeenCalled();
expect(selectionMock.exportSelected).toHaveBeenCalled();
expect(selectionMock.exportAll).toHaveBeenCalled();
expect(selectionMock.shareUrl).toHaveBeenCalled();
});
});