import assert from "node:assert"; import { describe, it } from "node:test"; import { CaptchaCollection } from "./index"; describe("Captcha plugin", () => { it("keeps loading state when API load is already in progress", () => { CaptchaCollection.state = { isScriptLoaded: false, isScriptLoading: true, isAPIReady: false, }; CaptchaCollection.loadAPI(); assert.equal(CaptchaCollection.state.isScriptLoading, true); assert.equal(CaptchaCollection.selector, "[data-js-google-captcha]"); }); it("has correct selector", () => { assert.equal(CaptchaCollection.selector, "[data-js-google-captcha]"); }); it("has correct event bubble names", () => { assert.equal(CaptchaCollection.bubbles.mount, "collection::mount"); assert.equal(CaptchaCollection.bubbles.unmount, "collection::unmount"); assert.equal(CaptchaCollection.bubbles.ready, "googleCaptchaReady"); assert.equal(CaptchaCollection.bubbles.render, "googleCaptchaRender"); assert.equal(CaptchaCollection.bubbles.reset, "googleCaptchaReset"); assert.equal(CaptchaCollection.bubbles.verify, "googleCaptchaVerify"); assert.equal(CaptchaCollection.bubbles.expire, "googleCaptchaExpire"); assert.equal(CaptchaCollection.bubbles.error, "googleCaptchaError"); }); it("initializes with correct default state", () => { CaptchaCollection.state = { isScriptLoaded: false, isScriptLoading: false, isAPIReady: false, }; assert.equal(CaptchaCollection.state.isScriptLoaded, false); assert.equal(CaptchaCollection.state.isScriptLoading, false); assert.equal(CaptchaCollection.state.isAPIReady, false); }); it("does not load API when already loaded", () => { const initialState = { ...CaptchaCollection.state }; CaptchaCollection.state = { isScriptLoaded: true, isScriptLoading: false, isAPIReady: false, }; CaptchaCollection.loadAPI(); assert.equal(CaptchaCollection.state.isScriptLoaded, true); CaptchaCollection.state = initialState; }); it("maintains state structure", () => { assert.ok(typeof CaptchaCollection.state === "object"); assert.ok("isScriptLoaded" in CaptchaCollection.state); assert.ok("isScriptLoading" in CaptchaCollection.state); assert.ok("isAPIReady" in CaptchaCollection.state); }); it("does not reinitiate loading when already in progress", () => { CaptchaCollection.state = { isScriptLoaded: false, isScriptLoading: true, isAPIReady: false, }; const beforeState = { ...CaptchaCollection.state }; CaptchaCollection.loadAPI(); assert.deepEqual(CaptchaCollection.state, beforeState); }); });