docs/_snippets/component-test-with-testing-library.md
import { render, screen, fireEvent } from '@testing-library/angular';
import { FormComponent } from './login-form.component';
import { InvalidForm } from './Form.stories'; //👈 Our stories imported here.
test('Checks if the form is valid ', async () => {
await render(FormComponent, {
componentProperties: InvalidForm.args,
});
fireEvent.click(screen.getByText('Submit'));
const isFormValid = screen.getByTestId('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import '@testing-library/jest-dom/extend-expect';
import { h } from 'preact';
import { render, fireEvent } from '@testing-library/preact';
import { InvalidForm } from './LoginForm.stories'; //👈 Our stories imported here.
it('Checks if the form is valid', async () => {
const { getByTestId, getByText } = render(<InvalidForm {...InvalidForm.args} />);
fireEvent.click(getByText('Submit'));
const isFormValid = getByTestId('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import { fireEvent, render, screen } from '@testing-library/react';
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import { composeStories } from '@storybook/your-framework';
import * as stories from './LoginForm.stories'; // 👈 Our stories imported here.
const { InvalidForm } = composeStories(stories);
test('Checks if the form is valid', async () => {
// Renders the composed story
await InvalidForm.run();
const buttonElement = screen.getByRole('button', {
name: 'Submit',
});
fireEvent.click(buttonElement);
const isFormValid = screen.getByLabelText('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import { fireEvent, render, screen } from '@testing-library/react';
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import { composeStories } from '@storybook/your-framework';
import * as stories from './LoginForm.stories'; // 👈 Our stories imported here.
const { InvalidForm } = composeStories(stories);
test('Checks if the form is valid', async () => {
// Renders the composed story
await InvalidForm.run();
const buttonElement = screen.getByRole('button', {
name: 'Submit',
});
fireEvent.click(buttonElement);
const isFormValid = screen.getByLabelText('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import { fireEvent, render, screen } from '@testing-library/svelte';
// Replace your-framework with the framework you are using, e.g. sveltekit or svelte-vite
import { composeStories } from '@storybook/your-framework';
import * as stories from './LoginForm.stories'; // 👈 Our stories imported here.
const { InvalidForm } = composeStories(stories);
it('Checks if the form is valid', async () => {
// Renders the composed story
await InvalidForm.run();
await fireEvent.click(screen.getByText('Submit'));
const isFormValid = screen.getByTestId('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import { fireEvent, render, screen } from '@testing-library/vue';
import { composeStories } from '@storybook/vue3-vite';
import * as stories from './LoginForm.stories'; // 👈 Our stories imported here.
const { InvalidForm } = composeStories(stories);
test('Checks if the form is valid', async () => {
// Renders the composed story
await InvalidForm.run();
const buttonElement = screen.getByRole('button', {
name: 'Submit',
});
fireEvent.click(buttonElement);
const isFormValid = screen.getByLabelText('invalid-form');
expect(isFormValid).toBeInTheDocument();
});
import { fireEvent, render, screen } from '@testing-library/vue';
import { composeStories } from '@storybook/vue3-vite';
import * as stories from './LoginForm.stories'; // 👈 Our stories imported here.
const { InvalidForm } = composeStories(stories);
test('Checks if the form is valid', async () => {
// Renders the composed story
await InvalidForm.run();
const buttonElement = screen.getByRole('button', {
name: 'Submit',
});
fireEvent.click(buttonElement);
const isFormValid = screen.getByLabelText('invalid-form');
expect(isFormValid).toBeInTheDocument();
});