-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexei Mochalov <[email protected]>
- Loading branch information
Showing
17 changed files
with
421 additions
and
21 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
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
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
64 changes: 64 additions & 0 deletions
64
catalog/app/components/Preview/quick/Markdown/Render.spec.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,64 @@ | ||
import * as React from 'react' | ||
import renderer from 'react-test-renderer' | ||
|
||
import AsyncResult from 'utils/AsyncResult' | ||
|
||
import Render from './Render' | ||
|
||
jest.mock( | ||
'constants/config', | ||
jest.fn(() => {}), | ||
) | ||
|
||
const useMarkdownRenderer = jest.fn() | ||
jest.mock('components/Preview/loaders/Markdown', () => ({ | ||
...jest.requireActual('components/Preview/loaders/Markdown'), | ||
useMarkdownRenderer: jest.fn(() => useMarkdownRenderer()), | ||
})) | ||
|
||
jest.mock( | ||
'components/Preview/renderers/Markdown', | ||
() => | ||
({ rendered }: { rendered: string }) => ( | ||
// eslint-disable-next-line react/no-danger | ||
<b dangerouslySetInnerHTML={{ __html: rendered }}>Markdown</b> | ||
), | ||
) | ||
|
||
jest.mock( | ||
'@material-ui/lab', | ||
jest.fn(() => ({ | ||
Alert: ({ children }: { children: string }) => <div>Error: {children}</div>, | ||
})), | ||
) | ||
|
||
const handle = { | ||
bucket: 'foo', | ||
key: 'bar', | ||
} | ||
|
||
describe('app/components/Preview/quick/Render.spec.tsx', () => { | ||
it('it shows the error for Init state, because it is intended to run with already resolved value', () => { | ||
useMarkdownRenderer.mockReturnValue(AsyncResult.Init()) | ||
const tree = renderer.create(<Render {...{ handle, value: 'any' }} />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
|
||
it('it shows the error for Pending state, because it is intended to run with already resolved value', () => { | ||
useMarkdownRenderer.mockReturnValue(AsyncResult.Pending()) | ||
const tree = renderer.create(<Render {...{ handle, value: 'any' }} />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
|
||
it('returns error on Err', () => { | ||
useMarkdownRenderer.mockReturnValue(AsyncResult.Err(new Error('some error'))) | ||
const tree = renderer.create(<Render {...{ handle, value: 'any' }} />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
|
||
it('returns markdown on data', () => { | ||
useMarkdownRenderer.mockReturnValue(AsyncResult.Ok('<h1>It works</h1>')) | ||
const tree = renderer.create(<Render {...{ handle, value: 'any' }} />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
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,30 @@ | ||
import * as React from 'react' | ||
import * as Lab from '@material-ui/lab' | ||
|
||
import { useMarkdownRenderer } from 'components/Preview/loaders/Markdown' | ||
import Markdown from 'components/Preview/renderers/Markdown' | ||
import type * as Model from 'model' | ||
import AsyncResult from 'utils/AsyncResult' | ||
|
||
import Skeleton from './Skeleton' | ||
|
||
interface RenderProps { | ||
value: string | ||
handle: Model.S3.S3ObjectLocation | ||
} | ||
|
||
export default function Render({ value, handle }: RenderProps) { | ||
const result = useMarkdownRenderer(AsyncResult.Ok(value), handle) | ||
// `result` is never `Pending` or `Init`, because we pass the `AsyncResult.Ok` value | ||
// but it can be `Err` if some post-processing fails | ||
return AsyncResult.case({ | ||
_: (x: { value?: Error }) => ( | ||
<Lab.Alert severity="error">{x.value?.message || 'Unexpected state'}</Lab.Alert> | ||
), | ||
Ok: (rendered: string) => ( | ||
<React.Suspense fallback={<Skeleton />}> | ||
<Markdown rendered={rendered} /> | ||
</React.Suspense> | ||
), | ||
})(result) | ||
} |
24 changes: 24 additions & 0 deletions
24
catalog/app/components/Preview/quick/Markdown/Skeleton.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,24 @@ | ||
import * as React from 'react' | ||
import * as M from '@material-ui/core' | ||
|
||
import Skel from 'components/Skeleton' | ||
|
||
const useSkeletonStyles = M.makeStyles((t) => ({ | ||
line: { | ||
height: t.spacing(3), | ||
marginBottom: t.spacing(1), | ||
}, | ||
})) | ||
|
||
const LINES = [80, 50, 100, 60, 30, 80, 50, 100, 60, 30, 20, 70] | ||
|
||
export default function Skeleton() { | ||
const classes = useSkeletonStyles() | ||
return ( | ||
<div> | ||
{LINES.map((width, index) => ( | ||
<Skel className={classes.line} width={`${width}%`} key={width + index} /> | ||
))} | ||
</div> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
catalog/app/components/Preview/quick/Markdown/__snapshots__/Render.spec.tsx.snap
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,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`app/components/Preview/quick/Render.spec.tsx it shows the error for Init state, because it is intended to run with already resolved value 1`] = ` | ||
<div> | ||
Error: | ||
Unexpected state | ||
</div> | ||
`; | ||
|
||
exports[`app/components/Preview/quick/Render.spec.tsx it shows the error for Pending state, because it is intended to run with already resolved value 1`] = ` | ||
<div> | ||
Error: | ||
Unexpected state | ||
</div> | ||
`; | ||
|
||
exports[`app/components/Preview/quick/Render.spec.tsx returns error on Err 1`] = ` | ||
<div> | ||
Error: | ||
some error | ||
</div> | ||
`; | ||
|
||
exports[`app/components/Preview/quick/Render.spec.tsx returns markdown on data 1`] = ` | ||
<b | ||
dangerouslySetInnerHTML={ | ||
{ | ||
"__html": "<h1>It works</h1>", | ||
} | ||
} | ||
> | ||
Markdown | ||
</b> | ||
`; |
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,15 @@ | ||
import * as React from 'react' | ||
|
||
import Skeleton from './Skeleton' | ||
|
||
const RenderLazy = React.lazy(() => import('./Render')) | ||
|
||
export { default as Skeleton } from './Skeleton' | ||
|
||
export function Render(props: Parameters<typeof RenderLazy>[0]) { | ||
return ( | ||
<React.Suspense fallback={<Skeleton />}> | ||
<RenderLazy {...props} /> | ||
</React.Suspense> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
catalog/app/components/Preview/quick/__snapshots__/index.spec.tsx.snap
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`app/components/Preview/quick/index.spec.tsx QuickPreview renders Markdown 1`] = ` | ||
<h1> | ||
This is Markdown quick preview | ||
</h1> | ||
`; | ||
|
||
exports[`app/components/Preview/quick/index.spec.tsx QuickPreview renders no preview if unsupported file type 1`] = ` | ||
<p | ||
className="MuiTypography-root MuiTypography-body1" | ||
> | ||
There is no content for quick preview | ||
</p> | ||
`; | ||
|
||
exports[`app/components/Preview/quick/index.spec.tsx QuickPreview renders no value 1`] = ` | ||
<p | ||
className="MuiTypography-root MuiTypography-body1" | ||
> | ||
There is no content for quick preview | ||
</p> | ||
`; |
Oops, something went wrong.