Skip to content

Commit

Permalink
feat: avatar component improvements and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dias999 committed Nov 28, 2023
1 parent 3dd50eb commit 63dae30
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 45 deletions.
29 changes: 10 additions & 19 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"moduleFileExtensions": ["js", "json", "ts", "tsx"],
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.jest.json"
}
},
"testRegex": ".*\\.spec\\.ts$",
"testPathIgnorePatterns": [
"/node_modules/",
"/dist/"
],
"testRegex": [".*\\.spec\\.ts$", ".*\\.spec\\.tsx$"],
"testPathIgnorePatterns": ["/node_modules/", "/dist/"],
"transform": {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": "ts-jest",
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
},
"collectCoverageFrom": [
"packages/**/*.ts",
Expand All @@ -28,11 +23,7 @@
"!**/__fixture__/**"
],
"coverageDirectory": "coverage",
"coverageReporters": [
"html",
"text",
"text-summary",
"cobertura"
],
"testEnvironment": "node"
}
"coverageReporters": ["html", "text", "text-summary", "cobertura"],
"testEnvironment": "node",
"preset": "ts-jest"
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
]
},
"devDependencies": {
"@commitlint/cli": "^16.1.0",
"@commitlint/config-conventional": "^16.0.0",
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/react": "^14.1.0",
"@types/jest": "^26.0.24",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"@commitlint/cli": "^16.1.0",
"@commitlint/config-conventional": "^16.0.0",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.24.2",
Expand Down
39 changes: 39 additions & 0 deletions packages/react-material-ui/__tests__/Avatar.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @jest-environment jsdom
*/

import '@testing-library/jest-dom';
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Avatar from '../src/components/Avatar/Avatar';

describe('Avatar Component', () => {
const props = {
src: '',
alt: 'Test Alt Text',
size: 50,
onClick: jest.fn(),
};

it('should render correctly', () => {
const { getByAltText } = render(<Avatar {...props} />);
const avatarImage = getByAltText(props.alt);

expect(avatarImage).toBeInTheDocument();
expect(avatarImage).toHaveAttribute('src', props.src);
});

it('calls the onClick function when clicked', () => {
const { getByAltText } = render(<Avatar {...props} />);
const avatarImage = getByAltText(props.alt);
fireEvent.click(avatarImage);
expect(props.onClick).toHaveBeenCalled();
});

it('renders the avatar image with the correct size', () => {
const { getByAltText } = render(<Avatar {...props} />);
const avatarImage = getByAltText(props.alt);
expect(avatarImage).toHaveStyle(`width: ${props.size}px`);
expect(avatarImage).toHaveStyle(`height: ${props.size}px`);
});
});
19 changes: 14 additions & 5 deletions packages/react-material-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"dist"
],
"peerDependencies": {
"@concepta/react-data-provider": "^1.0.0-alpha.5",
"@types/react": "^18.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"@concepta/react-data-provider": "^1.0.0-alpha.5"
},
"peerDependenciesMeta": {
"@types/react": {
Expand All @@ -34,13 +31,25 @@
"@rjsf/mui": "^5.0.0-beta.13",
"@rjsf/utils": "^5.0.0-beta.13",
"@rjsf/validator-ajv6": "^5.0.0-beta.13",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"json-schema": "0.4.0",
"lodash": "^4.17.21",
"next": "^13.4.19"
"next": "^13.4.19",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/lodash": "^4.14.198",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.(ts|tsx|js|jsx)$": "ts-jest"
}
}
}
37 changes: 31 additions & 6 deletions packages/react-material-ui/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import React, { FC } from 'react';
import React, { FC, useState } from 'react';
import Box from '@mui/material/Box';
import { Image } from './Styles';
import Text from '../Text';

type Props = {
src: string;
alt: string;
size: number;
initials?: string;
onClick?: () => void;
};

const Avatar: FC<Props> = (props) => {
const { src, alt, size, onClick } = props;
export const Avatar: FC<Props> = (props) => {
const { src, alt, size, initials, onClick } = props;
const [failed, setFailed] = useState(false);

return <Image src={src} alt={alt} size={size} onClick={onClick} />;
};
const handleImageError = () => {
setFailed(true);
};

if (failed && initials) {
return (
<Box sx={{ backgroundColor: 'grey.500' }}>
<Text fontSize={30} fontWeight={800} mt={1} gutterBottom>
{initials}
</Text>
</Box>
);
}

export default Avatar;
return (
<Image
src={src}
alt={alt}
size={size}
onClick={onClick}
onError={handleImageError}
style={{ display: failed ? 'none' : 'block' }}
/>
);
};
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"jsx": "react",
"typeRoots": ["./node_modules/@types"]
"typeRoots": ["./node_modules/@types"],
"baseUrl": ".",
"paths": {
"*": ["./packages/react-material-ui/src/*"]
}
},
"files": [],
"references": [
Expand Down
Loading

0 comments on commit 63dae30

Please sign in to comment.