Skip to content

Commit

Permalink
test: added tests for utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 26, 2025
1 parent 47a1a23 commit 98deefd
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
47 changes: 47 additions & 0 deletions phpmyfaq/admin/assets/src/utils/session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { handleSessionTimeout } from './session';

vi.mock('bootstrap', () => ({
Modal: vi.fn().mockImplementation(() => ({
show: vi.fn(),
hide: vi.fn(),
})),
}));

describe('Session Utils', () => {
afterEach(() => {
vi.restoreAllMocks();
});

describe('handleSessionTimeout', () => {
it('should observe attribute changes and reload page on button click', () => {
document.body.innerHTML = `
<div id="pmf-show-session-warning" data-value="hide"></div>
<div id="sessionWarningModal"></div>
<button id="pmf-button-reload-page"></button>
`;

const mockObserver = {
observe: vi.fn(),
disconnect: vi.fn(),
takeRecords: vi.fn(),
};
global.MutationObserver = vi.fn(() => mockObserver) as unknown as typeof MutationObserver;

const mockReload = vi.fn();
Object.defineProperty(global, 'location', {
value: {
reload: mockReload,
},
writable: true,
});

handleSessionTimeout();

expect(mockObserver.observe).toHaveBeenCalled();
const reloadButton = document.getElementById('pmf-button-reload-page');
reloadButton?.click();
expect(mockReload).toHaveBeenCalled();
});
});
});
43 changes: 43 additions & 0 deletions phpmyfaq/admin/assets/src/utils/sidebar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { sidebarToggle } from './sidebar';

describe('Sidebar Utils', () => {
afterEach(() => {
vi.restoreAllMocks();
});

describe('sidebarToggle', () => {
it('should toggle sidebar class and update localStorage on button click', () => {
document.body.innerHTML = `
<button id="sidebarToggle"></button>
`;

const mockSetItem = vi.fn();
Object.defineProperty(global, 'localStorage', {
value: {
setItem: mockSetItem,
},
writable: true,
});

sidebarToggle();

const toggleButton = document.getElementById('sidebarToggle');
toggleButton?.click();

expect(document.body.classList.contains('pmf-admin-sidenav-toggled')).toBe(true);
expect(mockSetItem).toHaveBeenCalledWith('sb|sidebar-toggle', 'true');

toggleButton?.click();

expect(document.body.classList.contains('pmf-admin-sidenav-toggled')).toBe(false);
expect(mockSetItem).toHaveBeenCalledWith('sb|sidebar-toggle', 'false');
});

it('should not throw an error if sidebarToggle button is not found', () => {
document.body.innerHTML = ``;

expect(() => sidebarToggle()).not.toThrow();
});
});
});
69 changes: 69 additions & 0 deletions phpmyfaq/admin/assets/src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { selectAll, unSelectAll, formatBytes, initializeTooltips } from './utils';
import { Tooltip } from 'bootstrap';

vi.mock('bootstrap', () => ({
Tooltip: vi.fn(),
}));

describe('Utils', () => {
afterEach(() => {
vi.restoreAllMocks();
});

describe('selectAll', () => {
it('should select all options in the select element', () => {
document.body.innerHTML = `
<select id="testSelect" multiple>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
`;

selectAll('testSelect');

const selectElement = document.getElementById('testSelect') as HTMLSelectElement;
for (const option of selectElement.options) {
expect(option.selected).toBe(true);
}
});
});

describe('unSelectAll', () => {
it('should unselect all options in the select element', () => {
document.body.innerHTML = `
<select id="testSelect" multiple>
<option value="1" selected>Option 1</option>
<option value="2" selected>Option 2</option>
</select>
`;

unSelectAll('testSelect');

const selectElement = document.getElementById('testSelect') as HTMLSelectElement;
for (const option of selectElement.options) {
expect(option.selected).toBe(false);
}
});
});

describe('formatBytes', () => {
it('should format bytes correctly', () => {
expect(formatBytes(1024)).toBe('1 KiB');
expect(formatBytes(1048576)).toBe('1 MiB');
expect(formatBytes(0)).toBe('0 Bytes');
});
});

describe('initializeTooltips', () => {
it('should initialize tooltips', () => {
document.body.innerHTML = `
<div data-bs-toggle="tooltip" title="Tooltip text"></div>
`;

initializeTooltips();

expect(vi.mocked(Tooltip)).toHaveBeenCalled();
});
});
});

0 comments on commit 98deefd

Please sign in to comment.