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

[AutocompleteOption][Avatar][joy] js test replaced with ts test #37088

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fireEvent,
} from 'test/utils';
import { ThemeProvider } from '@mui/joy/styles';
import Avatar, { avatarClasses as classes } from '@mui/joy/Avatar';
import Avatar, { AvatarClassKey, avatarClasses as classes } from '@mui/joy/Avatar';
import { unstable_capitalize as capitalize } from '@mui/utils';
import PersonIcon from '../internal/svg-icons/Person';

Expand Down Expand Up @@ -42,11 +42,13 @@ describe('<Avatar />', () => {
expect(getByTestId('root')).to.have.class(classes.variantSoft);
});

['outlined', 'soft', 'solid'].forEach((variant) => {
(['outlined', 'soft', 'solid'] as const).forEach((variant) => {
it(`should render ${variant}`, () => {
const { getByTestId } = render(<Avatar data-testid="root" variant={variant} />);

expect(getByTestId('root')).to.have.class(classes[`variant${capitalize(variant)}`]);
expect(getByTestId('root')).to.have.class(
classes[`variant${capitalize(variant)}` as AvatarClassKey],
);
});
});
});
Expand All @@ -58,11 +60,13 @@ describe('<Avatar />', () => {
expect(getByTestId('root')).to.have.class(classes.colorNeutral);
});

['primary', 'success', 'info', 'danger', 'neutral', 'warning'].forEach((color) => {
(['primary', 'success', 'info', 'danger', 'neutral', 'warning'] as const).forEach((color) => {
it(`should render ${color}`, () => {
const { getByTestId } = render(<Avatar data-testid="root" color={color} />);

expect(getByTestId('root')).to.have.class(classes[`color${capitalize(color)}`]);
expect(getByTestId('root')).to.have.class(
classes[`color${capitalize(color)}` as AvatarClassKey],
);
});
});
});
Expand All @@ -73,11 +77,13 @@ describe('<Avatar />', () => {

expect(getByTestId('root')).to.have.class(classes.sizeMd);
});
['sm', 'md', 'lg'].forEach((size) => {
(['sm', 'md', 'lg'] as const).forEach((size) => {
it(`should render ${size}`, () => {
const { getByTestId } = render(<Avatar data-testid="root" size={size} />);

expect(getByTestId('root')).to.have.class(classes[`size${capitalize(size)}`]);
expect(getByTestId('root')).to.have.class(
classes[`size${capitalize(size)}` as AvatarClassKey],
);
});
});
});
Expand All @@ -93,13 +99,12 @@ describe('<Avatar />', () => {
/>,
);
const avatar = container.firstChild;
const img = avatar.firstChild;
const img = avatar?.firstChild;
expect(avatar).to.have.tagName('div');
expect(img).to.have.tagName('img');
expect(avatar).to.have.class(classes.root);
expect(avatar).to.have.class('my-avatar');
expect(avatar).to.have.attribute('data-my-prop', 'woofAvatar');
expect(avatar).not.to.have.class(classes.colorDefault);
expect(img).to.have.class(classes.img);
expect(img).to.have.attribute('alt', 'Hello World!');
expect(img).to.have.attribute('src', '/fake.png');
Expand All @@ -109,7 +114,7 @@ describe('<Avatar />', () => {
const onError = spy();
const { container } = render(<Avatar src="/fake.png" slotProps={{ img: { onError } }} />);
const img = container.querySelector('img');
fireEvent.error(img);
fireEvent.error(img as HTMLImageElement);
expect(onError.callCount).to.equal(1);
});
});
Expand All @@ -122,6 +127,14 @@ describe('<Avatar />', () => {
expect(imgs.length).to.equal(1);
expect(avatar).to.have.text('');
});

it('should be able to add more props to the image', () => {
const onError = spy();
const { container } = render(<Avatar src="/fake.png" slotProps={{ img: { onError } }} />);
const img = container.querySelector('img');
fireEvent.error(img as HTMLImageElement);
expect(onError.callCount).to.equal(1);
});
});

describe('font icon avatar', () => {
Expand All @@ -134,7 +147,7 @@ describe('<Avatar />', () => {
</Avatar>,
);
const avatar = container.firstChild;
const icon = avatar.firstChild;
const icon = avatar?.firstChild;

expect(avatar).to.have.tagName('div');
expect(icon).to.have.tagName('span');
Expand Down Expand Up @@ -177,7 +190,7 @@ describe('<Avatar />', () => {
const avatar = container.firstChild;

expect(avatar).to.have.tagName('div');
const personIcon = avatar.firstChild;
const personIcon = (avatar as ChildNode).firstChild;
expect(personIcon).to.have.attribute('data-testid', 'PersonIcon');
});

Expand Down Expand Up @@ -212,7 +225,7 @@ describe('<Avatar />', () => {
const avatar = container.firstChild;

expect(avatar).to.have.tagName('div');
expect(avatar.firstChild).to.text('OT');
expect((avatar as ChildNode).firstChild).to.text('OT');
});

it('should merge user classes & spread custom props to the root node', () => {
Expand Down Expand Up @@ -242,7 +255,7 @@ describe('<Avatar />', () => {
const avatar = container.firstChild;

expect(avatar).to.have.tagName('div');
expect(avatar.firstChild).to.text('0');
expect((avatar as ChildNode).firstChild).to.text('0');
});

it('should merge user classes & spread custom props to the root node', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/mui-joy/src/Avatar/avatarClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function getAvatarUtilityClass(slot: string): string {

const avatarClasses: AvatarClasses = generateUtilityClasses('MuiAvatar', [
'root',
'colorDefault',
PunitSoniME marked this conversation as resolved.
Show resolved Hide resolved
'colorPrimary',
'colorNeutral',
'colorDanger',
Expand Down