Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/components/PATModal.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useId } from "react";
import { FiEye, FiEyeOff, FiSave, FiTrash2, FiX } from "react-icons/fi";
import { useApp } from "../context/AppContext";

export default function PATModal({ open, onClose }) {
const { pat, savePat } = useApp();
const tokenId = useId();

const [draft, setDraft] = useState(pat);
const [show, setShow] = useState(false);
Expand Down Expand Up @@ -144,16 +145,18 @@ export default function PATModal({ open, onClose }) {
padding: 24,
}}
>
<div
<label
htmlFor={tokenId}
style={{
fontSize: 12,
color: "var(--text2)",
marginBottom: 6,
letterSpacing: ".06em",
cursor: "pointer",
}}
>
PERSONAL ACCESS TOKEN
</div>
Personal access token
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
</label>

<div
style={{
Expand All @@ -163,6 +166,7 @@ export default function PATModal({ open, onClose }) {
}}
>
<input
id={tokenId}
type={show ? "text" : "password"}
value={draft}
onChange={(e) => setDraft(e.target.value)}
Expand All @@ -179,6 +183,7 @@ export default function PATModal({ open, onClose }) {

<button
type="button"
aria-label={show ? "Hide token" : "Show token"}
onClick={() => setShow((s) => !s)}
style={{
padding: "0 14px",
Expand Down
36 changes: 36 additions & 0 deletions src/components/PATModal.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import PATModal from "./PATModal";

const { savePat } = vi.hoisted(() => ({ savePat: vi.fn() }));
vi.mock("../context/AppContext", () => ({ useApp: () => ({ pat: "", savePat }) }));

describe("PATModal accessibility", () => {
beforeEach(() => {
savePat.mockReset();
});

it("focuses the token input when its label is clicked", async () => {
const user = userEvent.setup();
render(<PATModal open={true} onClose={() => {}} />);

const label = screen.getByText(/personal access token/i, { selector: "label" });
await user.click(label);

expect(screen.getByPlaceholderText(/ghp_/)).toHaveFocus();
});

it("names the visibility button by its next action and updates on toggle", async () => {
const user = userEvent.setup();
render(<PATModal open={true} onClose={() => {}} />);

expect(screen.getByRole("button", { name: "Show token" })).toBeInTheDocument();
expect(screen.getByPlaceholderText(/ghp_/)).toHaveAttribute("type", "password");

await user.click(screen.getByRole("button", { name: "Show token" }));

expect(screen.getByRole("button", { name: "Hide token" })).toBeInTheDocument();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
expect(screen.getByPlaceholderText(/ghp_/)).toHaveAttribute("type", "text");
});
});
Loading