Add JS tests, refactor SitemapController, extract JS modules

- Extract mobile-menu.js and tabs.js from app.js
- Add Vitest tests with happy-dom and v8 coverage (100% on modules)
- Add JS test step to CI frontend and SonarQube workflows
- SonarQube: add JS lcov coverage report path
- SitemapController: extract URLSET_TEMPLATE constant, deduplicate methods

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-19 00:19:34 +01:00
parent d34a684552
commit 922e8c5e02
26 changed files with 1632 additions and 41 deletions

View File

@@ -0,0 +1,48 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { initMobileMenu } from '../../assets/modules/mobile-menu.js'
describe('initMobileMenu', () => {
beforeEach(() => {
document.body.innerHTML = `
<button id="mobile-menu-btn" aria-expanded="false">
<svg id="menu-icon-open"></svg>
<svg id="menu-icon-close" class="hidden"></svg>
</button>
<div id="mobile-menu" class="hidden"></div>
`
})
it('toggles menu visibility on click', () => {
initMobileMenu()
const btn = document.getElementById('mobile-menu-btn')
const menu = document.getElementById('mobile-menu')
btn.click()
expect(menu.classList.contains('hidden')).toBe(false)
expect(btn.getAttribute('aria-expanded')).toBe('true')
btn.click()
expect(menu.classList.contains('hidden')).toBe(true)
expect(btn.getAttribute('aria-expanded')).toBe('false')
})
it('toggles icons on click', () => {
initMobileMenu()
const btn = document.getElementById('mobile-menu-btn')
const iconOpen = document.getElementById('menu-icon-open')
const iconClose = document.getElementById('menu-icon-close')
btn.click()
expect(iconOpen.classList.contains('hidden')).toBe(true)
expect(iconClose.classList.contains('hidden')).toBe(false)
btn.click()
expect(iconOpen.classList.contains('hidden')).toBe(false)
expect(iconClose.classList.contains('hidden')).toBe(true)
})
it('does nothing without elements', () => {
document.body.innerHTML = ''
expect(() => initMobileMenu()).not.toThrow()
})
})

54
tests/js/tabs.test.js Normal file
View File

@@ -0,0 +1,54 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { initTabs } from '../../assets/modules/tabs.js'
describe('initTabs', () => {
beforeEach(() => {
document.body.innerHTML = `
<button data-tab="tab-a" style="background-color:#111827;color:white;">Tab A</button>
<button data-tab="tab-b" style="background-color:white;color:#111827;">Tab B</button>
<div id="tab-a" style="display:block;">Content A</div>
<div id="tab-b" style="display:none;">Content B</div>
`
})
it('switches active tab on click', () => {
initTabs()
const btnB = document.querySelector('[data-tab="tab-b"]')
btnB.click()
expect(document.getElementById('tab-a').style.display).toBe('none')
expect(document.getElementById('tab-b').style.display).toBe('block')
})
it('updates button styles on click', () => {
initTabs()
const btnA = document.querySelector('[data-tab="tab-a"]')
const btnB = document.querySelector('[data-tab="tab-b"]')
btnB.click()
expect(btnB.style.backgroundColor).toBe('#111827')
expect(btnB.style.color).toBe('white')
expect(btnA.style.backgroundColor).toBe('white')
expect(btnA.style.color).toBe('#111827')
})
it('switches back to first tab', () => {
initTabs()
const btnA = document.querySelector('[data-tab="tab-a"]')
const btnB = document.querySelector('[data-tab="tab-b"]')
btnB.click()
btnA.click()
expect(document.getElementById('tab-a').style.display).toBe('block')
expect(document.getElementById('tab-b').style.display).toBe('none')
})
it('does nothing without tab elements', () => {
document.body.innerHTML = ''
expect(() => initTabs()).not.toThrow()
})
})