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
28 changes: 28 additions & 0 deletions src/components/spinner/Spinner.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { render, screen } from "@testing-library/react";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: 🟠 Major
Update tests to actually verify the component’s default size behavior and remove the unused userEvent setup.

Suggested Change:

import { render, screen } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { Spinner, type SpinnerProps } from "./Spinner";

const renderSpinner = (props: Partial<SpinnerProps> = {}) => {
  render(<Spinner {...props} />);
  return screen.getByRole("status", { name: /loading/i });
};

describe("Spinner", () => {
  it("should render medium size spinner by default", () => {
    const spinner = renderSpinner(); // no size prop
    expect(spinner).toHaveClass("w-8", "h-8");
  });

  it("should render small size spinner when size is sm", () => {
    const spinner = renderSpinner({ size: "sm" });
    expect(spinner).toHaveClass("w-4", "h-4");
  });

  it("should render large size spinner when size is lg", () => {
    const spinner = renderSpinner({ size: "lg" });
    expect(spinner).toHaveClass("w-12", "h-12");
  });
});

import userEvent from "@testing-library/user-event";
import { describe, it, expect } from "vitest";
import { Spinner, type SpinnerProps } from "./Spinner";

const defaultProps: SpinnerProps = {
size: "md",
className: "",
};

const renderSpinner = (additionalProps?: Partial<SpinnerProps>) => {
const user = userEvent.setup();

render(<Spinner {...defaultProps} {...additionalProps} />);

return {
user,
spinner: screen.getByRole("status", { name: /loading/i }),
};
};

describe("Spinner", () => {
it("should render a loading indicator with accessible role", () => {
const { spinner } = renderSpinner();

expect(spinner).toBeInTheDocument();
});
});
19 changes: 19 additions & 0 deletions src/components/spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface SpinnerProps {
size?: "sm" | "md" | "lg";
className?: string;
}

export const Spinner = ({ size = "md", className = "" }: SpinnerProps) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: 🟠 Major
Make the spinner more reusable/accessibility-friendly by fixing the border-3 class, forwarding standard DOM props, and allowing the accessible label to be customized.

Suggested Change:

import type { HTMLAttributes } from "react";

const SIZE_STYLES = {
  sm: "w-4 h-4 border-2",
  md: "w-8 h-8 border-[3px]", // or a Tailwind-supported border width
  lg: "w-12 h-12 border-4",
} as const;

export type SpinnerProps = {
  size?: keyof typeof SIZE_STYLES;
  ariaLabel?: string;
  className?: string;
} & Omit<HTMLAttributes<HTMLSpanElement>, "aria-label">;

export const Spinner = ({
  size = "md",
  ariaLabel = "Loading",
  className = "",
  ...rest
}: SpinnerProps) => {
  const sizeClass = SIZE_STYLES[size] ?? SIZE_STYLES.md;

  return (
    <span
      role="status"
      aria-live="polite"
      aria-label={ariaLabel}
      className={`inline-block ${sizeClass} border-gray-200 border-t-blue-600 rounded-full animate-spin ${className}`.trim()}
      {...rest}
    />
  );
};

const sizeStyles = {
sm: "w-4 h-4 border-2",
md: "w-8 h-8 border-3",
lg: "w-12 h-12 border-4",
} as const;

return (
<output
className={`inline-block ${sizeStyles[size]} border-gray-200 border-t-blue-600 rounded-full animate-spin ${className}`}
aria-label="Loading"
/>
);
};