@@ -2,23 +2,10 @@ import { render, screen } from '@testing-library/react'
22import React from 'react'
33import Badges from 'components/Badges'
44
5- jest . mock ( '@fortawesome/fontawesome-svg-core' , ( ) => {
6- const { BADGE_CLASS_MAP } = jest . requireActual ( 'utils/data' )
7- const registered = new Set (
8- Object . values ( BADGE_CLASS_MAP ) . map ( ( s : string ) => s . split ( ' ' ) . pop ( ) ?. replace ( 'fa-' , '' ) )
9- )
10-
11- return {
12- findIconDefinition : jest . fn ( ( { iconName } : { iconName : string } ) => {
13- return registered . has ( iconName ) ? { iconName } : null
14- } ) ,
15- }
16- } )
17-
185jest . mock ( 'wrappers/FontAwesomeIconWrapper' , ( ) => {
196 const RealWrapper = jest . requireActual ( 'wrappers/FontAwesomeIconWrapper' ) . default
207
21- const getName = ( icon : any ) => {
8+ const getName = ( icon ) => {
229 if ( ! icon ) return 'medal'
2310 if ( typeof icon === 'string' ) {
2411 const m = icon . match ( / f a - ( [ a - z 0 - 9 - ] + ) $ / i)
@@ -31,7 +18,7 @@ jest.mock('wrappers/FontAwesomeIconWrapper', () => {
3118 return 'medal'
3219 }
3320
34- return function MockFontAwesomeIconWrapper ( props : any ) {
21+ return function MockFontAwesomeIconWrapper ( props ) {
3522 const name = getName ( props . icon )
3623 return (
3724 < div data-testid = { `icon-${ name } ` } >
@@ -110,17 +97,25 @@ describe('Badges Component', () => {
11097 { cssClass : 'ribbon' , expectedIcon : 'ribbon' } ,
11198 { cssClass : 'star' , expectedIcon : 'star' } ,
11299 { cssClass : 'certificate' , expectedIcon : 'certificate' } ,
113- { cssClass : 'bug-slash ' , expectedIcon : 'bug' } ,
100+ { cssClass : 'bug_slash ' , expectedIcon : 'bug' } , // Backend snake_case input
114101 ]
115102
116103 backendIcons . forEach ( ( { cssClass, expectedIcon } ) => {
117- it ( `renders ${ cssClass } icon correctly` , ( ) => {
104+ it ( `renders ${ cssClass } icon correctly (transforms snake_case to camelCase) ` , ( ) => {
118105 render ( < Badges name = { `${ cssClass } Badge` } cssClass = { cssClass } /> )
119106
120107 const icon = screen . getByTestId ( 'badge-icon' )
121108 expect ( icon ) . toBeInTheDocument ( )
122109 expect ( icon ) . toHaveAttribute ( 'data-icon' , expectedIcon )
123110 } )
124111 } )
112+
113+ it ( 'handles camelCase input directly' , ( ) => {
114+ render ( < Badges name = "Bug Slash Badge" cssClass = "bugSlash" /> )
115+
116+ const icon = screen . getByTestId ( 'badge-icon' )
117+ expect ( icon ) . toBeInTheDocument ( )
118+ expect ( icon ) . toHaveAttribute ( 'data-icon' , 'bug' )
119+ } )
125120 } )
126121} )
0 commit comments