Given this Svelte component test using Testing Library, what will be the text content of the button after the click event?
import { render, fireEvent } from '@testing-library/svelte'; import ToggleButton from './ToggleButton.svelte'; test('button toggles text on click', async () => { const { getByText } = render(ToggleButton); const button = getByText('Off'); await fireEvent.click(button); expect(button.textContent).toBe('On'); });
Think about what the component does when the button is clicked.
The button initially shows "Off". After clicking, the component toggles the text to "On".
Choose the correct import statement to use Testing Library in a Svelte test file.
Check the official package name for Testing Library with Svelte.
The official Testing Library package for Svelte is '@testing-library/svelte'. Other options are incorrect or for different frameworks.
Examine the test code below. Why does the test fail with the error 'Unable to find an element with the text: Submit'?
import { render } from '@testing-library/svelte'; import FormButton from './FormButton.svelte'; test('renders submit button', () => { const { getByText } = render(FormButton, { props: { label: 'Submit' } }); getByText('Submit'); });
Check how the component uses props to render text.
If the component does not use the 'label' prop to set the button text, the text 'Submit' won't appear, causing getByText to fail.
Given this Svelte counter component test, what is the final number displayed after the clicks?
import { render, fireEvent } from '@testing-library/svelte'; import Counter from './Counter.svelte'; test('increments and decrements counter', async () => { const { getByText } = render(Counter); const incrementBtn = getByText('+'); const decrementBtn = getByText('-'); const countDisplay = getByText('0'); await fireEvent.click(incrementBtn); await fireEvent.click(incrementBtn); await fireEvent.click(decrementBtn); expect(countDisplay.textContent).toBe('1'); });
Count how many times the increment and decrement buttons are clicked.
Two increments add 2, one decrement subtracts 1, so final count is 1.
In Testing Library for Svelte, which query method best selects a button element by its accessible name for accessibility compliance?
Consider ARIA roles and accessible names for best practice.
Using getByRole with the 'button' role and accessible name ensures the element is a button and matches the visible accessible accessible label, improving accessibility testing.