-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract hasPhotoChanges util and add tests
- Loading branch information
Showing
3 changed files
with
86 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |