1+ import React from 'react'
12import { vi } from 'vitest'
2- import { render , fireEvent } from '../../../../tests/utils'
3- import { EnumSize } from '../../../../types/enum'
4- import { Button , ButtonMenu } from '../Button'
5- import { Menu } from '@headlessui/react'
3+ import { render , fireEvent , renderHook } from '../../../../tests/utils'
4+ import { EnumSize , EnumVariant } from '../../../../types/enum'
5+ import {
6+ Button ,
7+ makeButton ,
8+ VARIANT ,
9+ SHAPE ,
10+ SIZE ,
11+ EnumButtonShape ,
12+ } from '../Button'
613
714describe ( 'Button' , ( ) => {
815 test ( 'renders with default variant, shape, and size' , ( ) => {
916 const { getByText } = render ( < Button > Click me</ Button > )
1017 const button = getByText ( 'Click me' )
1118
12- expect ( button ) . toHaveClass ( 'bg-secondary-500' )
13- expect ( button ) . toHaveClass ( 'rounded-md' )
14- expect ( button ) . toHaveClass ( 'px-3 py-2 text-base' )
19+ expect ( button ) . toHaveClass ( VARIANT . get ( EnumVariant . Primary ) as string )
20+ expect ( button ) . toHaveClass ( SHAPE . get ( EnumButtonShape . Rounded ) as string )
21+ expect ( button ) . toHaveClass ( SIZE . get ( EnumSize . md ) as string )
1522 } )
1623
1724 test ( 'renders with custom variant, shape, and size' , ( ) => {
1825 const { getByText } = render (
1926 < Button
20- variant = "primary"
21- shape = "square"
27+ variant = { EnumVariant . Secondary }
28+ shape = { EnumButtonShape . Square }
2229 size = { EnumSize . sm }
2330 >
2431 Click me
2532 </ Button > ,
2633 )
2734 const button = getByText ( 'Click me' )
2835
29- expect ( button ) . toHaveClass ( 'bg-secondary-100' )
30- expect ( button ) . not . toHaveClass ( 'rounded-md' )
31- expect ( button ) . toHaveClass ( 'px-2 py-1 text-xs' )
36+ expect ( button ) . toHaveClass ( VARIANT . get ( EnumVariant . Secondary ) as string )
37+ expect ( button ) . toHaveClass ( SHAPE . get ( EnumButtonShape . Square ) as string )
38+ expect ( button ) . toHaveClass ( SIZE . get ( EnumSize . sm ) as string )
3239 } )
3340
3441 test ( 'calls onClick when clicked' , ( ) => {
@@ -43,34 +50,69 @@ describe('Button', () => {
4350
4451describe ( 'ButtonMenu' , ( ) => {
4552 test ( 'renders with default variant, shape, and size' , ( ) => {
53+ const ButtonMenu = createButton ( )
54+ const { getByText } = render ( < ButtonMenu > Click me</ ButtonMenu > )
55+ const button = getByText ( 'Click me' )
56+
57+ expect ( button ) . toHaveClass ( VARIANT . get ( EnumVariant . Primary ) as string )
58+ expect ( button ) . toHaveClass ( SHAPE . get ( EnumButtonShape . Rounded ) as string )
59+ expect ( button ) . toHaveClass ( SIZE . get ( EnumSize . md ) as string )
60+ } )
61+
62+ test ( 'renders with custom variant, shape, and size' , ( ) => {
63+ const ButtonMenu = createButton ( )
4664 const { getByText } = render (
47- < Menu >
48- < ButtonMenu > Click me</ ButtonMenu >
49- </ Menu > ,
65+ < ButtonMenu
66+ variant = { EnumVariant . Secondary }
67+ shape = { EnumButtonShape . Square }
68+ size = { EnumSize . sm }
69+ >
70+ Click me
71+ </ ButtonMenu > ,
5072 )
5173 const button = getByText ( 'Click me' )
5274
53- expect ( button ) . toHaveClass ( 'bg-secondary-500' )
54- expect ( button ) . toHaveClass ( 'rounded-md' )
55- expect ( button ) . toHaveClass ( 'px-3 py-2 text-base' )
75+ expect ( button ) . toHaveClass ( VARIANT . get ( EnumVariant . Secondary ) as string )
76+ expect ( button ) . toHaveClass ( SHAPE . get ( EnumButtonShape . Square ) as string )
77+ expect ( button ) . toHaveClass ( SIZE . get ( EnumSize . sm ) as string )
5678 } )
5779
58- test ( 'renders with custom variant, shape, and size' , ( ) => {
80+ test ( 'calls onClick when clicked' , ( ) => {
81+ const ButtonMenu = createButton ( )
82+ const onClick = vi . fn ( )
5983 const { getByText } = render (
60- < Menu >
61- < ButtonMenu
62- variant = "primary"
63- shape = "square"
64- size = { EnumSize . sm }
65- >
66- Click me
67- </ ButtonMenu >
68- </ Menu > ,
84+ < ButtonMenu onClick = { onClick } > Click me</ ButtonMenu > ,
6985 )
7086 const button = getByText ( 'Click me' )
7187
72- expect ( button ) . toHaveClass ( 'bg-secondary-100' )
73- expect ( button ) . not . toHaveClass ( 'rounded-md' )
74- expect ( button ) . toHaveClass ( 'px-2 py-1 text-xs' )
88+ fireEvent . click ( button )
89+ expect ( onClick ) . toHaveBeenCalled ( )
90+ } )
91+
92+ test ( 'get ref from component' , ( ) => {
93+ const ButtonMenu = createButton ( )
94+ const { result } = renderHook ( ( ) => React . useRef < HTMLDivElement > ( null ) )
95+ const { getByText } = render (
96+ < ButtonMenu ref = { result . current } > Click me</ ButtonMenu > ,
97+ )
98+ const button = getByText ( 'Click me' )
99+
100+ expect ( result . current . current ) . toBe ( button )
75101 } )
76102} )
103+
104+ function createButton ( ) : any {
105+ return makeButton (
106+ React . forwardRef < any , any > ( function Button ( { children, ...props } , ref ) {
107+ return (
108+ < div
109+ { ...props }
110+ role = "button"
111+ ref = { ref }
112+ >
113+ { children }
114+ </ div >
115+ )
116+ } ) ,
117+ )
118+ }
0 commit comments