5180 fichiers 38.5 Mo 0 téléchargements 191 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / components / login / loadTurnstileScript.test.ts
loadTurnstileScript.test.ts 2.9 Ko Télécharger le fichier
/* @vitest-environment jsdom */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { loadTurnstileScript, resetTurnstileScriptForTests, TURNSTILE_SCRIPT_URL } from './loadTurnstileScript';

const scriptTag = () => document.querySelector<HTMLScriptElement>(`script[src="${TURNSTILE_SCRIPT_URL}"]`);

describe('loadTurnstileScript', () => {
    beforeEach(() => {
        vi.useFakeTimers();
        resetTurnstileScriptForTests();
        document.head.innerHTML = '';
        delete window.turnstile;
    });

    afterEach(() => {
        vi.useRealTimers();
    });

    it('does not touch the page until something asks for it', () => {
        expect(scriptTag()).toBeNull();
    });

    it('resolves once Cloudflare has answered', async () => {
        const loading = loadTurnstileScript();

        const script = scriptTag();
        expect(script).not.toBeNull();

        window.turnstile = { render: vi.fn(), reset: vi.fn(), remove: vi.fn() };
        script.dispatchEvent(new Event('load'));

        await expect(loading).resolves.toBe(true);
    });

    it('gives up when the script is blocked instead of waiting forever', async () => {
        const loading = loadTurnstileScript(10000);

        vi.advanceTimersByTime(10000);

        await expect(loading).resolves.toBe(false);
    });

    it('reports a failed load', async () => {
        const loading = loadTurnstileScript();

        scriptTag().dispatchEvent(new Event('error'));

        await expect(loading).resolves.toBe(false);
    });

    it('fetches the script once however many widgets ask', async () => {
        const first = loadTurnstileScript();
        const second = loadTurnstileScript();

        expect(document.querySelectorAll(`script[src="${TURNSTILE_SCRIPT_URL}"]`)).toHaveLength(1);

        window.turnstile = { render: vi.fn(), reset: vi.fn(), remove: vi.fn() };
        scriptTag().dispatchEvent(new Event('load'));

        await expect(first).resolves.toBe(true);
        await expect(second).resolves.toBe(true);
    });

    it('answers at once when Turnstile is already there', async () => {
        window.turnstile = { render: vi.fn(), reset: vi.fn(), remove: vi.fn() };

        await expect(loadTurnstileScript()).resolves.toBe(true);
        expect(scriptTag()).toBeNull();
    });

    it('retries with a fresh script after a failed load', async () => {
        const first = loadTurnstileScript();
        const failed = scriptTag();
        failed.dispatchEvent(new Event('error'));
        await expect(first).resolves.toBe(false);
        expect(scriptTag()).toBeNull();

        const retry = loadTurnstileScript();
        const fresh = scriptTag();
        expect(fresh).not.toBe(failed);

        window.turnstile = { render: vi.fn(), reset: vi.fn(), remove: vi.fn() };
        fresh.dispatchEvent(new Event('load'));

        await expect(retry).resolves.toBe(true);
    });
});
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser