Files
nimbus/resources/js/tests/stores/useGeneratorCommandStore.test.ts
Mazen Touati 6ba071dc98 test: front-end tests cleanup (round 1)
the aim is to make the tests more about the behavior rather than implementation, add some missing tests, and improve the code.
2025-11-16 19:03:40 +01:00

53 lines
1.8 KiB
TypeScript

import { ValueGeneratorCommandOpenMethod } from '@/interfaces/ui';
import { useGeneratorCommandStore } from '@/stores/generators/useGeneratorCommandStore';
import { createPinia, setActivePinia } from 'pinia';
import { beforeEach, describe, expect, it } from 'vitest';
describe('useGeneratorCommandStore', () => {
beforeEach(() => {
setActivePinia(createPinia());
});
it('opens and closes command while tracking input refs', () => {
const store = useGeneratorCommandStore();
const input = document.createElement('input');
store.openCommand(input, ValueGeneratorCommandOpenMethod.SHIFT_SHIFT);
expect(store.isCommandOpen).toBe(true);
expect(store.currentInputRef).toBe(input);
expect(store.wasOpenedViaShiftShift).toBe(true);
store.closeCommand();
expect(store.isCommandOpen).toBe(false);
expect(store.currentInputRef).toBeNull();
});
it('updates command state and maintains recent generators cap', () => {
const store = useGeneratorCommandStore();
store.setSearchQuery('email');
store.setSelectedCategory('strings');
store.addToRecentGenerators('uuid');
store.addToRecentGenerators('email');
store.addToRecentGenerators('uuid'); // promotes existing entry
expect(store.commandState.searchQuery).toBe('email');
expect(store.commandState.selectedCategory).toBe('strings');
expect(store.commandState.recentGenerators).toEqual(['uuid', 'email']);
});
it('restores prior command search when command has no query', () => {
const store = useGeneratorCommandStore();
store.setSearchQuery('date');
const commandInstance = { filterState: { search: '' } };
store.restoreCommandState(commandInstance);
expect(commandInstance.filterState.search).toBe('date');
});
});