Skip to content

Commit

Permalink
Fix TypeError: Cannot read property 'name' of null (#2286)
Browse files Browse the repository at this point in the history
Refactoring props structure, add back the safe navigator operator and add new unit tests

References: CV2-6130
  • Loading branch information
danielevalverde committed Feb 7, 2025
1 parent 7465d18 commit a598541
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
10 changes: 3 additions & 7 deletions src/app/components/media/MediaOrigin.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ const getIconAndMessage = (origin, user) => {
}
};

const MediaOrigin = ({
projectMedia: {
media_cluster_origin: origin,
media_cluster_origin_user: user = {},
} = {},
}) => {
const { icon, message, tooltipMessage } = getIconAndMessage(origin, user.name);
const MediaOrigin = ({ projectMedia }) => {
const { media_cluster_origin: origin, media_cluster_origin_user: user } = projectMedia;
const { icon, message, tooltipMessage } = getIconAndMessage(origin, user?.name);

return (
<Tooltip arrow title={tooltipMessage}>
Expand Down
5 changes: 5 additions & 0 deletions src/app/components/media/MediaOrigin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ describe('<MediaOrigin />', () => {
const wrapper = mountWithIntl(<MediaOrigin projectMedia={{ media_cluster_origin: 99, media_cluster_origin_user: { name: 'user' } }} />);
expect(wrapper.find('FormattedMessage').length).toEqual(0);
});

it('should not crash when media cluster origin and media cluster origin user are null', () => {
const wrapper = mountWithIntl(<MediaOrigin projectMedia={{ media_cluster_origin: null, media_cluster_origin_user: null }} />);
expect(wrapper.find('FormattedMessage').length).toEqual(0);
});
});
11 changes: 5 additions & 6 deletions src/app/components/media/MediaOriginBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,14 @@ const getIconAndMessage = (origin, mediaClusterRelationship, user, originTimesta
}
};

const MediaOriginBanner = ({
projectMedia: {
const MediaOriginBanner = ({ projectMedia }) => {
const {
media_cluster_origin: origin,
media_cluster_origin_timestamp: originTimestamp,
media_cluster_origin_user: user = {},
media_cluster_origin_user: user,
media_cluster_relationship: mediaClusterRelationship,
},
}) => {
const { icon, message } = getIconAndMessage(origin, mediaClusterRelationship, user.name, originTimestamp);
} = projectMedia;
const { icon, message } = getIconAndMessage(origin, mediaClusterRelationship, user?.name, originTimestamp);
return (
<div style={{ marginBottom: '8px' }}>
<Alert
Expand Down
5 changes: 5 additions & 0 deletions src/app/components/media/MediaOriginBanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ describe('<MediaOriginBanner />', () => {
const wrapper = mountWithIntl(<MediaOriginBanner projectMedia={{ ...projectMedia, media_cluster_origin: 99 }} />);
expect(wrapper.find('FormattedMessage').length).toEqual(0);
});

it('should not crash when media cluster origin and media cluster origin user are null', () => {
const wrapper = mountWithIntl(<MediaOriginBanner projectMedia={{ media_cluster_origin: null, media_cluster_origin_user: null }} />);
expect(wrapper.find('FormattedMessage').length).toEqual(0);
});
});

0 comments on commit a598541

Please sign in to comment.