-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "feat: Integrated Dicebear Library To Enhance Security (…
- Loading branch information
1 parent
ebcc977
commit c0c2cc7
Showing
14 changed files
with
1,291 additions
and
580 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const initials = jest.fn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const createAvatar = jest.fn(() => { | ||
return { | ||
toDataUriSync: jest.fn(() => 'mocked-data-uri'), | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import Avatar from './Avatar'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { I18nextProvider } from 'react-i18next'; | ||
import i18nForTest from 'utils/i18nForTest'; | ||
|
||
describe('Avatar component', () => { | ||
test('renders with name and alt attribute', () => { | ||
const testName = 'John Doe'; | ||
const testAlt = 'Test Alt Text'; | ||
const testSize = 64; | ||
|
||
const { getByAltText } = render( | ||
<BrowserRouter> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Avatar name={testName} alt={testAlt} size={testSize} /> | ||
</I18nextProvider> | ||
</BrowserRouter>, | ||
); | ||
const avatarElement = getByAltText(testAlt); | ||
|
||
expect(avatarElement).toBeInTheDocument(); | ||
expect(avatarElement.getAttribute('src')).toBeDefined(); | ||
}); | ||
|
||
test('renders with custom style and data-testid', () => { | ||
const testName = 'Jane Doe'; | ||
const testStyle = 'custom-avatar-style'; | ||
const testDataTestId = 'custom-avatar-test-id'; | ||
|
||
const { getByAltText } = render( | ||
<BrowserRouter> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Avatar | ||
name={testName} | ||
avatarStyle={testStyle} | ||
dataTestId={testDataTestId} | ||
/> | ||
</I18nextProvider> | ||
</BrowserRouter>, | ||
); | ||
const avatarElement = getByAltText('Dummy Avatar'); | ||
|
||
expect(avatarElement).toBeInTheDocument(); | ||
expect(avatarElement.getAttribute('src')).toBeDefined(); | ||
expect(avatarElement.getAttribute('class')).toContain(testStyle); | ||
expect(avatarElement.getAttribute('data-testid')).toBe(testDataTestId); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { useMemo } from 'react'; | ||
import { createAvatar } from '@dicebear/core'; | ||
import { initials } from '@dicebear/collection'; | ||
|
||
interface InterfaceAvatarProps { | ||
name: string; | ||
alt?: string; | ||
size?: number; | ||
avatarStyle?: string; | ||
dataTestId?: string; | ||
} | ||
|
||
const Avatar = ({ | ||
name, | ||
alt = 'Dummy Avatar', | ||
size, | ||
avatarStyle, | ||
dataTestId, | ||
}: InterfaceAvatarProps): JSX.Element => { | ||
const avatar = useMemo(() => { | ||
return createAvatar(initials, { | ||
size: size || 128, | ||
seed: name, | ||
}).toDataUriSync(); | ||
}, [name, size]); | ||
|
||
const svg = avatar.toString(); | ||
|
||
return ( | ||
<img | ||
src={svg} | ||
alt={alt} | ||
className={avatarStyle ? avatarStyle : ''} | ||
data-testid={dataTestId ? dataTestId : ''} | ||
/> | ||
); | ||
}; | ||
|
||
export default Avatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters