4 Commits

Author SHA1 Message Date
7da99efc85 release: 1.0.11
All checks were successful
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 78.38% (551/703) * Lines of Code: 703 #### Quality Gates Summary No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 88
Frontend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 84.57% (581/687) * Lines of Code: 687 #### Quality Gates Summary No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head This commit looks good
2026-09-20 02:18:40 +00:00
f4786e970a fix: properly cleanup turnstile on unmount
All checks were successful
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 78.38% (551/703) * Lines of Code: 703 #### Quality Gates Summary No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 88
Frontend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 84.57% (581/687) * Lines of Code: 687 #### Quality Gates Summary No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head This commit looks good
2026-09-20 02:12:14 +00:00
d207b0221a fix: properly cleanup turnstile on unmount 2026-09-20 02:09:54 +00:00
361cf46905 fix: reload turnstile on SPA navigation
Some checks failed
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 78.66% (553/703) * Lines of Code: 703 #### Quality Gates Summary No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 83
Frontend Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 80.20% (555/692) * Lines of Code: 692 #### Quality Gates Summary No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head There was a failure building this commit
2026-09-20 01:53:31 +00:00
2 changed files with 170 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ export default function Captcha({ onToken }) {
}
let cancelled = false
let localWidgetId = null
const renderWidget = () => {
if (
@@ -25,19 +26,21 @@ export default function Captcha({ onToken }) {
return
}
if (widgetIdRef.current !== null) {
// Nettoyage d'un éventuel widget précédent.
if (localWidgetId !== null) {
try {
window.turnstile.remove(widgetIdRef.current)
window.turnstile.remove(localWidgetId)
} catch {
// Widget déjà supprimé
// Widget déjà supprimé.
}
localWidgetId = null
widgetIdRef.current = null
}
containerRef.current.innerHTML = ''
widgetIdRef.current = window.turnstile.render(
localWidgetId = window.turnstile.render(
containerRef.current,
{
sitekey: TURNSTILE_SITE_KEY,
@@ -58,6 +61,8 @@ export default function Captcha({ onToken }) {
},
}
)
widgetIdRef.current = localWidgetId
}
const existingScript = document.getElementById(
@@ -66,19 +71,11 @@ export default function Captcha({ onToken }) {
if (window.turnstile) {
renderWidget()
return
}
if (existingScript) {
} else if (existingScript) {
existingScript.addEventListener('load', renderWidget, {
once: true,
})
return () => {
cancelled = true
}
}
} else {
const script = document.createElement('script')
script.id = TURNSTILE_SCRIPT_ID
@@ -91,21 +88,25 @@ export default function Captcha({ onToken }) {
})
document.head.appendChild(script)
}
return () => {
cancelled = true
if (
widgetIdRef.current !== null &&
window.turnstile
) {
const widgetId =
localWidgetId !== null
? localWidgetId
: widgetIdRef.current
if (widgetId !== null && window.turnstile) {
try {
window.turnstile.remove(widgetIdRef.current)
window.turnstile.remove(widgetId)
} catch {
// Widget déjà supprimé
// Widget déjà supprimé.
}
}
localWidgetId = null
widgetIdRef.current = null
}
}, [onToken])

View File

@@ -0,0 +1,139 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { render, cleanup, waitFor } from '@testing-library/react'
vi.mock('../../config/captcha', () => ({
CAPTCHA_ENABLED: true,
TURNSTILE_SITE_KEY: '0x4AAAAAAE0AF9mR2Y1luOeG',
}))
import Captcha from '../../components/Captcha'
describe('Captcha', () => {
let originalTurnstile
beforeEach(() => {
originalTurnstile = window.turnstile
document.head
.querySelectorAll('#cf-turnstile-script')
.forEach((script) => script.remove())
window.turnstile = undefined
})
afterEach(() => {
cleanup()
window.turnstile = originalTurnstile
vi.restoreAllMocks()
})
it('rend le widget lorsque Turnstile est déjà disponible', async () => {
const onToken = vi.fn()
window.turnstile = {
render: vi.fn((container, options) => {
options.callback('token-test')
return 'widget-1'
}),
remove: vi.fn(),
}
render(<Captcha onToken={onToken} />)
await waitFor(() => {
expect(window.turnstile.render).toHaveBeenCalled()
})
expect(onToken).toHaveBeenCalledWith('token-test')
})
it('gère expiration et erreur du widget', async () => {
const onToken = vi.fn()
let options
window.turnstile = {
render: vi.fn((container, opts) => {
options = opts
return 'widget-2'
}),
remove: vi.fn(),
}
render(<Captcha onToken={onToken} />)
await waitFor(() => {
expect(options).toBeDefined()
})
options['expired-callback']()
options['error-callback']()
expect(onToken).toHaveBeenNthCalledWith(1, '')
expect(onToken).toHaveBeenNthCalledWith(2, '')
})
it('charge Turnstile lorsque le script existe déjà', async () => {
const onToken = vi.fn()
const script = document.createElement('script')
script.id = 'cf-turnstile-script'
document.head.appendChild(script)
render(<Captcha onToken={onToken} />)
window.turnstile = {
render: vi.fn(() => 'widget-3'),
remove: vi.fn(),
}
script.dispatchEvent(new Event('load'))
await waitFor(() => {
expect(window.turnstile.render).toHaveBeenCalled()
})
})
it('supprime le widget au démontage', async () => {
const onToken = vi.fn()
const remove = vi.fn()
window.turnstile = {
render: vi.fn(() => 'widget-4'),
remove,
}
const { unmount } = render(<Captcha onToken={onToken} />)
await waitFor(() => {
expect(window.turnstile.render).toHaveBeenCalledTimes(1)
})
// Le widget est bien créé avant le démontage.
unmount()
expect(remove).toHaveBeenCalledWith('widget-4')
})
it('tolère une erreur lors de la suppression du widget', async () => {
const onToken = vi.fn()
const remove = vi.fn(() => {
throw new Error('widget déjà supprimé')
})
window.turnstile = {
render: vi.fn(() => 'widget-5'),
remove,
}
const { unmount } = render(<Captcha onToken={onToken} />)
await waitFor(() => {
expect(window.turnstile.render).toHaveBeenCalledTimes(1)
})
expect(() => unmount()).not.toThrow()
expect(remove).toHaveBeenCalledWith('widget-5')
})
})