refactor: move @/ui/display/icon to twenty-ui (#4820)
Split from https://github.com/twentyhq/twenty/pull/4518 Part of https://github.com/twentyhq/twenty/issues/4766
This commit is contained in:
@ -0,0 +1,44 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import * as recoil from 'recoil';
|
||||
|
||||
import {
|
||||
Icon123,
|
||||
IconBuildingSkyscraper,
|
||||
IconUser,
|
||||
} from '@ui/display/icon/components/TablerIcons';
|
||||
import { useIcons } from '@ui/display/icon/hooks/useIcons';
|
||||
|
||||
describe('useIcons', () => {
|
||||
const mockedStateIcons = {
|
||||
IconUser,
|
||||
Icon123,
|
||||
IconBuildingSkyscraper,
|
||||
};
|
||||
jest
|
||||
.spyOn(recoil, 'useRecoilValue')
|
||||
.mockImplementationOnce(() => mockedStateIcons);
|
||||
const { result } = renderHook(() => useIcons(), {
|
||||
wrapper: recoil.RecoilRoot,
|
||||
});
|
||||
|
||||
it('returns default icon when no icon key is provided', () => {
|
||||
expect(result.current.getIcon()).toEqual(Icon123);
|
||||
});
|
||||
|
||||
it('returns the specified icon if the icon key exists in the iconsState', () => {
|
||||
expect(result.current.getIcon('Icon123')).toEqual(Icon123);
|
||||
expect(result.current.getIcon('IconUser')).toEqual(IconUser);
|
||||
expect(result.current.getIcon('IconBuildingSkyscraper')).toEqual(
|
||||
IconBuildingSkyscraper,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns default icon if the specified icon key does not exist in the iconsState', () => {
|
||||
expect(result.current.getIcon('nonExistentKey')).toEqual(Icon123);
|
||||
});
|
||||
|
||||
it('returns all icons in getIcons', () => {
|
||||
expect(result.current.getIcons()).toEqual(mockedStateIcons);
|
||||
expect(Object.keys(result.current.getIcons())).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
20
packages/twenty-ui/src/display/icon/hooks/useIcons.ts
Normal file
20
packages/twenty-ui/src/display/icon/hooks/useIcons.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { useRecoilValue } from 'recoil';
|
||||
|
||||
import { Icon123 } from '@ui/display/icon/components/TablerIcons';
|
||||
import { iconsState } from '@ui/display/icon/states/iconsState';
|
||||
|
||||
export const useIcons = () => {
|
||||
const icons = useRecoilValue(iconsState);
|
||||
const defaultIcon = Icon123;
|
||||
|
||||
const getIcons = () => {
|
||||
return icons;
|
||||
};
|
||||
|
||||
const getIcon = (iconKey?: string | null) => {
|
||||
if (!iconKey) return defaultIcon;
|
||||
return icons[iconKey] ?? defaultIcon;
|
||||
};
|
||||
|
||||
return { getIcons, getIcon };
|
||||
};
|
||||
Reference in New Issue
Block a user