diff --git a/packages/web-react/src/hooks/__tests__/useIcon.test.tsx b/packages/web-react/src/hooks/__tests__/useIcon.test.tsx index 85359b6fe1..4db34a86fb 100644 --- a/packages/web-react/src/hooks/__tests__/useIcon.test.tsx +++ b/packages/web-react/src/hooks/__tests__/useIcon.test.tsx @@ -1,20 +1,49 @@ -import React, { ReactNode } from 'react'; import { renderHook } from '@testing-library/react-hooks'; -import { useIcon } from '../useIcon'; +import React, { ReactNode } from 'react'; +import warning from '../../common/utilities/warning'; import { IconsProvider } from '../../context/IconsContext'; +import { useIcon } from '../useIcon'; + +jest.mock('../../common/utilities/warning', () => jest.fn()); describe('useIcon', () => { + const mockedWarning = warning as jest.MockedFunction; + const icons = { warning: '' }; + const wrapper = ({ children }: { children: ReactNode }) => {children}; + + beforeEach(() => { + jest.clearAllMocks(); + }); + it('should return empty string', () => { const { result } = renderHook(() => useIcon('')); expect(result.current).toBe(''); }); + it('should raise warning when icon name is missing from the assets', () => { + renderHook(() => useIcon('warning'), { + wrapper: ({ children }: { children: ReactNode }) => {children}, + }); + + expect(mockedWarning).toHaveBeenCalled(); + }); + it('should return icon path', () => { - const icons = { warning: '' }; - const wrapper = ({ children }: { children: ReactNode }) => {children}; const { result } = renderHook(() => useIcon('warning'), { wrapper }); expect(result.current).toBe(''); }); + + it('should return icon path based on fallback icon', () => { + const { result } = renderHook(() => useIcon('danger'), { wrapper }); + + expect(result.current).toBe(''); + }); + + it('should raise warning when fallback icon name is used', () => { + renderHook(() => useIcon('danger'), { wrapper }); + + expect(mockedWarning).toHaveBeenCalled(); + }); }); diff --git a/packages/web-react/src/hooks/useIcon.ts b/packages/web-react/src/hooks/useIcon.ts index 951b9504c9..cbf6c6b4f0 100644 --- a/packages/web-react/src/hooks/useIcon.ts +++ b/packages/web-react/src/hooks/useIcon.ts @@ -21,13 +21,20 @@ export const useIcon = (name: string) => { } if (icons != null && icons[name] == null) { - warning( - name !== 'danger' || iconFallbacks[name] !== 'warning', - 'The "danger" icon is missing from your assets. It will be required in the next major version. Using "warning" as a fallback.', - ); + if (name === 'danger' && iconFallbacks[name] === 'warning') { + warning( + false, + 'The "danger" icon is missing from your assets. It will be required in the next major version. Using "warning" as a fallback.', + ); - return icons[iconFallbacks[name]]; + return icons[iconFallbacks[name]]; + } } + warning( + false, + `The ${name} icon is missing from your assets or icon map provided by the IconsProvider. Please make sure you have provided all icons needed by used components.`, + ); + return ''; };