diff --git a/packages/mui-joy/src/Avatar/Avatar.spec.tsx b/packages/mui-joy/src/Avatar/Avatar.spec.tsx new file mode 100644 index 00000000000000..1c4bbd60a261dd --- /dev/null +++ b/packages/mui-joy/src/Avatar/Avatar.spec.tsx @@ -0,0 +1,34 @@ +import Avatar from '@mui/joy/Avatar'; +import * as React from 'react'; + +; + +; + +// `variant` +; +; +; +; + +// `color` +; +; +; +; +; +; + +// `size` +; +; +; + +// @ts-expect-error there is no variant `filled` +; + +// @ts-expect-error there is no color `secondary` +; + +// @ts-expect-error there is no size `xl2` +; diff --git a/packages/mui-joy/src/Avatar/Avatar.test.js b/packages/mui-joy/src/Avatar/Avatar.test.js new file mode 100644 index 00000000000000..89694120f9c581 --- /dev/null +++ b/packages/mui-joy/src/Avatar/Avatar.test.js @@ -0,0 +1,266 @@ +import * as React from 'react'; +import { expect } from 'chai'; +import { createRenderer, describeConformance, fireEvent } from 'test/utils'; +import Avatar, { avatarClasses as classes } from '@mui/joy/Avatar'; +import { unstable_capitalize as capitalize } from '@mui/utils'; +import { spy } from 'sinon'; +import PersonIcon from '../internal/svg-icons/Person'; + +describe('', () => { + const { render } = createRenderer(); + + describeConformance(, () => ({ + classes, + inheritComponent: 'div', + render, + muiName: 'MuiAvatar', + refInstanceof: window.HTMLDivElement, + testComponentPropWith: 'span', + skip: [ + 'themeVariants', + 'classesRoot', + 'componentsProp', + 'themeDefaultProps', + 'themeStyleOverrides', + ], + })); + + describe('prop: variant', () => { + it('light by default', () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes.variantLight); + }); + + ['outlined', 'light', 'contained'].forEach((variant) => { + it(`should render ${variant}`, () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes[`variant${capitalize(variant)}`]); + }); + }); + }); + + describe('prop: color', () => { + it('adds a neutral class by default', () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes.colorNeutral); + }); + + ['primary', 'success', 'info', 'danger', 'neutral', 'warning'].forEach((color) => { + it(`should render ${color}`, () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes[`color${capitalize(color)}`]); + }); + }); + }); + + describe('prop: size', () => { + it('md by default', () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes.sizeMd); + }); + ['sm', 'md', 'lg'].forEach((size) => { + it(`should render ${size}`, () => { + const { getByTestId } = render(); + + expect(getByTestId('root')).to.have.class(classes[`size${capitalize(size)}`]); + }); + }); + }); + + describe('image avatar', () => { + it('should render a div containing an img', () => { + const { container } = render( + , + ); + const avatar = container.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'); + }); + + it('should be able to add more props to the image', () => { + const onError = spy(); + const { container } = render(); + const img = container.querySelector('img'); + fireEvent.error(img); + expect(onError.callCount).to.equal(1); + }); + }); + + describe('image avatar with unrendered children', () => { + it('should render a div containing an img, not children', () => { + const { container } = render(MB); + const avatar = container.firstChild; + const imgs = container.querySelectorAll('img'); + 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(); + const img = container.querySelector('img'); + fireEvent.error(img); + expect(onError.callCount).to.equal(1); + }); + }); + + describe('font icon avatar', () => { + it('should render a div containing an font icon', () => { + const { container } = render( + + + icon + + , + ); + const avatar = container.firstChild; + const icon = avatar.firstChild; + + expect(avatar).to.have.tagName('div'); + expect(icon).to.have.tagName('span'); + expect(icon).to.have.class('my-icon-font'); + expect(icon).to.have.text('icon'); + }); + + it('should merge user classes & spread custom props to the root node', () => { + const { container } = render( + + icon + , + ); + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.root); + expect(avatar).to.have.class('my-avatar'); + expect(avatar).to.have.attribute('data-my-prop', 'woofAvatar'); + }); + + it('should apply the colorNeutral class', () => { + const { container } = render( + + icon + , + ); + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.colorNeutral); + }); + }); + + describe('svg icon avatar', () => { + it('should render a div containing an svg icon', () => { + const container = render( + + + , + ).container; + const avatar = container.firstChild; + + expect(avatar).to.have.tagName('div'); + const personIcon = avatar.firstChild; + expect(personIcon).to.have.attribute('data-testid', 'PersonIcon'); + }); + + it('should merge user classes & spread custom props to the root node', () => { + const container = render( + + + , + ).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.root); + expect(avatar).to.have.class('my-avatar'); + expect(avatar).to.have.attribute('data-my-prop', 'woofAvatar'); + }); + + it('should apply the colorNeutral class', () => { + const container = render( + + + , + ).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.colorNeutral); + }); + }); + + describe('text avatar', () => { + it('should render a div containing a string', () => { + const container = render(OT).container; + const avatar = container.firstChild; + + expect(avatar).to.have.tagName('div'); + expect(avatar.firstChild).to.text('OT'); + }); + + it('should merge user classes & spread custom props to the root node', () => { + const container = render( + + OT + , + ).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.root); + expect(avatar).to.have.class('my-avatar'); + expect(avatar).to.have.attribute('data-my-prop', 'woofAvatar'); + }); + + it('should apply the colorNeutral class', () => { + const container = render(OT).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.colorNeutral); + }); + }); + + describe('falsey avatar', () => { + it('should render with defaultColor class when supplied with a child with falsey value', () => { + const container = render({0}).container; + const avatar = container.firstChild; + + expect(avatar).to.have.tagName('div'); + expect(avatar.firstChild).to.text('0'); + }); + + it('should merge user classes & spread custom props to the root node', () => { + const container = render( + + {0} + , + ).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.root); + expect(avatar).to.have.class('my-avatar'); + expect(avatar).to.have.attribute('data-my-prop', 'woofAvatar'); + }); + + it('should apply the colorNeutral class', () => { + const container = render({0}).container; + const avatar = container.firstChild; + + expect(avatar).to.have.class(classes.colorNeutral); + }); + }); +});