Skip to content

Commit 63e9377

Browse files
mendyEdriArnonZ
andauthored
add numeric size support to Avatar's badge + unit-tests (#850)
* add numeric size support to Avatar's badge + unit-tests * fix missing default value on non-exist size key. change unittest according to default value change, removed redundant test. * Update src/components/avatar/__tests__/index.spec.js fix typo Co-authored-by: arnonz <[email protected]>
1 parent 7e35bcf commit 63e9377

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/components/avatar/__tests__/index.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Avatar} from '../index';
22
import {Colors} from '../../../style';
3+
import {BADGE_SIZES} from '../../badge';
34

45
describe('Avatar Badge', () => {
56
describe('getStatusBadgeColor', () => {
@@ -58,4 +59,35 @@ describe('Avatar Badge', () => {
5859
expect(uut.renderBadge()).toEqual(undefined);
5960
});
6061
});
62+
63+
describe('badgeProps.size, supports enum or number', () => {
64+
it('should return 99 as the size number given', () => {
65+
const uut = new Avatar({badgeProps: {size: 99}});
66+
expect(uut.getBadgeSize()).toEqual(99);
67+
});
68+
69+
it('should return 0 as the given size number', () => {
70+
const uut = new Avatar({badgeProps: {size: 0}});
71+
expect(uut.getBadgeSize()).toEqual(0);
72+
});
73+
74+
it('should return the first badge size mapped by given key', () => {
75+
const firstSizeKey = Object.keys(BADGE_SIZES)[1];
76+
const uut = new Avatar({badgeProps: {size: firstSizeKey}});
77+
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[firstSizeKey]);
78+
});
79+
80+
it('should return the last badge size mapped by given key', () => {
81+
const keys = Object.keys(BADGE_SIZES);
82+
const lastSizeKey = keys[keys.length - 1];
83+
const uut = new Avatar({badgeProps: {size: lastSizeKey}});
84+
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[lastSizeKey]);
85+
});
86+
87+
it('should return a default value from Badge for a non-exist size type', () => {
88+
const sizeKey = '!NOT_A_VALID_ENUM$';
89+
const uut = new Avatar({badgeProps: {size: sizeKey}});
90+
expect(typeof uut.getBadgeSize()).toBe('number');
91+
});
92+
});
6193
});

src/components/avatar/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,21 @@ class Avatar extends PureComponent<AvatarPropTypes> {
222222
return _.get(this.props, 'badgeProps.backgroundColor') || statusColor || onlineColor;
223223
}
224224

225-
getBadgeSize = () => _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
225+
getBadgeSize = () => {
226+
const badgeSize = _.get(this.props, 'badgeProps.size', DEFAULT_BADGE_SIZE);
227+
228+
if (_.isString(badgeSize)) {
229+
return BADGE_SIZES[badgeSize] || BADGE_SIZES[DEFAULT_BADGE_SIZE];
230+
}
231+
return badgeSize;
232+
}
226233

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

0 commit comments

Comments
 (0)