0
0
Svelteframework~20 mins

Component testing with Testing Library in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Testing Library Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output after clicking the button?

Given this Svelte component test using Testing Library, what will be the text content of the button after the click event?

Svelte
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');
});
A"On"
B"Off"
C"Clicked"
D"Error: button not found"
Attempts:
2 left
💡 Hint

Think about what the component does when the button is clicked.

📝 Syntax
intermediate
1:30remaining
Which option correctly imports Testing Library for Svelte?

Choose the correct import statement to use Testing Library in a Svelte test file.

Aimport { render, fireEvent } from 'testing-library-svelte';
Bimport { render, fireEvent } from 'svelte-testing-library';
Cimport { render, fireEvent } from '@testing-library/svelte';
Dimport { render, fireEvent } from '@testing-library/react';
Attempts:
2 left
💡 Hint

Check the official package name for Testing Library with Svelte.

🔧 Debug
advanced
2:30remaining
Why does this test fail with 'Unable to find an element with the text: Submit'?

Examine the test code below. Why does the test fail with the error 'Unable to find an element with the text: Submit'?

Svelte
import { render } from '@testing-library/svelte';
import FormButton from './FormButton.svelte';

test('renders submit button', () => {
  const { getByText } = render(FormButton, { props: { label: 'Submit' } });
  getByText('Submit');
});
AThe getByText method is misspelled and causes an error.
BThe FormButton component does not use the 'label' prop to set button text.
CThe test forgot to await an async render call.
DThe component file path is incorrect, so it renders nothing.
Attempts:
2 left
💡 Hint

Check how the component uses props to render text.

state_output
advanced
2:00remaining
What is the final count displayed after these interactions?

Given this Svelte counter component test, what is the final number displayed after the clicks?

Svelte
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');
});
A1
B0
C2
D-1
Attempts:
2 left
💡 Hint

Count how many times the increment and decrement buttons are clicked.

🧠 Conceptual
expert
2:30remaining
Which Testing Library query is best for selecting a button by its accessible name?

In Testing Library for Svelte, which query method best selects a button element by its accessible name for accessibility compliance?

AgetByLabelText('Submit')
BgetByText('Submit')
CgetByTestId('submit-button')
DgetByRole('button', { name: 'Submit' })
Attempts:
2 left
💡 Hint

Consider ARIA roles and accessible names for best practice.