Add cart.js coverage: redirect, no-redirect, fetch error, invalid price

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-21 16:03:17 +01:00
parent 67147c1f5b
commit 3744fb84f1

View File

@@ -158,6 +158,80 @@ describe('initCart', () => {
})
})
it('redirects after successful checkout', async () => {
const fetchMock = vi.fn().mockResolvedValue({
json: () => Promise.resolve({ redirect: '/commande/1/paiement' }),
})
globalThis.fetch = fetchMock
globalThis.location = { href: '' }
createBilletterie([{ id: 1, price: '10.00', max: 5 }])
initCart()
document.querySelector('[data-cart-plus]').click()
document.getElementById('cart-checkout').click()
await new Promise(r => setTimeout(r, 10))
expect(globalThis.location.href).toBe('/commande/1/paiement')
})
it('does not redirect when response has no redirect', async () => {
const fetchMock = vi.fn().mockResolvedValue({
json: () => Promise.resolve({}),
})
globalThis.fetch = fetchMock
globalThis.location = { href: '/original' }
createBilletterie([{ id: 1, price: '10.00', max: 5 }])
initCart()
document.querySelector('[data-cart-plus]').click()
document.getElementById('cart-checkout').click()
await new Promise(r => setTimeout(r, 10))
expect(globalThis.location.href).toBe('/original')
})
it('re-enables button on fetch error', async () => {
const fetchMock = vi.fn().mockRejectedValue(new Error('Network error'))
globalThis.fetch = fetchMock
createBilletterie([{ id: 1, price: '10.00', max: 5 }])
initCart()
document.querySelector('[data-cart-plus]').click()
document.getElementById('cart-checkout').click()
await new Promise(r => setTimeout(r, 10))
const btn = document.getElementById('cart-checkout')
expect(btn.disabled).toBe(false)
expect(btn.textContent).toBe('Commander')
})
it('handles invalid price gracefully', () => {
document.body.innerHTML = `
<div id="billetterie">
<div data-cart-item data-billet-id="1" data-price="invalid" data-max="5">
<button data-cart-minus></button>
<input data-cart-qty type="number" min="0" max="5" value="0" readonly>
<button data-cart-plus></button>
<span data-cart-line-total></span>
</div>
<span id="cart-total"></span><span id="cart-count"></span>
<button id="cart-checkout" disabled data-order-url="/order"></button>
</div>
`
initCart()
document.querySelector('[data-cart-plus]').click()
expect(document.getElementById('cart-total').textContent).toBe('0,00 \u20AC')
expect(document.getElementById('cart-count').textContent).toBe('1')
})
it('does not post when cart is empty on checkout', () => {
const fetchMock = vi.fn()
globalThis.fetch = fetchMock