-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathImageBodyView.stories.tsx
More file actions
157 lines (141 loc) · 4.54 KB
/
ImageBodyView.stories.tsx
File metadata and controls
157 lines (141 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import React, { type ReactNode } from "react";
import { expect, fn, userEvent, within } from "storybook/test";
import type { Meta, StoryObj } from "@storybook/react-vite";
import { useMockedViewModel } from "../../../../../core/viewmodel/useMockedViewModel";
import { withViewDocs } from "../../../../../../.storybook/withViewDocs";
import {
ImageBodyView,
ImageBodyViewPlaceholder,
ImageBodyViewState,
type ImageBodyViewActions,
type ImageBodyViewSnapshot,
} from "./ImageBodyView";
const imageSrc = new URL("../../../../../../static/image-body/install-spinner.png", import.meta.url).href;
const thumbnailSrc = new URL("../../../../../../static/image-body/install-spinner.png", import.meta.url).href;
const animatedGifSrc = new URL("../../../../../../static/image-body/install-spinner.gif", import.meta.url).href;
const demoBlurhash = "LEHV6nWB2yk8pyo0adR*.7kCMdnj";
const imageBodyViewStateOptions = [ImageBodyViewState.ERROR, ImageBodyViewState.HIDDEN, ImageBodyViewState.READY];
const imageBodyViewPlaceholderOptions = [
ImageBodyViewPlaceholder.NONE,
ImageBodyViewPlaceholder.SPINNER,
ImageBodyViewPlaceholder.BLURHASH,
];
type ImageBodyViewProps = ImageBodyViewSnapshot &
ImageBodyViewActions & {
className?: string;
children?: ReactNode;
};
const ImageBodyViewWrapperImpl = ({
onLinkClick,
onHiddenButtonClick,
onImageLoad,
onImageError,
className,
children,
...snapshotProps
}: ImageBodyViewProps): ReactNode => {
const vm = useMockedViewModel(snapshotProps, {
onLinkClick: onLinkClick ?? fn(),
onHiddenButtonClick: onHiddenButtonClick ?? fn(),
onImageLoad: onImageLoad ?? fn(),
onImageError: onImageError ?? fn(),
});
return (
<ImageBodyView vm={vm} className={className}>
{children}
</ImageBodyView>
);
};
const ImageBodyViewWrapper = withViewDocs(ImageBodyViewWrapperImpl, ImageBodyView);
const meta = {
title: "Timeline/Timeline Body/ImageBodyView",
component: ImageBodyViewWrapper,
tags: ["autodocs"],
argTypes: {
state: {
options: imageBodyViewStateOptions,
control: { type: "select" },
},
placeholder: {
options: imageBodyViewPlaceholderOptions,
control: { type: "select" },
},
className: { control: "text" },
},
args: {
state: ImageBodyViewState.READY,
alt: "Element logo",
hiddenButtonLabel: "Show image",
errorLabel: "Unable to show image due to error",
src: imageSrc,
thumbnailSrc,
showAnimatedContentOnHover: false,
placeholder: ImageBodyViewPlaceholder.NONE,
blurhash: demoBlurhash,
maxWidth: 320,
maxHeight: 320,
aspectRatio: "1 / 1",
isSvg: false,
gifLabel: undefined,
bannerLabel: "install-spinner.png",
tooltipLabel: undefined,
linkUrl: imageSrc,
linkTarget: undefined,
className: undefined,
children: <div>File body slot</div>,
},
} satisfies Meta<typeof ImageBodyViewWrapper>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Default: Story = {};
export const Hidden: Story = {
args: {
state: ImageBodyViewState.HIDDEN,
linkUrl: undefined,
tooltipLabel: undefined,
},
};
export const LoadingWithSpinner: Story = {
args: {
placeholder: ImageBodyViewPlaceholder.SPINNER,
},
};
export const LoadingWithBlurhash: Story = {
args: {
placeholder: ImageBodyViewPlaceholder.BLURHASH,
},
};
export const AnimatedPreview: Story = {
args: {
src: animatedGifSrc,
thumbnailSrc,
linkUrl: animatedGifSrc,
showAnimatedContentOnHover: true,
gifLabel: "GIF",
},
};
export const ErrorState: Story = {
args: {
state: ImageBodyViewState.ERROR,
linkUrl: undefined,
children: undefined,
},
};
export const WithTooltip: Story = {
args: {
tooltipLabel: "Tooltip image name",
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.hover(canvas.getByRole("img", { name: "Element logo" }));
await expect(
within(canvasElement.ownerDocument.body).findByText("Tooltip image name"),
).resolves.toBeInTheDocument();
},
};