Skip to content

add numeric size support to Avatar's badge + unit-tests #850

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

Merged
merged 5 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions src/components/avatar/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Avatar} from '../index';
import {Colors} from '../../../style';
import {BADGE_SIZES} from '../../badge';

describe('Avatar Badge', () => {
describe('getStatusBadgeColor', () => {
Expand Down Expand Up @@ -58,4 +59,35 @@ describe('Avatar Badge', () => {
expect(uut.renderBadge()).toEqual(undefined);
});
});

describe('badgeProps.size, supports enum or number', () => {
it('should return 99 as the size number given', () => {
const uut = new Avatar({badgeProps: {size: 99}});
expect(uut.getBadgeSize()).toEqual(99);
});

it('should return 0 as the given size number', () => {
const uut = new Avatar({badgeProps: {size: 0}});
expect(uut.getBadgeSize()).toEqual(0);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the end of the world obviously but having 3 tests testing numeric input might be 2 tests too much 😸 .
If one of these tests fail, all of them will fail for the same reason.
For your consideration...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, keeping two for zero/undefined check + some number

it('should return the first badge size mapped by given key', () => {
const firstSizeKey = Object.keys(BADGE_SIZES)[1];
const uut = new Avatar({badgeProps: {size: firstSizeKey}});
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[firstSizeKey]);
});

it('should return the last badge size mapped by given key', () => {
const keys = Object.keys(BADGE_SIZES);
const lastSizeKey = keys[keys.length - 1];
const uut = new Avatar({badgeProps: {size: lastSizeKey}});
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[lastSizeKey]);
});

it('should return a default value from Badge for a non-exist size type', () => {
const sizeKey = '!NOT_A_VALID_ENUM$';
const uut = new Avatar({badgeProps: {size: sizeKey}});
expect(typeof uut.getBadgeSize()).toBe('number');
});
});
});
11 changes: 9 additions & 2 deletions src/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,21 @@ class Avatar extends PureComponent<AvatarPropTypes> {
return _.get(this.props, 'badgeProps.backgroundColor') || statusColor || onlineColor;
}

getBadgeSize = () => _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
getBadgeSize = () => {
const badgeSize = _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);

if (_.isString(badgeSize)) {
return BADGE_SIZES[badgeSize] || BADGE_SIZES[DEFAULT_BADGE_SIZE];
}
return badgeSize;
}

getBadgePosition() {
const {size, badgePosition} = this.props;
const radius = size / 2;
const x = Math.sqrt(radius ** 2 * 2);
const y = x - radius;
const shift = Math.sqrt(y ** 2 / 2) - (BADGE_SIZES[this.getBadgeSize()] + this.getBadgeBorderWidth() * 2) / 2;
const shift = Math.sqrt(y ** 2 / 2) - (this.getBadgeSize() + this.getBadgeBorderWidth() * 2) / 2;
const badgeLocation = _.split(_.toLower(badgePosition), '_', 2);
const badgeAlignment = {position: 'absolute', [badgeLocation[0]]: shift, [badgeLocation[1]]: shift};

Expand Down