Files
e-ticket/tests/js/editor.test.js
Serreau Jovann 8b3b1dab13 Add Event entity, create event page, and custom WYSIWYG editor component
- Create Event entity with fields: account, title, description (text), startAt, endAt, address, zipcode, city, eventMainPicture (VichUploader), isOnline, createdAt, updatedAt
- Create EventRepository
- Add migration for event table with all columns
- Add "Creer un evenement" button on account events tab
- Add create event page (/mon-compte/evenement/creer) with full form
- Build custom web component <e-ticket-editor> WYSIWYG editor:
  - Toolbar: bold, italic, underline, paragraph, bullet list, remove formatting
  - contentEditable div with HTML sync to hidden textarea
  - HTML sanitizer (strips disallowed tags, XSS safe)
  - Neo-brutalist CSS styling
  - CSP compliant (no inline styles)
- Register editor in app.js via customElements.define
- Add editor CSS in app.scss
- Add 16 Event entity tests (all fields + isOnline + picture upload + updatedAt)
- Add 16 editor JS tests (sanitizer + custom element lifecycle)
- Add 3 AccountController tests (create event page, submit, access control)
- Update placeholders to generic examples (no association-specific data)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-20 12:49:24 +01:00

118 lines
4.0 KiB
JavaScript

import { describe, it, expect, beforeEach } from 'vitest'
import { sanitizeHtml, ETicketEditor, registerEditor } from '../../assets/modules/editor.js'
describe('sanitizeHtml', () => {
it('keeps allowed tags', () => {
const html = '<p>Hello <b>world</b></p>'
expect(sanitizeHtml(html)).toBe('<p>Hello <b>world</b></p>')
})
it('strips disallowed tags but keeps content', () => {
const html = '<div><span>Hello</span></div>'
expect(sanitizeHtml(html)).toBe('Hello')
})
it('strips links but keeps text', () => {
const html = '<a href="https://example.com">Link</a>'
expect(sanitizeHtml(html)).toBe('Link')
})
it('keeps list elements', () => {
const html = '<ul><li>Item 1</li><li>Item 2</li></ul>'
expect(sanitizeHtml(html)).toBe('<ul><li>Item 1</li><li>Item 2</li></ul>')
})
it('strips blockquote but keeps content', () => {
const html = '<blockquote>Citation</blockquote>'
expect(sanitizeHtml(html)).toBe('Citation')
})
it('returns empty string for empty input', () => {
expect(sanitizeHtml('')).toBe('')
})
it('strips headings but keeps content', () => {
const html = '<h2>Title</h2><h3>Subtitle</h3>'
expect(sanitizeHtml(html)).toBe('TitleSubtitle')
})
it('strips style attributes from allowed tags', () => {
const html = '<p style="color:red">Text</p>'
expect(sanitizeHtml(html)).toBe('<p>Text</p>')
})
})
function createEditor(innerHtml = '<textarea></textarea>') {
registerEditor()
document.body.innerHTML = ''
const el = document.createElement('e-ticket-editor')
el.innerHTML = innerHtml
document.body.appendChild(el)
el.connectedCallback()
return el
}
describe('ETicketEditor', () => {
beforeEach(() => {
registerEditor()
})
it('registers the custom element', () => {
expect(globalThis.customElements.get('e-ticket-editor')).toBe(ETicketEditor)
})
it('hides the textarea and creates toolbar + content area', () => {
const editor = createEditor('<textarea placeholder="Ecrivez ici...">Hello</textarea>')
const textarea = editor.querySelector('textarea')
const toolbar = editor.querySelector('.ete-toolbar')
const content = editor.querySelector('.ete-content')
expect(textarea.style.display).toBe('none')
expect(toolbar).not.toBeNull()
expect(content).not.toBeNull()
expect(content.getAttribute('contenteditable')).toBe('true')
expect(content.innerHTML).toBe('Hello')
expect(content.dataset.placeholder).toBe('Ecrivez ici...')
})
it('does nothing without a textarea', () => {
const editor = createEditor('')
expect(editor.querySelector('.ete-toolbar')).toBeNull()
expect(editor.querySelector('.ete-content')).toBeNull()
})
it('toolbar has buttons', () => {
const editor = createEditor()
const buttons = editor.querySelectorAll('.ete-btn')
expect(buttons.length).toBeGreaterThan(0)
})
it('toolbar has separators', () => {
const editor = createEditor()
const separators = editor.querySelectorAll('.ete-separator')
expect(separators.length).toBeGreaterThan(0)
})
it('getHtml returns textarea value', () => {
const editor = createEditor('<textarea>Content</textarea>')
expect(editor.getHtml()).toBe('Content')
})
it('prevents tab key default in content area', () => {
const editor = createEditor()
const content = editor.querySelector('.ete-content')
const event = new KeyboardEvent('keydown', { key: 'Tab', cancelable: true })
content.dispatchEvent(event)
expect(event.defaultPrevented).toBe(true)
})
it('allows non-tab keys', () => {
const editor = createEditor()
const content = editor.querySelector('.ete-content')
const event = new KeyboardEvent('keydown', { key: 'Enter', cancelable: true })
content.dispatchEvent(event)
expect(event.defaultPrevented).toBe(false)
})
})