Files
portal/app/tests/Frontend/CommandPalette.spec.ts
T

75 lines
2.8 KiB
TypeScript

import { describe, expect, test, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createVuetify } from 'vuetify';
import CommandPalette from '../../resources/js/components/layout/CommandPalette.vue';
import { useCommandPalette } from '../../resources/js/composables/useCommandPalette';
const pushMock = vi.fn();
vi.mock('vue-router', () => ({
useRouter: () => ({ push: pushMock }),
}));
const vuetify = createVuetify();
function mountPalette() {
return mount(CommandPalette, {
global: {
plugins: [vuetify],
stubs: { VDialog: { template: '<div><slot /></div>' } },
},
});
}
describe('CommandPalette (B3)', () => {
beforeEach(() => {
pushMock.mockClear();
useCommandPalette().closePalette(); // сброс singleton между тестами
});
test('по умолчанию показывает все 8 разделов', () => {
const wrapper = mountPalette();
expect(wrapper.vm.filteredItems).toHaveLength(8);
});
test('фильтрует по подстроке (case-insensitive)', () => {
const wrapper = mountPalette();
wrapper.vm.query = 'КАНБ';
expect(wrapper.vm.filteredItems).toHaveLength(1);
expect(wrapper.vm.filteredItems[0].to).toBe('/kanban');
});
test('пустой результат при отсутствии совпадений', () => {
const wrapper = mountPalette();
wrapper.vm.query = 'zzzнеттакого';
expect(wrapper.vm.filteredItems).toHaveLength(0);
});
test('selectItem навигирует и закрывает палитру', () => {
const { open } = useCommandPalette();
open.value = true;
const wrapper = mountPalette();
wrapper.vm.selectItem({ title: 'Сделки', icon: 'x', to: '/deals' });
expect(pushMock).toHaveBeenCalledWith('/deals');
expect(open.value).toBe(false);
});
test('onSubmit навигирует на первый отфильтрованный результат', () => {
const wrapper = mountPalette();
wrapper.vm.query = 'отч';
wrapper.vm.onSubmit();
expect(pushMock).toHaveBeenCalledWith('/reports');
});
test('рендерит 8 пунктов списка по умолчанию', () => {
const wrapper = mountPalette();
expect(wrapper.findAll('[data-testid="command-palette-item"]')).toHaveLength(8);
});
test('показывает пустое состояние при отсутствии совпадений', async () => {
const wrapper = mountPalette();
wrapper.vm.query = 'zzzнеттакого';
await wrapper.vm.$nextTick();
expect(wrapper.find('[data-testid="command-palette-empty"]').exists()).toBe(true);
});
});