From 151549c8a88ad038d6c0198add12d0ffe2b907a0 Mon Sep 17 00:00:00 2001 From: roble Date: Thu, 26 Nov 2026 21:00:52 +0000 Subject: [PATCH] feat: stories for VoteButton and ItemCard Co-Authored-By: Claude Opus 5.5 --- .../js/react/components/ItemCard.stories.tsx | 107 ++++++++++++++++++ resources/js/react/components/ItemCard.tsx | 5 +- .../react/components/VoteButton.stories.tsx | 49 ++++++++ resources/js/react/components/VoteButton.tsx | 5 +- .../js/vue/components/ItemCard.stories.ts | 101 +++++++++++++++++ .../js/vue/components/VoteButton.stories.ts | 49 ++++++++ 6 files changed, 312 insertions(+), 4 deletions(-) create mode 100644 resources/js/react/components/ItemCard.stories.tsx create mode 100644 resources/js/react/components/VoteButton.stories.tsx create mode 100644 resources/js/vue/components/ItemCard.stories.ts create mode 100644 resources/js/vue/components/VoteButton.stories.ts diff --git a/resources/js/react/components/ItemCard.stories.tsx b/resources/js/react/components/ItemCard.stories.tsx new file mode 100644 index 0000000..f8c8e22 --- /dev/null +++ b/resources/js/react/components/ItemCard.stories.tsx @@ -0,0 +1,107 @@ +import preview from '@/.storybook/preview'; +import { expect, fn } from 'storybook/test'; +import ItemCard from './ItemCard'; + +/** + * An item on the roadmap's board: its vote, title, description, type, and comment count. + * The title opens the item in a slideover. `vote` asks whoever shows it to count a vote. + */ +const meta = preview.meta({ + title: 'Roadmap/ItemCard', + component: ItemCard, + args: { + item: { + id: 7, + title: 'Dark mode for the dashboard', + slug: 'dark-mode-for-the-dashboard', + url: '/roadmap/dark-mode-for-the-dashboard', + description: + 'Follow the system setting, with a switch in the user menu to override it.', + status: 'planned', + status_label: 'Planned', + type: 'feature', + type_label: 'Feature', + votes_count: 42, + comments_count: 5, + has_voted: false, + votable: true, + created_at: '2026-11-02T09:00:00Z', + }, + typeVariant: 'default', + commentsEnabled: true, + showStatus: false, + votePending: false, + }, + decorators: [ + (Story) => ( +
+ +
+ ), + ], +}); +export default meta; + +/** On the board, in its status's column. Its vote button asks for a vote. */ +export const Default = meta.story({ + // The spy here only: in the meta's args it would leave every snippet incomplete. + args: { onVote: fn() }, + play: async ({ args, canvas, userEvent }) => { + await expect( + canvas.getByRole('link', { name: 'Dark mode for the dashboard' }), + ).toBeVisible(); + await expect( + canvas.getByRole('link', { name: 'Comments' }), + ).toHaveTextContent('5'); + + await userEvent.click(canvas.getByRole('button', { name: 'Upvote' })); + + await expect(args.onVote).toHaveBeenCalled(); + }, +}); + +/** In the visitor's own submissions, off the board: it says its status. */ +export const WithStatus = meta.story({ + args: { showStatus: true }, + play: async ({ canvas }) => { + await expect(canvas.getByText('Planned')).toBeVisible(); + }, +}); + +/** Comments turned off: no count. */ +export const WithoutComments = meta.story({ + args: { commentsEnabled: false }, + play: async ({ canvas }) => { + await expect( + canvas.queryByRole('link', { name: 'Comments' }), + ).toBeNull(); + }, +}); + +/** A bug the visitor voted for, with no description. */ +export const VotedBug = meta.story({ + args: { + item: { + id: 8, + title: 'Invoices show the wrong currency', + slug: 'invoices-wrong-currency', + url: '/roadmap/invoices-wrong-currency', + description: null, + status: 'in_progress', + status_label: 'In progress', + type: 'bug', + type_label: 'Bug', + votes_count: 12, + comments_count: 0, + has_voted: true, + votable: true, + created_at: '2026-11-18T09:00:00Z', + }, + typeVariant: 'destructive', + }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toHaveAttribute('aria-pressed', 'true'); + }, +}); diff --git a/resources/js/react/components/ItemCard.tsx b/resources/js/react/components/ItemCard.tsx index 50596e2..f264327 100644 --- a/resources/js/react/components/ItemCard.tsx +++ b/resources/js/react/components/ItemCard.tsx @@ -21,7 +21,8 @@ export default function ItemCard({ /** Own submissions are listed off the board, where the column no longer says the status. */ showStatus?: boolean; votePending?: boolean; - onVote: (item: RoadmapItem) => void; + /** Asks for a vote on the item; whoever shows the card counts it. */ + onVote?: (item: RoadmapItem) => void; }) { const t = useT(); @@ -35,7 +36,7 @@ export default function ItemCard({ votes={item.votes_count} voted={item.has_voted} pending={votePending || !item.votable} - onVote={() => onVote(item)} + onVote={() => onVote?.(item)} />
diff --git a/resources/js/react/components/VoteButton.stories.tsx b/resources/js/react/components/VoteButton.stories.tsx new file mode 100644 index 0000000..f8532fc --- /dev/null +++ b/resources/js/react/components/VoteButton.stories.tsx @@ -0,0 +1,49 @@ +import preview from '@/.storybook/preview'; +import { expect, fn } from 'storybook/test'; +import VoteButton from './VoteButton'; + +/** + * An item's upvote and its count. Filled once the visitor has voted. Whoever shows it + * counts the vote, on `vote`; `pending` disables it meanwhile, or for an item that takes + * no votes. + */ +const meta = preview.meta({ + title: 'Roadmap/VoteButton', + component: VoteButton, + args: { itemId: 1, votes: 42, voted: false, pending: false }, +}); +export default meta; + +/** Not voted yet. A click asks for the vote. */ +export const Default = meta.story({ + // The spy here only: in the meta's args it would leave every snippet incomplete. + args: { onVote: fn() }, + play: async ({ args, canvas, userEvent }) => { + const button = canvas.getByRole('button', { name: 'Upvote' }); + await expect(button).toHaveAttribute('aria-pressed', 'false'); + + await userEvent.click(button); + + await expect(args.onVote).toHaveBeenCalled(); + }, +}); + +/** The visitor's vote counts: filled. */ +export const Voted = meta.story({ + args: { voted: true, votes: 43 }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toHaveAttribute('aria-pressed', 'true'); + }, +}); + +/** A vote on its way, or an item that takes none: disabled. */ +export const Pending = meta.story({ + args: { pending: true }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toBeDisabled(); + }, +}); diff --git a/resources/js/react/components/VoteButton.tsx b/resources/js/react/components/VoteButton.tsx index 93fab3b..b416d94 100644 --- a/resources/js/react/components/VoteButton.tsx +++ b/resources/js/react/components/VoteButton.tsx @@ -14,7 +14,8 @@ export default function VoteButton({ votes: number; voted: boolean; pending?: boolean; - onVote: () => void; + /** Asks for the vote; whoever shows the button counts it. */ + onVote?: () => void; }) { const t = useT(); @@ -34,7 +35,7 @@ export default function VoteButton({ )} onClick={(event) => { event.preventDefault(); - onVote(); + onVote?.(); }} > diff --git a/resources/js/vue/components/ItemCard.stories.ts b/resources/js/vue/components/ItemCard.stories.ts new file mode 100644 index 0000000..dfab53d --- /dev/null +++ b/resources/js/vue/components/ItemCard.stories.ts @@ -0,0 +1,101 @@ +import preview from '@/.storybook/preview'; +import { expect, fn } from 'storybook/test'; +import ItemCard from './ItemCard.vue'; + +/** + * An item on the roadmap's board: its vote, title, description, type, and comment count. + * The title opens the item in a slideover. `vote` asks whoever shows it to count a vote. + */ +const meta = preview.meta({ + title: 'Roadmap/ItemCard', + component: ItemCard, + args: { + item: { + id: 7, + title: 'Dark mode for the dashboard', + slug: 'dark-mode-for-the-dashboard', + url: '/roadmap/dark-mode-for-the-dashboard', + description: + 'Follow the system setting, with a switch in the user menu to override it.', + status: 'planned', + status_label: 'Planned', + type: 'feature', + type_label: 'Feature', + votes_count: 42, + comments_count: 5, + has_voted: false, + votable: true, + created_at: '2026-11-02T09:00:00Z', + }, + typeVariant: 'default', + commentsEnabled: true, + showStatus: false, + votePending: false, + }, + decorators: [() => ({ template: '
' })], +}); +export default meta; + +/** On the board, in its status's column. Its vote button asks for a vote. */ +export const Default = meta.story({ + // The spy here only: in the meta's args it would leave every snippet incomplete. + args: { onVote: fn() }, + play: async ({ args, canvas, userEvent }) => { + await expect( + canvas.getByRole('link', { name: 'Dark mode for the dashboard' }), + ).toBeVisible(); + await expect( + canvas.getByRole('link', { name: 'Comments' }), + ).toHaveTextContent('5'); + + await userEvent.click(canvas.getByRole('button', { name: 'Upvote' })); + + await expect(args.onVote).toHaveBeenCalled(); + }, +}); + +/** In the visitor's own submissions, off the board: it says its status. */ +export const WithStatus = meta.story({ + args: { showStatus: true }, + play: async ({ canvas }) => { + await expect(canvas.getByText('Planned')).toBeVisible(); + }, +}); + +/** Comments turned off: no count. */ +export const WithoutComments = meta.story({ + args: { commentsEnabled: false }, + play: async ({ canvas }) => { + await expect( + canvas.queryByRole('link', { name: 'Comments' }), + ).toBeNull(); + }, +}); + +/** A bug the visitor voted for, with no description. */ +export const VotedBug = meta.story({ + args: { + item: { + id: 8, + title: 'Invoices show the wrong currency', + slug: 'invoices-wrong-currency', + url: '/roadmap/invoices-wrong-currency', + description: null, + status: 'in_progress', + status_label: 'In progress', + type: 'bug', + type_label: 'Bug', + votes_count: 12, + comments_count: 0, + has_voted: true, + votable: true, + created_at: '2026-11-18T09:00:00Z', + }, + typeVariant: 'destructive', + }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toHaveAttribute('aria-pressed', 'true'); + }, +}); diff --git a/resources/js/vue/components/VoteButton.stories.ts b/resources/js/vue/components/VoteButton.stories.ts new file mode 100644 index 0000000..30dbc92 --- /dev/null +++ b/resources/js/vue/components/VoteButton.stories.ts @@ -0,0 +1,49 @@ +import preview from '@/.storybook/preview'; +import { expect, fn } from 'storybook/test'; +import VoteButton from './VoteButton.vue'; + +/** + * An item's upvote and its count. Filled once the visitor has voted. Whoever shows it + * counts the vote, on `vote`; `pending` disables it meanwhile, or for an item that takes + * no votes. + */ +const meta = preview.meta({ + title: 'Roadmap/VoteButton', + component: VoteButton, + args: { itemId: 1, votes: 42, voted: false, pending: false }, +}); +export default meta; + +/** Not voted yet. A click asks for the vote. */ +export const Default = meta.story({ + // The spy here only: in the meta's args it would leave every snippet incomplete. + args: { onVote: fn() }, + play: async ({ args, canvas, userEvent }) => { + const button = canvas.getByRole('button', { name: 'Upvote' }); + await expect(button).toHaveAttribute('aria-pressed', 'false'); + + await userEvent.click(button); + + await expect(args.onVote).toHaveBeenCalled(); + }, +}); + +/** The visitor's vote counts: filled. */ +export const Voted = meta.story({ + args: { voted: true, votes: 43 }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toHaveAttribute('aria-pressed', 'true'); + }, +}); + +/** A vote on its way, or an item that takes none: disabled. */ +export const Pending = meta.story({ + args: { pending: true }, + play: async ({ canvas }) => { + await expect( + canvas.getByRole('button', { name: 'Upvote' }), + ).toBeDisabled(); + }, +});