@@ -8,35 +8,75 @@ const MockIcon = (props: React.SVGProps<SVGSVGElement>) => (
8
8
) ;
9
9
10
10
describe ( 'Button' , ( ) => {
11
- it ( 'renders children' , ( ) => {
12
- render ( < Button > Click Me</ Button > ) ;
13
- expect ( screen . getByText ( 'Click Me' ) ) . toBeInTheDocument ( ) ;
14
- } ) ;
15
-
16
- it ( 'calls onClick handler when clicked' , ( ) => {
17
- const handleClick = jest . fn ( ) ;
18
- render ( < Button onClick = { handleClick } > Click</ Button > ) ;
19
- fireEvent . click ( screen . getByText ( 'Click' ) ) ;
20
- expect ( handleClick ) . toHaveBeenCalledTimes ( 1 ) ;
21
- } ) ;
22
-
11
+ // Tag
23
12
it ( 'renders as an anchor when href is provided' , ( ) => {
24
13
render ( < Button href = "https://example.com" > Link</ Button > ) ;
25
14
const anchor = screen . getByRole ( 'link' ) ;
15
+ expect ( anchor . tagName . toLowerCase ( ) ) . toBe ( 'a' ) ;
26
16
expect ( anchor ) . toHaveAttribute ( 'href' , 'https://example.com' ) ;
27
17
} ) ;
28
18
29
- it ( 'renders as a React Router link when `to` is provided' , ( ) => {
19
+ it ( 'renders as a React Router <Link> when `to` is provided' , ( ) => {
30
20
render ( < Button to = "/dashboard" > Go</ Button > ) ;
31
21
const link = screen . getByRole ( 'link' ) ;
22
+ expect ( link . tagName . toLowerCase ( ) ) . toBe ( 'a' ) ; // Link renders as <a>
32
23
expect ( link ) . toHaveAttribute ( 'href' , '/dashboard' ) ;
33
24
} ) ;
34
25
35
- it ( 'renders with iconBefore' , ( ) => {
26
+ it ( 'renders as a <button> by default' , ( ) => {
27
+ render ( < Button > Click Me</ Button > ) ;
28
+ const el = screen . getByRole ( 'button' ) ;
29
+ expect ( el . tagName . toLowerCase ( ) ) . toBe ( 'button' ) ;
30
+ } ) ;
31
+
32
+ // Children & Icons
33
+ it ( 'renders children' , ( ) => {
34
+ render ( < Button > Click Me</ Button > ) ;
35
+ expect ( screen . getByText ( 'Click Me' ) ) . toBeInTheDocument ( ) ;
36
+ } ) ;
37
+
38
+ it ( 'renders an iconBefore and button text' , ( ) => {
39
+ render (
40
+ < Button iconBefore = { < MockIcon aria-label = "iconbefore" /> } >
41
+ This has a before icon
42
+ </ Button >
43
+ ) ;
44
+ expect ( screen . getByLabelText ( 'iconbefore' ) ) . toBeInTheDocument ( ) ;
45
+ expect ( screen . getByRole ( 'button' ) ) . toHaveTextContent (
46
+ 'This has a before icon'
47
+ ) ;
48
+ } ) ;
49
+
50
+ it ( 'renders with iconAfter' , ( ) => {
36
51
render (
37
- < Button iconBefore = { < MockIcon aria-label = "github" /> } > Create</ Button >
52
+ < Button iconAfter = { < MockIcon aria-label = "iconafter" /> } >
53
+ This has an after icon
54
+ </ Button >
55
+ ) ;
56
+ expect ( screen . getByLabelText ( 'iconafter' ) ) . toBeInTheDocument ( ) ;
57
+ expect ( screen . getByRole ( 'button' ) ) . toHaveTextContent (
58
+ 'This has an after icon'
38
59
) ;
39
- expect ( screen . getByLabelText ( 'github' ) ) . toBeInTheDocument ( ) ;
60
+ } ) ;
61
+
62
+ it ( 'renders only the icon if iconOnly' , ( ) => {
63
+ render (
64
+ < Button iconAfter = { < MockIcon aria-label = "iconafter" /> } iconOnly >
65
+ This has an after icon
66
+ </ Button >
67
+ ) ;
68
+ expect ( screen . getByLabelText ( 'iconafter' ) ) . toBeInTheDocument ( ) ;
69
+ expect ( screen . getByRole ( 'button' ) ) . not . toHaveTextContent (
70
+ 'This has an after icon'
71
+ ) ;
72
+ } ) ;
73
+
74
+ // HTML attributes
75
+ it ( 'calls onClick handler when clicked' , ( ) => {
76
+ const handleClick = jest . fn ( ) ;
77
+ render ( < Button onClick = { handleClick } > Click</ Button > ) ;
78
+ fireEvent . click ( screen . getByText ( 'Click' ) ) ;
79
+ expect ( handleClick ) . toHaveBeenCalledTimes ( 1 ) ;
40
80
} ) ;
41
81
42
82
it ( 'renders disabled state' , ( ) => {
0 commit comments