Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-interactions: Fix using dates in expect statements #28413

Open
wants to merge 15 commits into
base: next-8.5
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: refactor
  • Loading branch information
yann-combarnous committed Jul 1, 2024
commit ec0348e2e691379da55552e2ae39440622a5caec
2 changes: 1 addition & 1 deletion code/addons/interactions/src/components/MethodCall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export const ElementNode = ({
};

export const DateNode = ({ value }: { value: string | Date }) => {
const [date, time, ms] = (value instanceof Date ? value.toISOString() : value).split(/[T.Z]/);
const [date, time, ms] = new Date(value).toISOString().split(/[T.Z]/);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change assumes value can always be converted to a valid Date. However, if value is an invalid date string, new Date(value) will result in an invalid Date object (Invalid Date).

It can lead to runtime errors if value is not a valid date string. Consider adding error handling for invalid dates.

Suggestion: To handle potential invalid date values, add error handling and validate the date conversion.

Something like this:

export const DateNode = ({ value }: { value: string | Date }) => {
  let dateStr, time, ms;

  try {
    const dateObj = new Date(value);
    if (isNaN(dateObj.getTime())) {
      throw new Error('Invalid date');
    }
    [dateStr, time, ms] = dateObj.toISOString().split(/[T.Z]/);
  } catch (error) {
    console.error('Invalid date value provided to DateNode:', value);
    [dateStr, time, ms] = ['Invalid date', '', ''];
  }

  const colors = useThemeColors();
  return (
    <span style={{ whiteSpace: 'nowrap', color: colors.date }}>
      {dateStr} {time} {ms}
    </span>
  );
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx @jadehamel , updated PR with catch for invalid date.

Copy link
Member

@ghengeveld ghengeveld Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good suggestion. I think we can even make do without the try/catch by omitting the getTime call. I left a review with my take on it.

const colors = useThemeColors();
return (
<span style={{ whiteSpace: 'nowrap', color: colors.date }}>
Expand Down