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 8 commits into
base: next-8.5
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const Args = () => (
/>
<Node value={{ __date__: { value: new Date(1600000000000).toISOString() } }} />
<Node value={{ __date__: { value: new Date(1600000000123).toISOString() } }} />
<Node value={{ __date__: { value: new Date(1600000000123) } }} />
<Node value={{ __error__: { name: 'EvalError', message: '' } }} />
<Node value={{ __error__: { name: 'SyntaxError', message: "Can't do that" } }} />
<Node
Expand Down
15 changes: 13 additions & 2 deletions code/addons/interactions/src/components/MethodCall.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReactElement } from 'react';
import React, { Fragment } from 'react';
import { logger } from 'storybook/internal/client-logger';

import { useTheme } from 'storybook/internal/theming';

Expand Down Expand Up @@ -344,9 +345,19 @@ export const ElementNode = ({
);
};

export const DateNode = ({ value }: { value: string }) => {
const [date, time, ms] = value.split(/[T.Z]/);
export const DateNode = ({ value }: { value: string | Date }) => {
let parsed = new Date(value);
if (isNaN(+parsed)) {
logger.warn('Invalid date value:', value);
parsed = null;
}

const colors = useThemeColors();
if (!parsed) {
return <span style={{ whiteSpace: 'nowrap', color: colors.date }}>Invalid date</span>;
}

const [date, time, ms] = parsed.toISOString().split(/[T.Z]/);
return (
<span style={{ whiteSpace: 'nowrap', color: colors.date }}>
{date}
Expand Down
Loading