Files
ss-tools/frontend/src/lib/stores/__tests__/sidebar.test.js
2026-02-19 16:05:59 +03:00

116 lines
3.8 KiB
JavaScript

// @RELATION: VERIFIES -> frontend/src/lib/stores/sidebar.js
// [DEF:frontend.src.lib.stores.__tests__.sidebar:Module]
// @TIER: STANDARD
// @PURPOSE: Unit tests for sidebar store
// @LAYER: Domain (Tests)
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { get } from 'svelte/store';
import { sidebarStore, toggleSidebar, setActiveItem, setMobileOpen, closeMobile, toggleMobileSidebar } from '../sidebar.js';
// Mock the $app/environment module
vi.mock('$app/environment', () => ({
browser: false
}));
describe('SidebarStore', () => {
// [DEF:test_sidebar_initial_state:Function]
// @TEST: Store initializes with default values
// @PRE: No localStorage state
// @POST: Default state is { isExpanded: true, activeCategory: 'dashboards', activeItem: '/dashboards', isMobileOpen: false }
describe('initial state', () => {
it('should have default values when no localStorage', () => {
const state = get(sidebarStore);
expect(state.isExpanded).toBe(true);
expect(state.activeCategory).toBe('dashboards');
expect(state.activeItem).toBe('/dashboards');
expect(state.isMobileOpen).toBe(false);
});
});
// [DEF:test_toggleSidebar:Function]
// @TEST: toggleSidebar toggles isExpanded state
// @PRE: Store is initialized
// @POST: isExpanded is toggled from previous value
describe('toggleSidebar', () => {
it('should toggle isExpanded from true to false', () => {
const initialState = get(sidebarStore);
expect(initialState.isExpanded).toBe(true);
toggleSidebar();
const newState = get(sidebarStore);
expect(newState.isExpanded).toBe(false);
});
it('should toggle isExpanded from false to true', () => {
toggleSidebar(); // Now false
toggleSidebar(); // Should be true again
const state = get(sidebarStore);
expect(state.isExpanded).toBe(true);
});
});
// [DEF:test_setActiveItem:Function]
// @TEST: setActiveItem updates activeCategory and activeItem
// @PRE: Store is initialized
// @POST: activeCategory and activeItem are updated
describe('setActiveItem', () => {
it('should update activeCategory and activeItem', () => {
setActiveItem('datasets', '/datasets');
const state = get(sidebarStore);
expect(state.activeCategory).toBe('datasets');
expect(state.activeItem).toBe('/datasets');
});
it('should update to admin category', () => {
setActiveItem('admin', '/settings');
const state = get(sidebarStore);
expect(state.activeCategory).toBe('admin');
expect(state.activeItem).toBe('/settings');
});
});
// [DEF:test_mobile_functions:Function]
// @TEST: Mobile functions correctly update isMobileOpen
// @PRE: Store is initialized
// @POST: isMobileOpen is correctly updated
describe('mobile functions', () => {
it('should set isMobileOpen to true with setMobileOpen', () => {
setMobileOpen(true);
const state = get(sidebarStore);
expect(state.isMobileOpen).toBe(true);
});
it('should set isMobileOpen to false with closeMobile', () => {
setMobileOpen(true);
closeMobile();
const state = get(sidebarStore);
expect(state.isMobileOpen).toBe(false);
});
it('should toggle isMobileOpen with toggleMobileSidebar', () => {
const initialState = get(sidebarStore);
const initialMobileOpen = initialState.isMobileOpen;
toggleMobileSidebar();
const state1 = get(sidebarStore);
expect(state1.isMobileOpen).toBe(!initialMobileOpen);
toggleMobileSidebar();
const state2 = get(sidebarStore);
expect(state2.isMobileOpen).toBe(initialMobileOpen);
});
});
});
// [/DEF:frontend.src.lib.stores.__tests__.sidebar:Module]