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

TestAddon: Refactor UI & add config options #29662

Merged
merged 26 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d849ed7
allow passing generic up
ndelangen Nov 19, 2024
917bb30
add useful generic types
ndelangen Nov 19, 2024
e47e23a
do not wrap custom render
ndelangen Nov 19, 2024
4e01e33
refactor ui of test addon
ndelangen Nov 19, 2024
3196ee6
add editing
ndelangen Nov 19, 2024
ac091c1
cleanup
ndelangen Nov 20, 2024
e9aa7fb
Merge branch 'next' into norbert/testmodule-options
ndelangen Nov 20, 2024
c889cfb
move editing to local state and move custom state to config and make …
ndelangen Nov 20, 2024
fa74d64
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 21, 2024
7a53842
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 22, 2024
b66b5fe
emit config change event when it's set & emit config along with switc…
ndelangen Nov 22, 2024
34b2a53
Merge branch 'norbert/testmodule-options' of https://github.com/story…
ndelangen Nov 22, 2024
b8e0e2e
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 22, 2024
6938f52
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 22, 2024
f62c975
add to list of exports
ndelangen Nov 22, 2024
29f1b77
Merge branch 'norbert/testmodule-options' of https://github.com/story…
ndelangen Nov 22, 2024
f603ffb
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 22, 2024
4956363
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 22, 2024
255e11f
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 25, 2024
942e321
Merge branch 'determine-total-test-count' into norbert/testmodule-opt…
ndelangen Nov 25, 2024
2e4075f
fixes
ndelangen Nov 25, 2024
27279ec
refactor the sidebarbottom to support dynamic height content
ndelangen Nov 26, 2024
adb4e15
add more stories & cleanup
ndelangen Nov 26, 2024
8ebbba4
Merge branch 'next' into norbert/testmodule-options
ndelangen Nov 26, 2024
e03be02
fix tests
ndelangen Nov 26, 2024
3f34f9d
rename slow to indicate it's about the CSS transition being toggled o…
ndelangen Nov 26, 2024
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
55 changes: 55 additions & 0 deletions code/addons/test/src/components/Description.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';

import { Link as LinkComponent } from 'storybook/internal/components';
import { type TestProviderConfig, type TestProviderState } from 'storybook/internal/core-events';
import { styled } from 'storybook/internal/theming';

import { RelativeTime } from './RelativeTime';

export const DescriptionStyle = styled.div(({ theme }) => ({
fontSize: theme.typography.size.s1,
color: theme.barTextColor,
}));

export function Description({
errorMessage,
setIsModalOpen,
state,
}: {
state: TestProviderConfig & TestProviderState;
errorMessage: string;
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
}) {
let description: string | React.ReactNode = 'Not run';
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved

if (state.running) {
description = state.progress
? `Testing... ${state.progress.numPassedTests}/${state.progress.numTotalTests}`
: 'Starting...';
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: numTotalTests could be undefined here, which would show 'Testing... 5/undefined'

} else if (state.failed && !errorMessage) {
description = '';
Comment on lines +29 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Setting description to empty string when failed without error message could lead to confusing UI state. Consider showing a default failure message instead.

} else if (state.crashed || (state.failed && errorMessage)) {
description = (
<>
<LinkComponent
isButton
onClick={() => {
setIsModalOpen(true);
}}
>
{state.error?.name || 'View full error'}
</LinkComponent>
</>
Comment on lines +34 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Fragment wrapper is unnecessary since LinkComponent is the only child

);
} else if (state.progress?.finishedAt) {
description = (
<RelativeTime
timestamp={new Date(state.progress.finishedAt)}
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved
testCount={state.progress.numTotalTests}
/>
);
} else if (state.watching) {
description = 'Watching for file changes';
}
return <DescriptionStyle id="testing-module-description">{description}</DescriptionStyle>;
}
19 changes: 18 additions & 1 deletion code/addons/test/src/components/RelativeTime.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { useEffect, useState } from 'react';

import { getRelativeTimeString } from '../manager';
export function getRelativeTimeString(date: Date): string {
const delta = Math.round((date.getTime() - Date.now()) / 1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: delta calculation will be negative for future dates, but Math.abs is only used later - could cause incorrect time displays

const cutoffs = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity];
const units: Intl.RelativeTimeFormatUnit[] = [
'second',
'minute',
'hour',
'day',
'week',
'month',
'year',
];

const unitIndex = cutoffs.findIndex((cutoff) => cutoff > Math.abs(delta));
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1;
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
return rtf.format(Math.floor(delta / divisor), units[unitIndex]);
}

