-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added tests for utility functions
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |