xiangyixiangqin/miniapp/components/UserCard/userCardUtils.js
2026-01-02 18:00:49 +08:00

66 lines
1.9 KiB
JavaScript

/**
* UserCard utility functions for testable logic
* These functions extract the core logic from the UserCard component
* to enable property-based testing.
*/
/**
* Determines if the photo should be displayed or blurred
* Property 3: User Card Photo Display
* @param {boolean} isPhotoPublic - Whether the photo is public
* @param {string} firstPhoto - The photo URL
* @returns {Object} Photo display state
*/
export function getPhotoDisplayState(isPhotoPublic, firstPhoto) {
return {
showPhoto: isPhotoPublic && !!firstPhoto,
showPlaceholder: !isPhotoPublic || !firstPhoto,
isBlurred: !isPhotoPublic
}
}
/**
* Determines which badges should be displayed
* Property 4: User Card Badge Display
* @param {Object} userData - User data object
* @param {boolean} userData.isMember - Whether user is a member
* @param {boolean} userData.isRealName - Whether user is verified
* @param {boolean} userData.viewedToday - Whether user was viewed today
* @returns {Object} Badge display state
*/
export function getBadgeDisplayState(userData) {
const { isMember = false, isRealName = false, viewedToday = false } = userData || {}
return {
showMemberBadge: isMember === true,
showRealNameBadge: isRealName === true,
showViewedTodayIndicator: viewedToday === true
}
}
/**
* Validates user card props
* @param {Object} props - Component props
* @returns {Object} Validation result
*/
export function validateUserCardProps(props) {
const errors = []
if (typeof props.userId !== 'number' || props.userId <= 0) {
errors.push('userId must be a positive number')
}
if (typeof props.age !== 'number' || props.age < 0) {
errors.push('age must be a non-negative number')
}
if (typeof props.height !== 'number' || props.height < 0) {
errors.push('height must be a non-negative number')
}
return {
isValid: errors.length === 0,
errors
}
}