export const RelativeTime = ({ timestamp, testCount }: { timestamp: Date; testCount: number }) => {
const [relativeTimeString, setRelativeTimeString] = useState(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: initial state should be typed as useState<string | null>(null)

Expand Down
4 changes: 2 additions & 2 deletions code/addons/test/src/components/Subnav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const StyledSubnav = styled.nav(({ theme }) => ({
paddingLeft: 15,
}));

export interface SubnavProps {
interface SubnavProps {
controls: Controls;
controlStates: ControlStates;
status: Call['status'];
Expand All @@ -64,7 +64,7 @@ const Note = styled(TooltipNote)(({ theme }) => ({
fontFamily: theme.typography.fonts.base,
}));

export const StyledIconButton = styled(IconButton as any)(({ theme }) => ({
const StyledIconButton = styled(IconButton)(({ theme }) => ({
color: theme.textMutedColor,
margin: '0 3px',
}));
Expand Down
158 changes: 158 additions & 0 deletions code/addons/test/src/components/TestProviderRender.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React from 'react';

import type { TestProviderConfig, TestProviderState } from 'storybook/internal/core-events';
import { ManagerContext } from 'storybook/internal/manager-api';
import { styled } from 'storybook/internal/theming';
import { Addon_TypesEnum } from 'storybook/internal/types';

import type { Meta, StoryObj } from '@storybook/react';
import { fn, within } from '@storybook/test';

import type { Config, Details } from '../constants';
import { TestProviderRender } from './TestProviderRender';

type Story = StoryObj<typeof TestProviderRender>;
const managerContext: any = {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Avoid using 'any' type for managerContext. Consider creating a proper type definition based on ManagerContext requirements.

state: {
testProviders: {
'test-provider-id': {
id: 'test-provider-id',
name: 'Test Provider',
type: Addon_TypesEnum.experimental_TEST_PROVIDER,
},
},
},
api: {
getDocsUrl: fn().mockName('api::getDocsUrl'),
emit: fn().mockName('api::emit'),
updateTestProviderState: fn().mockName('api::updateTestProviderState'),
},
};

const config: TestProviderConfig = {
id: 'test-provider-id',
name: 'Test Provider',
type: Addon_TypesEnum.experimental_TEST_PROVIDER,
runnable: true,
watchable: true,
};

const baseState: TestProviderState<Details, Config> = {
cancellable: true,
cancelling: false,
crashed: false,
error: null,
failed: false,
running: false,
watching: false,
config: {
a11y: false,
coverage: false,
},
details: {
testResults: [
{
endTime: 0,
startTime: 0,
status: 'passed',
message: 'All tests passed',
results: [
{
storyId: 'story-id',
status: 'success',
duration: 100,
testRunId: 'test-run-id',
},
],
},
],
},
};

const Content = styled.div({
padding: '12px 6px',
display: 'flex',
flexDirection: 'column',
gap: '12px',
});

export default {
title: 'TestProviderRender',
component: TestProviderRender,
args: {
state: {
...config,
...baseState,
},
api: managerContext.api,
},
decorators: [
(StoryFn) => (
<Content>
<StoryFn />
</Content>
),
(StoryFn) => (
<ManagerContext.Provider value={managerContext}>
<StoryFn />
</ManagerContext.Provider>
),
],
} as Meta<typeof TestProviderRender>;

export const Default: Story = {
args: {
state: {
...config,
...baseState,
},
},
};

export const Running: Story = {
args: {
state: {
...config,
...baseState,
running: true,
},
},
};

export const EnableA11y: Story = {
args: {
state: {
...config,
...baseState,
details: {
testResults: [],
},
config: {
a11y: true,
coverage: false,
},
},
},
};

export const EnableEditing: Story = {
args: {
state: {
...config,
...baseState,
config: {
a11y: true,
coverage: false,
},
details: {
testResults: [],
},
},
},

play: async ({ canvasElement }) => {
const screen = within(canvasElement);

screen.getByLabelText('Edit').click();
},
Comment on lines +153 to +157
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Add error handling in case Edit button is not found

};
Loading