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

133 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-06-30 20:52:03 -04:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Meta, ReactRenderer, StoryFn } from '@storybook/react';
2022-06-30 20:52:03 -04:00
import type { PlayFunction } from '@storybook/csf';
import React from 'react';
2024-07-08 11:23:27 -07:00
import { expect, fn, within, userEvent } from '@storybook/test';
2022-06-30 20:52:03 -04:00
import { action } from '@storybook/addon-actions';
import type { PropsType } from './MyStoryButton';
import { MyStoryButton } from './MyStoryButton';
2025-06-26 12:24:07 -04:00
import { getDefaultConversation } from '../test-helpers/getDefaultConversation';
import { getFakeMyStory } from '../test-helpers/getFakeStory';
import { SendStatus } from '../messages/MessageSendState';
2022-11-16 17:10:11 -05:00
import { ResolvedSendStatus } from '../types/Stories';
2022-06-30 20:52:03 -04:00
2025-03-13 12:52:08 -07:00
const { i18n } = window.SignalContext;
2022-06-30 20:52:03 -04:00
export default {
title: 'Components/MyStoriesButton',
component: MyStoryButton,
args: {
i18n,
me: getDefaultConversation(),
myStories: [getFakeMyStory()],
2025-02-19 10:16:41 -08:00
onAddStory: fn(action('onAddStory')) as ReturnType<typeof action>,
onClick: fn(action('onClick')) as ReturnType<typeof action>,
queueStoryDownload: action('queueStoryDownload'),
showToast: action('showToast'),
2022-06-30 20:52:03 -04:00
},
} satisfies Meta<PropsType>;
2022-06-30 20:52:03 -04:00
2022-11-17 16:45:19 -08:00
// eslint-disable-next-line react/function-component-definition
const Template: StoryFn<PropsType> = args => <MyStoryButton {...args} />;
2022-06-30 20:52:03 -04:00
const interactionTest: PlayFunction<ReactRenderer, PropsType> = async ({
2022-06-30 20:52:03 -04:00
args,
canvasElement,
}) => {
const canvas = within(canvasElement);
const btnAddStory = canvas.getByLabelText('Add a story');
await userEvent.click(btnAddStory);
const textStory = canvas.getByText('Text story');
await userEvent.click(textStory);
await expect(args.onAddStory).toHaveBeenCalled();
if (args.myStories.length > 0) {
const btnStory = canvas.getByText('My Stories');
await userEvent.click(btnStory);
await expect(args.onClick).toHaveBeenCalled();
}
2022-06-30 20:52:03 -04:00
};
export const NoStory = Template.bind({});
NoStory.args = {
2022-11-16 17:10:11 -05:00
myStories: [],
2022-06-30 20:52:03 -04:00
};
2022-06-30 20:52:03 -04:00
NoStory.play = interactionTest;
export const OneStory = Template.bind({});
OneStory.args = {};
2022-06-30 20:52:03 -04:00
OneStory.play = interactionTest;
export const ManyStories = Template.bind({});
ManyStories.args = {
2022-11-16 17:10:11 -05:00
myStories: [getFakeMyStory(), getFakeMyStory()],
2022-06-30 20:52:03 -04:00
};
2022-06-30 20:52:03 -04:00
ManyStories.play = interactionTest;
export const SendingStory = Template.bind({});
2022-11-16 17:10:11 -05:00
{
const myStory = getFakeMyStory();
SendingStory.args = {
myStories: [
{
2022-11-16 17:10:11 -05:00
...myStory,
reducedSendStatus: ResolvedSendStatus.Sending,
stories: myStory.stories.map((story, index) => {
if (index === 0) {
return {
...story,
sendState: [
{
status: SendStatus.Pending,
recipient: getDefaultConversation(),
},
],
};
}
return story;
}),
},
2022-11-16 17:10:11 -05:00
getFakeMyStory(),
],
2022-11-16 17:10:11 -05:00
};
}
SendingStory.play = interactionTest;
export const FailedSendStory = Template.bind({});
2022-11-16 17:10:11 -05:00
{
const myStory = getFakeMyStory();
FailedSendStory.args = {
myStories: [
{
2022-11-16 17:10:11 -05:00
...myStory,
reducedSendStatus: ResolvedSendStatus.Failed,
stories: myStory.stories.map((story, index) => {
if (index === 0) {
return {
...story,
sendState: [
{
status: SendStatus.Failed,
recipient: getDefaultConversation(),
},
],
};
}
return story;
}),
},
2022-11-16 17:10:11 -05:00
getFakeMyStory(),
],
2022-11-16 17:10:11 -05:00
};
}
FailedSendStory.play = interactionTest;