Skip to content

Commit 93989ea

Browse files
committed
add numeric size support to Avatar's badge + unit-tests
1 parent 2f378e7 commit 93989ea

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

Lines changed: 37 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,40 @@ describe('Avatar Badge', () => {
5859
expect(uut.renderBadge()).toEqual(undefined);
5960
});
6061
});
62+
63+
describe('badgeProps.size, supports enum or number', () => {
64+
it('should return 10 as the size number given', () => {
65+
const uut = new Avatar({badgeProps: {size: 10}});
66+
expect(uut.getBadgeSize()).toEqual(10);
67+
});
68+
69+
it('should return 876 as the size number given', () => {
70+
const uut = new Avatar({badgeProps: {size: 876}});
71+
expect(uut.getBadgeSize()).toEqual(876);
72+
});
73+
74+
it('should return 0 as the given size number', () => {
75+
const uut = new Avatar({badgeProps: {size: 0}});
76+
expect(uut.getBadgeSize()).toEqual(0);
77+
});
78+
79+
it('should return the first badge size mapped by given key', () => {
80+
const firstSizeKey = Object.keys(BADGE_SIZES)[1];
81+
const uut = new Avatar({badgeProps: {size: firstSizeKey}});
82+
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[firstSizeKey]);
83+
});
84+
85+
it('should return the last badge size mapped by given key', () => {
86+
const keys = Object.keys(BADGE_SIZES);
87+
const lastSizeKey = keys[keys.length - 1];
88+
const uut = new Avatar({badgeProps: {size: lastSizeKey}});
89+
expect(uut.getBadgeSize()).toEqual(BADGE_SIZES[lastSizeKey]);
90+
});
91+
92+
it('should return undefined for a non-exist size type', () => {
93+
const sizeKey = '!NOT_A_VALID_ENUM$';
94+
const uut = new Avatar({badgeProps: {size: sizeKey}});
95+
expect(uut.getBadgeSize()).toBeUndefined();
96+
});
97+
});
6198
});

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];
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)