Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(web-react): Warn when icon asset is missing from the map #1421

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions packages/web-react/src/hooks/__tests__/useIcon.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof warning>;
const icons = { warning: '<path d="ERRW ADSFDSFDS"></path>' };
const wrapper = ({ children }: { children: ReactNode }) => <IconsProvider value={icons}>{children}</IconsProvider>;

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 }) => <IconsProvider value={{}}>{children}</IconsProvider>,
});

expect(mockedWarning).toHaveBeenCalled();
});

it('should return icon path', () => {
const icons = { warning: '<path d="ERRW ADSFDSFDS"></path>' };
const wrapper = ({ children }: { children: ReactNode }) => <IconsProvider value={icons}>{children}</IconsProvider>;
const { result } = renderHook(() => useIcon('warning'), { wrapper });

expect(result.current).toBe('<path d="ERRW ADSFDSFDS"></path>');
});

it('should return icon path based on fallback icon', () => {
const { result } = renderHook(() => useIcon('danger'), { wrapper });

expect(result.current).toBe('<path d="ERRW ADSFDSFDS"></path>');
});

it('should raise warning when fallback icon name is used', () => {
renderHook(() => useIcon('danger'), { wrapper });

expect(mockedWarning).toHaveBeenCalled();
});
});
17 changes: 12 additions & 5 deletions packages/web-react/src/hooks/useIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
};
Loading