Skip to content

Commit

Permalink
extract hasPhotoChanges util and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luiqor committed Nov 23, 2024
1 parent 7ce0a96 commit 913c74b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 45 deletions.
48 changes: 3 additions & 45 deletions src/pages/edit-profile/EditProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, useMemo, useCallback } from 'react'
import { useEffect, useState, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import {
Link,
Expand All @@ -20,8 +20,6 @@ import SidebarMenu from '~/components/sidebar-menu/SidebarMenu'
import {
ButtonVariantEnum,
SizeEnum,
UpdatedPhoto,
EditProfilePhoto,
UpdateUserParams,
UserProfileTabsEnum,
UserRole,
Expand All @@ -37,10 +35,9 @@ import { LoadingStatusEnum } from '~/redux/redux.constants'
import { openAlert } from '~/redux/features/snackbarSlice'
import { snackbarVariants } from '~/constants'
import { authRoutes } from '~/router/constants/authRoutes'
import { areAllValuesEmptyStrings } from '~/utils/are-all-values-empty-strings'
import { isUpdatedPhoto } from '~/utils/is-updated-photo'

import { styles } from '~/pages/edit-profile/EditProfile.styles'
import { hasPhotoChanges } from '~/utils/has-photo-changes'

const EditProfile = () => {
const [initialEditProfileState, setInitialEditProfileState] = useState<
Expand Down Expand Up @@ -84,45 +81,6 @@ const EditProfile = () => {
return JSON.stringify(initialData) !== JSON.stringify(currentData)
}

const hasPhotoChanges = useCallback(
(
initialPhoto: EditProfilePhoto,
currentPhoto: EditProfilePhoto
): boolean => {
if (initialPhoto !== '' && currentPhoto === '') {
return true
}

if (
typeof initialPhoto === 'string' &&
isUpdatedPhoto(currentPhoto) &&
(currentPhoto as UpdatedPhoto).name !== initialPhoto
) {
return true
}

if (
initialPhoto === null &&
isUpdatedPhoto(currentPhoto) &&
areAllValuesEmptyStrings(currentPhoto as UpdatedPhoto)
) {
return true
}

if (
isUpdatedPhoto(initialPhoto) &&
isUpdatedPhoto(currentPhoto) &&
(initialPhoto as UpdatedPhoto).name !==
(currentPhoto as UpdatedPhoto).name
) {
return true
}

return false
},
[]
)

useEffect(() => {
const fetchData = async () => {
await dispatch(
Expand Down Expand Up @@ -174,7 +132,7 @@ const EditProfile = () => {
} else {
return {}
}
}, [profileState, initialEditProfileState, hasPhotoChanges])
}, [profileState, initialEditProfileState])

const isChanged = useMemo<boolean>(
() => Object.keys(changedFields).length > 0,
Expand Down
38 changes: 38 additions & 0 deletions src/utils/has-photo-changes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { EditProfilePhoto, UpdatedPhoto } from '~/types'
import { isUpdatedPhoto } from '~/utils/is-updated-photo'
import { areAllValuesEmptyStrings } from '~/utils/are-all-values-empty-strings'

export const hasPhotoChanges = (
initialPhoto: EditProfilePhoto,
currentPhoto: EditProfilePhoto
): boolean => {
if (initialPhoto !== '' && currentPhoto === '') {
return true
}

if (
typeof initialPhoto === 'string' &&
isUpdatedPhoto(currentPhoto) &&
(currentPhoto as UpdatedPhoto).name !== initialPhoto
) {
return true
}

if (
initialPhoto === null &&
isUpdatedPhoto(currentPhoto) &&
areAllValuesEmptyStrings(currentPhoto as UpdatedPhoto)
) {
return true
}

if (
isUpdatedPhoto(initialPhoto) &&
isUpdatedPhoto(currentPhoto) &&
(initialPhoto as UpdatedPhoto).name !== (currentPhoto as UpdatedPhoto).name
) {
return true
}

return false
}
45 changes: 45 additions & 0 deletions tests/unit/utils/has-photo-changes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { hasPhotoChanges } from '~/utils/has-photo-changes'

describe('hasPhotoChanges', () => {
it('should return true if initialPhoto is not empty and currentPhoto is empty', () => {
const initialPhoto = 'initialPhoto'
const currentPhoto = ''
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(true)
})

it('should return true if initialPhoto is a string and currentPhoto is an UpdatedPhoto with a different name', () => {
const initialPhoto = 'initialPhoto'
const currentPhoto = { src: 'src', name: 'newName' }
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(true)
})

it('should return true if initialPhoto is null and currentPhoto is an UpdatedPhoto with all values empty strings', () => {
const initialPhoto = null
const currentPhoto = { src: '', name: '' }
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(true)
})

it('should return true if both initialPhoto and currentPhoto are UpdatedPhotos with different names', () => {
const initialPhoto = { src: 'src1', name: 'name1' }
const currentPhoto = { src: 'src2', name: 'name2' }
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(true)
})

it('should return false if there are no changes', () => {
const initialPhoto = { src: 'src', name: 'name' }
const currentPhoto = { src: 'src', name: 'name' }
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(false)
})

it('should return false if both initialPhoto and currentPhoto are empty strings', () => {
const initialPhoto = ''
const currentPhoto = ''
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(false)
})

it('should return false if both initialPhoto and currentPhoto are null', () => {
const initialPhoto = null
const currentPhoto = null
expect(hasPhotoChanges(initialPhoto, currentPhoto)).toBe(false)
})
})

0 comments on commit 913c74b

Please sign in to comment.