Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Mar 15, 2020
1 parent 0546c18 commit 69f9942
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/pages/api-docs/avatar-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ You can learn more about the difference by [reading this guide](/guides/minimizi
|:-----|:-----|:--------|:------------|
| <span class="prop-name">children</span> | <span class="prop-type">node</span> | | The avatars to stack. |
| <span class="prop-name">classes</span> | <span class="prop-type">object</span> | | Override or extend the styles applied to the component. See [CSS API](#css) below for more details. |
| <span class="prop-name">spacing</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'<br>&#124;&nbsp;number</span> | <span class="prop-default">'medium'</span> | The spacing between avatars. A group with `small` spacing value has a larger overlap. |
| <span class="prop-name">max</span> | <span class="prop-type">number</span> | <span class="prop-default">5</span> | The maximum number of avatars to display. An additional text avatar will display the number of hidden avatars, if any. |
| <span class="prop-name">max</span> | <span class="prop-type">number</span> | <span class="prop-default">5</span> | Max avatars to show before +x. |
| <span class="prop-name">spacing</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'<br>&#124;&nbsp;number</span> | <span class="prop-default">'medium'</span> | Spacing between avatars. |

The `ref` is forwarded to the root element.

Expand Down
8 changes: 4 additions & 4 deletions packages/material-ui-lab/src/AvatarGroup/AvatarGroup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export interface AvatarGroupProps
* The avatars to stack.
*/
children: React.ReactNode;
/**
* Spacing between avatars.
*/
spacing?: 'small' | 'medium' | number;
/**
* Max avatars to show before +x.
*/
max?: number;
/**
* Spacing between avatars.
*/
spacing?: 'small' | 'medium' | number;
}

export type AvatarGroupClassKey = 'root' | 'avatar';
Expand Down
26 changes: 18 additions & 8 deletions packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export const styles = theme => ({
});

const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
const { children: childrenProp, classes, className, spacing = 'medium', max = 5, ...other } = props;
const {
children: childrenProp,
classes,
className,
spacing = 'medium',
max = 5,
...other
} = props;

const children = React.Children.toArray(childrenProp).filter(child => {
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -54,14 +61,17 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
},
});
})}
{extraAvatars ?
{extraAvatars ? (
<Avatar
className={classes.avatar}
style={{
zIndex: 0,
marginLeft: spacing && SPACINGS[spacing] !== undefined ? SPACINGS[spacing] : -spacing,
}}>+{extraAvatars}</Avatar>
: null}
}}
>
+{extraAvatars}
</Avatar>
) : null}
</div>
);
});
Expand All @@ -85,13 +95,13 @@ AvatarGroup.propTypes = {
*/
className: PropTypes.string,
/**
* The spacing between avatars. A group with `small` spacing value has a larger overlap.
* Max avatars to show before +x.
*/
spacing: PropTypes.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.number]),
max: PropTypes.number,
/**
* The maximum number of avatars to display. An additional text avatar will display the number of hidden avatars, if any.
* Spacing between avatars.
*/
max: PropTypes.number,
spacing: PropTypes.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.number]),
};

export default withStyles(styles, { name: 'MuiAvatarGroup' })(AvatarGroup);
16 changes: 16 additions & 0 deletions packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import { expect } from 'chai';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '@material-ui/core/test-utils/describeConformance';
import { createClientRender } from 'test/utils/createClientRender';
import AvatarGroup from './AvatarGroup';
import { Avatar } from '@material-ui/core';

describe('<AvatarGroup />', () => {
let mount;
let classes;
const render = createClientRender();

before(() => {
mount = createMount({ strict: true });
Expand All @@ -23,4 +27,16 @@ describe('<AvatarGroup />', () => {
refInstanceof: window.HTMLDivElement,
skip: ['componentProp'],
}));

it('should not display all the avatars', () => {
const { container } = render(
<AvatarGroup max={1}>
<Avatar src="broken-url" />
<Avatar src="broken-url" />
<Avatar src="broken-url" />
</AvatarGroup>,
);
expect(container.querySelectorAll('img').length).to.equal(1);
expect(container.textContent).to.equal('+2');
});
});

0 comments on commit 69f9942

Please sign in to comment.