Skip to content

Commit

Permalink
fix: add test for the ability to change the placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Sep 18, 2024
1 parent dc386e7 commit de8a799
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/TextEditor/TextEditor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const meta = {
args: {
onChange: fn(),
},
render: (args) => <TextEditor {...args} />,
} satisfies Meta<typeof TextEditor>;
export default meta;
type Story = StoryObj<typeof meta>;
Expand Down
55 changes: 55 additions & 0 deletions src/TextEditor/TextEditorWrapper.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Meta, StoryObj } from '@storybook/react';
import { expect, fn, userEvent, within } from '@storybook/test';

import { useState } from 'react';

import TextEditor, { TextEditorProps } from './TextEditor.js';

const BUTTON_ID = 'button-id';
const NEW_PLACEHOLDER_TEXT = 'new placeholder text';
const TextEditorWrapper = (args: TextEditorProps): JSX.Element => {
const [placeholder, setPlaceholder] = useState(args.placeholderText);
return (
<>
<button
data-testid={BUTTON_ID}
onClick={() => setPlaceholder(NEW_PLACEHOLDER_TEXT)}
>
change text
</button>
<TextEditor {...args} placeholderText={placeholder} />
</>
);
};

const meta = {
title: 'Text/TextEditorWrapper',
component: TextEditorWrapper,
args: {
onChange: fn(),
id: 'editor-id',
},
} satisfies Meta<typeof TextEditor>;
export default meta;
type Story = StoryObj<typeof meta>;

export const Placeholder = {
args: {
placeholderText: 'Initial placeholder',
value: '',
},
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
await expect(
canvasElement
.querySelector(`[id=${args.id}] .ql-editor`)
?.getAttribute('data-placeholder'),
).toEqual(args.placeholderText);
await userEvent.click(canvas.getByTestId(BUTTON_ID));
await expect(
canvasElement
.querySelector(`[id=${args.id}] .ql-editor`)
?.getAttribute('data-placeholder'),
).toEqual(NEW_PLACEHOLDER_TEXT);
},
} satisfies Story;

0 comments on commit de8a799

Please sign in to comment.