-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29309 from storybookjs/valentin/add-global-error-…
…modal Addon-Test: Implement Global Error Modal
- Loading branch information
Showing
8 changed files
with
209 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
code/addons/test/src/components/GlobalErrorModal.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { ManagerContext } from 'storybook/internal/manager-api'; | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { expect, fn, userEvent, within } from '@storybook/test'; | ||
|
||
import dedent from 'ts-dedent'; | ||
|
||
import { GlobalErrorModal } from './GlobalErrorModal'; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const managerContext: any = { | ||
state: {}, | ||
api: { | ||
getDocsUrl: fn(({ subpath }) => `https://storybook.js.org/docs/${subpath}`).mockName( | ||
'api::getDocsUrl' | ||
), | ||
}, | ||
}; | ||
|
||
const meta = { | ||
component: GlobalErrorModal, | ||
decorators: [ | ||
(storyFn) => ( | ||
<ManagerContext.Provider value={managerContext}> | ||
<div | ||
style={{ | ||
width: '100%', | ||
minWidth: '1200px', | ||
height: '800px', | ||
background: | ||
'repeating-linear-gradient(45deg, #000000, #ffffff 50px, #ffffff 50px, #ffffff 80px)', | ||
}} | ||
> | ||
{storyFn()} | ||
</div> | ||
</ManagerContext.Provider> | ||
), | ||
], | ||
args: { | ||
onRerun: fn(), | ||
onClose: fn(), | ||
open: false, | ||
}, | ||
} satisfies Meta<typeof GlobalErrorModal>; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = { | ||
args: { | ||
error: dedent` | ||
ReferenceError: FAIL is not defined | ||
at Constraint.execute (the-best-file.js:525:2) | ||
at Constraint.recalculate (the-best-file.js:424:21) | ||
at Planner.addPropagate (the-best-file.js:701:6) | ||
at Constraint.satisfy (the-best-file.js:184:15) | ||
at Planner.incrementalAdd (the-best-file.js:591:21) | ||
at Constraint.addConstraint (the-best-file.js:162:10) | ||
at Constraint.BinaryConstraint (the-best-file.js:346:7) | ||
at Constraint.EqualityConstraint (the-best-file.js:515:38) | ||
at chainTest (the-best-file.js:807:6) | ||
at deltaBlue (the-best-file.js:879:2)`, | ||
}, | ||
render: (props) => { | ||
const [isOpen, setOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<GlobalErrorModal {...props} open={isOpen} /> | ||
<button onClick={() => setOpen(true)}>Open modal</button> | ||
</> | ||
); | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement.parentElement!); | ||
const button = canvas.getByText('Open modal'); | ||
await userEvent.click(button); | ||
await expect(canvas.findByText('Storybook Tests error details')).resolves.toBeInTheDocument(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
|
||
import { Button, IconButton, Modal } from 'storybook/internal/components'; | ||
import { useStorybookApi } from 'storybook/internal/manager-api'; | ||
|
||
import { CloseIcon, SyncIcon } from '@storybook/icons'; | ||
import { styled } from '@storybook/theming'; | ||
|
||
import { TROUBLESHOOTING_LINK } from '../constants'; | ||
|
||
interface GlobalErrorModalProps { | ||
error: string; | ||
open: boolean; | ||
onClose: () => void; | ||
onRerun: () => void; | ||
} | ||
|
||
const ModalBar = styled.div({ | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
alignItems: 'center', | ||
padding: '6px 6px 6px 20px', | ||
}); | ||
|
||
const ModalActionBar = styled.div({ | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
alignItems: 'center', | ||
}); | ||
|
||
const ModalTitle = styled.div(({ theme: { typography } }) => ({ | ||
fontSize: typography.size.s2, | ||
fontWeight: typography.weight.bold, | ||
})); | ||
|
||
const ModalStackTrace = styled.pre(({ theme }) => ({ | ||
whiteSpace: 'pre-wrap', | ||
overflow: 'auto', | ||
maxHeight: '60vh', | ||
margin: 0, | ||
padding: `20px`, | ||
fontFamily: theme.typography.fonts.mono, | ||
fontSize: '12px', | ||
borderTop: `1px solid ${theme.appBorderColor}`, | ||
borderRadius: 0, | ||
})); | ||
|
||
const TroubleshootLink = styled.a(({ theme }) => ({ | ||
color: theme.color.defaultText, | ||
})); | ||
|
||
export function GlobalErrorModal({ onRerun, onClose, error, open }: GlobalErrorModalProps) { | ||
const api = useStorybookApi(); | ||
|
||
const troubleshootURL = api.getDocsUrl({ | ||
subpath: TROUBLESHOOTING_LINK, | ||
versioned: true, | ||
renderer: true, | ||
}); | ||
|
||
return ( | ||
<Modal onEscapeKeyDown={onClose} onInteractOutside={onClose} open={open}> | ||
<ModalBar> | ||
<ModalTitle>Storybook Tests error details</ModalTitle> | ||
<ModalActionBar> | ||
<Button onClick={onRerun} variant="ghost"> | ||
<SyncIcon /> | ||
Rerun | ||
</Button> | ||
<Button variant="ghost" asChild> | ||
<a target="_blank" href={troubleshootURL} rel="noreferrer"> | ||
Troubleshoot | ||
</a> | ||
</Button> | ||
<IconButton onClick={onClose}> | ||
<CloseIcon /> | ||
</IconButton> | ||
</ModalActionBar> | ||
</ModalBar> | ||
<ModalStackTrace> | ||
{error} | ||
<br /> | ||
<br /> | ||
Troubleshoot:{' '} | ||
<TroubleshootLink target="_blank" href={troubleshootURL}> | ||
{troubleshootURL} | ||
</TroubleshootLink> | ||
</ModalStackTrace> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters