最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shadcnui - How do I get a Shadcn button's variant for testing? - Stack Overflow

programmeradmin4浏览0评论

For testing purposes, I want to check if my <Button /> component is of the "disabled" variant, however I'm not sure how to check for that.

it('renders disabled button', () => {
  render(<Button variant="disabled" />);

  const button = screen.getByRole("button");
  
  // How would I get the variant of button?
})

In general, how can I get the variant of a Shadcn Button component for my tests?

For testing purposes, I want to check if my <Button /> component is of the "disabled" variant, however I'm not sure how to check for that.

it('renders disabled button', () => {
  render(<Button variant="disabled" />);

  const button = screen.getByRole("button");
  
  // How would I get the variant of button?
})

In general, how can I get the variant of a Shadcn Button component for my tests?

Share Improve this question edited 4 hours ago Jasperan asked 4 hours ago JasperanJasperan 3,7066 gold badges27 silver badges60 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the toHaveClass() method and grab the associated classes from the buttonVariants variable exported by the Button component.

In your code you can do:

import { Button, buttonVariants } from "@/components/ui/button";

it('renders disabled button', () => {
  render(<Button variant="disabled" />);

  const button = screen.getByRole("button");

  const expectedClass = buttonVariants({ variant: "disabled" })
  expect(button).toHaveClass(expectedClass)
})
发布评论

评论列表(0)

  1. 暂无评论