Skip to content

Commit

Permalink
fix(Contacts): Func getPrimaryOrFirst should not crash for array of null
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Apr 25, 2024
1 parent 97faf6d commit 3e4061a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cozy-client/src/models/contact.js
Expand Up @@ -10,7 +10,7 @@ export const CONTACTS_DOCTYPE = 'io.cozy.contacts'
*/

export const getPrimaryOrFirst = property => obj =>
!obj[property] || obj[property].length === 0
!obj || !obj[property] || obj[property].length === 0 || !obj[property][0]
? ''
: obj[property].find(property => property.primary) || obj[property][0]

Expand Down
36 changes: 36 additions & 0 deletions packages/cozy-client/src/models/contact.spec.js
Expand Up @@ -15,6 +15,12 @@ import {
} from './contact'

describe('getPrimaryOrFirst', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryOrFirst('email')({ email: undefined })).toEqual('')
expect(getPrimaryOrFirst('email')({ email: [null] })).toEqual('')
expect(getPrimaryOrFirst('email')({ email: [undefined] })).toEqual('')
})

it("should return the first contact's email address if no primary", () => {
const contact = {
email: [
Expand Down Expand Up @@ -136,6 +142,12 @@ describe('getInitials', () => {
})

describe('getPrimaryCozy', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryCozy({ cozy: undefined })).toEqual(undefined)
expect(getPrimaryCozy({ cozy: [null] })).toEqual('')
expect(getPrimaryCozy({ cozy: [undefined] })).toEqual('')
})

it('should return the main cozy', () => {
const contact = {
cozy: [
Expand Down Expand Up @@ -178,6 +190,12 @@ describe('getPrimaryCozy', () => {
})

describe('getPrimaryPhone', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryPhone({ phone: undefined })).toEqual('')
expect(getPrimaryPhone({ phone: [null] })).toEqual('')
expect(getPrimaryPhone({ phone: [undefined] })).toEqual('')
})

it('should return the main phone number', () => {
const contact = {
phone: [
Expand All @@ -192,6 +210,12 @@ describe('getPrimaryPhone', () => {
})

describe('getPrimaryAddress', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryAddress({ address: undefined })).toEqual('')
expect(getPrimaryAddress({ address: [null] })).toEqual('')
expect(getPrimaryAddress({ address: [undefined] })).toEqual('')
})

it('should return the main address', () => {
const contact = {
name: {
Expand Down Expand Up @@ -438,6 +462,12 @@ describe('makeDisplayName', () => {
})

describe('getPrimaryEmail', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryEmail({ email: undefined })).toEqual(undefined)
expect(getPrimaryEmail({ email: [null] })).toEqual('')
expect(getPrimaryEmail({ email: [undefined] })).toEqual('')
})

it('should return the main email', () => {
const contact = {
email: [
Expand Down Expand Up @@ -609,6 +639,12 @@ describe('makeDefaultSortIndexValue', () => {
})

describe('getPrimaryCozyDomain', () => {
it('should not crash if array is undefined, or contents null/undefined', () => {
expect(getPrimaryCozyDomain({ cozy: undefined })).toEqual(undefined)
expect(getPrimaryCozyDomain({ cozy: [null] })).toEqual('')
expect(getPrimaryCozyDomain({ cozy: [undefined] })).toEqual('')
})

it('should returns empty string if no cozy url', () => {
const contact = {
cozy: [{ url: '' }]
Expand Down

0 comments on commit 3e4061a

Please sign in to comment.