signal-desktop/ts/components/Avatar.stories.tsx

246 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { Meta, StoryFn } from '@storybook/react';
import * as React from 'react';
2022-07-22 00:44:35 +00:00
import { action } from '@storybook/addon-actions';
import { expect, jest } from '@storybook/jest';
2022-07-22 00:44:35 +00:00
import { isBoolean } from 'lodash';
2022-06-24 16:52:48 +00:00
import { within, userEvent } from '@storybook/testing-library';
2022-07-22 00:44:35 +00:00
import type { AvatarColorType } from '../types/Colors';
import type { Props } from './Avatar';
import enMessages from '../../_locales/en/messages.json';
2022-07-22 00:44:35 +00:00
import { Avatar, AvatarBlur, AvatarSize } from './Avatar';
import { AvatarColors } from '../types/Colors';
2022-07-22 00:44:35 +00:00
import { HasStories } from '../types/Stories';
import { ThemeType } from '../types/Util';
2022-07-22 00:44:35 +00:00
import { getFakeBadge } from '../test-both/helpers/getFakeBadge';
import { setupI18n } from '../util/setupI18n';
const i18n = setupI18n('en', enMessages);
2021-05-28 16:15:17 +00:00
const colorMap: Record<string, AvatarColorType> = AvatarColors.reduce(
(m, color) => ({
...m,
[color]: color,
}),
{}
);
const conversationTypeMap: Record<string, Props['conversationType']> = {
direct: 'direct',
group: 'group',
};
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/Avatar',
component: Avatar,
argTypes: {
badge: {
control: false,
},
blur: {
control: { type: 'radio' },
options: {
Undefined: undefined,
2022-06-07 00:48:02 +00:00
NoBlur: AvatarBlur.NoBlur,
BlurPicture: AvatarBlur.BlurPicture,
BlurPictureWithClickToView: AvatarBlur.BlurPictureWithClickToView,
},
},
color: {
options: colorMap,
},
conversationType: {
control: { type: 'radio' },
options: conversationTypeMap,
},
size: {
control: false,
},
storyRing: {
control: { type: 'radio' },
2022-07-22 00:44:35 +00:00
options: [undefined, ...Object.values(HasStories)],
2022-06-07 00:48:02 +00:00
},
theme: {
control: { type: 'radio' },
options: ThemeType,
},
},
args: {
blur: undefined,
color: AvatarColors[0],
onClick: action('onClick'),
theme: ThemeType.light,
},
} satisfies Meta<Props>;
2022-06-07 00:48:02 +00:00
const createProps = (overrideProps: Partial<Props> = {}): Props => ({
acceptedMessageRequest: isBoolean(overrideProps.acceptedMessageRequest)
? overrideProps.acceptedMessageRequest
: true,
2022-06-07 00:48:02 +00:00
avatarPath: overrideProps.avatarPath || '',
2021-11-02 23:01:13 +00:00
badge: overrideProps.badge,
blur: overrideProps.blur,
2022-06-07 00:48:02 +00:00
color: overrideProps.color || AvatarColors[0],
conversationType: overrideProps.conversationType || 'direct',
i18n,
2021-05-07 22:21:10 +00:00
isMe: false,
2022-06-07 00:48:02 +00:00
loading: Boolean(overrideProps.loading),
noteToSelf: Boolean(overrideProps.noteToSelf),
onClick: jest.fn(action('onClick')),
2021-11-18 20:01:53 +00:00
onClickBadge: action('onClickBadge'),
2022-06-07 00:48:02 +00:00
phoneNumber: overrideProps.phoneNumber || '',
searchResult: Boolean(overrideProps.searchResult),
2021-05-07 22:21:10 +00:00
sharedGroupNames: [],
size: 80,
title: overrideProps.title || '',
theme: overrideProps.theme || ThemeType.light,
storyRing: overrideProps.storyRing,
});
2022-06-07 00:48:02 +00:00
const sizes = Object.values(AvatarSize).filter(
x => typeof x === 'number'
) as Array<AvatarSize>;
2022-11-18 00:45:19 +00:00
// eslint-disable-next-line react/function-component-definition
const Template: StoryFn<Props> = (args: Props) => {
return (
<>
{sizes.map(size => (
<Avatar key={size} {...args} size={size} />
))}
</>
);
};
2022-11-18 00:45:19 +00:00
// eslint-disable-next-line react/function-component-definition
const TemplateSingle: StoryFn<Props> = (args: Props) => (
2022-12-09 20:37:45 +00:00
<Avatar {...args} size={AvatarSize.EIGHTY} />
2022-06-07 00:48:02 +00:00
);
2022-06-07 00:48:02 +00:00
export const Default = Template.bind({});
Default.args = createProps({
avatarPath: '/fixtures/giphy-GVNvOUpeYmI7e.gif',
2021-11-02 23:01:13 +00:00
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Default.play = async (context: any) => {
const { args, canvasElement } = context;
2022-06-24 16:52:48 +00:00
const canvas = within(canvasElement);
const [avatar] = canvas.getAllByRole('button');
await userEvent.click(avatar);
await expect(args.onClick).toHaveBeenCalled();
};
2021-11-02 23:01:13 +00:00
2022-06-07 00:48:02 +00:00
export const WithBadge = Template.bind({});
WithBadge.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
badge: getFakeBadge(),
2021-05-10 17:50:04 +00:00
});
2022-06-07 00:48:02 +00:00
export const WideImage = Template.bind({});
WideImage.args = createProps({
avatarPath: '/fixtures/wide.jpg',
});
2022-06-07 00:48:02 +00:00
export const OneWordName = Template.bind({});
OneWordName.args = createProps({
title: 'John',
});
2022-06-07 00:48:02 +00:00
export const TwoWordName = Template.bind({});
TwoWordName.args = createProps({
title: 'John Smith',
});
2022-06-07 00:48:02 +00:00
export const WideInitials = Template.bind({});
WideInitials.args = createProps({
title: 'Walter White',
});
2022-06-07 00:48:02 +00:00
export const ThreeWordName = Template.bind({});
ThreeWordName.args = createProps({
title: 'Walter H. White',
});
2022-06-07 00:48:02 +00:00
export const NoteToSelf = Template.bind({});
NoteToSelf.args = createProps({
noteToSelf: true,
});
2022-06-07 00:48:02 +00:00
export const ContactIcon = Template.bind({});
ContactIcon.args = createProps();
2022-06-07 00:48:02 +00:00
export const GroupIcon = Template.bind({});
GroupIcon.args = createProps({
conversationType: 'group',
});
2022-06-07 00:48:02 +00:00
export const SearchIcon = Template.bind({});
SearchIcon.args = createProps({
searchResult: true,
2021-11-12 01:17:29 +00:00
});
2022-11-18 00:45:19 +00:00
export function Colors(): JSX.Element {
const props = createProps();
2022-06-07 00:48:02 +00:00
return (
<>
{AvatarColors.map(color => (
<Avatar key={color} {...props} color={color} />
))}
</>
);
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
export const BrokenColor = Template.bind({});
BrokenColor.args = createProps({
color: 'nope' as AvatarColorType,
});
2022-06-07 00:48:02 +00:00
export const BrokenAvatar = Template.bind({});
BrokenAvatar.args = createProps({
avatarPath: 'badimage.png',
});
2022-06-07 00:48:02 +00:00
export const BrokenAvatarForGroup = Template.bind({});
BrokenAvatarForGroup.args = createProps({
avatarPath: 'badimage.png',
conversationType: 'group',
});
2022-06-07 00:48:02 +00:00
export const Loading = Template.bind({});
Loading.args = createProps({
loading: true,
});
2021-04-27 13:20:17 +00:00
2022-06-07 00:48:02 +00:00
export const BlurredBasedOnProps = TemplateSingle.bind({});
BlurredBasedOnProps.args = createProps({
acceptedMessageRequest: false,
avatarPath: '/fixtures/kitten-3-64-64.jpg',
});
2022-06-07 00:48:02 +00:00
export const ForceBlurred = TemplateSingle.bind({});
ForceBlurred.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
blur: AvatarBlur.BlurPicture,
2021-04-27 13:20:17 +00:00
});
2022-06-07 00:48:02 +00:00
export const BlurredWithClickToView = TemplateSingle.bind({});
BlurredWithClickToView.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
blur: AvatarBlur.BlurPictureWithClickToView,
});
2021-04-27 13:20:17 +00:00
2022-06-07 00:48:02 +00:00
export const StoryUnread = TemplateSingle.bind({});
StoryUnread.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
2022-07-22 00:44:35 +00:00
storyRing: HasStories.Unread,
2021-04-27 13:20:17 +00:00
});
2022-03-04 21:14:52 +00:00
2022-06-07 00:48:02 +00:00
export const StoryRead = TemplateSingle.bind({});
StoryRead.args = createProps({
avatarPath: '/fixtures/kitten-3-64-64.jpg',
2022-07-22 00:44:35 +00:00
storyRing: HasStories.Read,
2022-06-07 00:48:02 +00:00
});