interaction.test.ts

2.0 KB · raw

1// @vitest-environment jsdom23import { beforeEach, describe, expect, it } from "vitest";45import { resolveAutofillInteractionTargets } from "./interaction";67describe("resolveAutofillInteractionTargets", () => {8  beforeEach(() => {9    document.body.innerHTML = "";10  });1112  it("keeps passive focus gated when no credential form is present", () => {13    const anchor = document.createElement("input");14    anchor.type = "email";1516    const targets = resolveAutofillInteractionTargets(anchor, []);1718    expect(targets.shouldOpen).toBe(false);19    expect(targets.usernameEl).toBeNull();20    expect(targets.passwordEl).toBeNull();21  });2223  it("prefers detected username and password fields when they exist", () => {24    const anchor = document.createElement("input");25    anchor.type = "email";26    const username = document.createElement("input");27    username.type = "email";28    const password = document.createElement("input");29    password.type = "password";3031    const targets = resolveAutofillInteractionTargets(anchor, [32      { type: "username", el: username },33      { type: "password", el: password },34    ]);3536    expect(targets.shouldOpen).toBe(true);37    expect(targets.usernameEl).toBe(username);38    expect(targets.passwordEl).toBe(password);39  });4041  it("lets an explicit email-field click open Peach even if detection is partial", () => {42    const anchor = document.createElement("input");43    anchor.type = "email";4445    const targets = resolveAutofillInteractionTargets(anchor, [], true);4647    expect(targets.shouldOpen).toBe(true);48    expect(targets.usernameEl).toBe(anchor);49    expect(targets.passwordEl).toBeNull();50  });5152  it("lets an explicit password-field click open Peach even if detection is partial", () => {53    const anchor = document.createElement("input");54    anchor.type = "password";5556    const targets = resolveAutofillInteractionTargets(anchor, [], true);5758    expect(targets.shouldOpen).toBe(true);59    expect(targets.usernameEl).toBeNull();60    expect(targets.passwordEl).toBe(anchor);61  });62});