concept test in category svelte

appears as: test, tests, tests, test
Svelte and Sapper in Action

This is an excerpt from Manning's book Svelte and Sapper in Action.

The current database starts as "test", even if no such database exist yet.

Listing 12.9 Jest tests for Category component in Category.spec.js

import {cleanup, fireEvent, render, waitFor} from '@testing-library/svelte';

import Category from './Category.svelte';

describe('Category', () => {
  let itemCount = 0;                                           #1

  const category = {id: 1, name: 'Clothes', items: {}};
  const categories = [category];
  const dnd = {};                                              #2
  const props = {categories, category, dnd, show: 'all'};

  beforeEach(() => {
    category.items = {
      1: {id: 1, name: 'socks', packed: true},
      2: {id: 2, name: 'shoes', packed: false}
    };
    itemCount = Object.keys(category.items).length;
  });

  afterEach(cleanup);

  test('should match snapshot', () => {
    const {container} = render(Category, props);
    expect(container).toMatchSnapshot();
  });

  function expectItemCount(count) {
    return waitFor(() => {
      const lis = document.querySelectorAll('li');             #3
      expect(lis.length).toBe(count);
    });
  }

  test('should render', async () => {
    const {getByText} = render(Category, props);
    expect(getByText('Clothes'));
    expect(getByText('1 of 2 remaining'));
    expect(getByText('New Item'));
    expect(getByText('Add Item'));
    await expectItemCount(itemCount);
  });

  test('should add an item', async () => {
    const {getByTestId, getByText} = render(Category, props);

    const input = getByTestId('item-input');
    const value = 't-shirts';
    fireEvent.input(input, {target: {value}});
    fireEvent.click(getByText('Add Item'));

    await expectItemCount(itemCount + 1);
    expect(getByText(value));
  });

  test('should delete an item', async () => {
    const {getAllByTestId} = render(Category, props);
 
    const deleteBtns = getAllByTestId('delete');
    fireEvent.click(deleteBtns[0]); // deletes first item
    await expectItemCount(itemCount - 1);
  });
 
  test('should toggle an item', async () => {
    const {container, getByText} = render(Category, props);
 
    const checkboxes = container.querySelectorAll('input[type="checkbox"]');
    expect(checkboxes.length).toBe(2);
 
    const [shoesCheckbox, socksCheckbox] = checkboxes;         #4
 
    expect(socksCheckbox.nextElementSibling.textContent).toBe('socks');
    await fireEvent.click(socksCheckbox);
    expect(getByText('2 of 2 remaining'));                     #5

    expect(shoesCheckbox.nextElementSibling.textContent).toBe('shoes');
    await fireEvent.click(shoesCheckbox);
    expect(getByText('1 of 2 remaining'));                     #6
  });
});

When the Jest tests for the Travel Packing app are run, DialogPolyfill, which is used in the custom Dialog component, will not be defined. To account for this, modify the call to onMount in src/Dialog.svelte to match the following:

DListing 12.10 ialog component onMount in src/Dialog.svelte

onMount(() => {
  if (dialogPolyfill) dialogPolyfill.registerDialog(dialog);
});

You might be tempted to wrap an if around the call to onMount instead of placing the if inside the function passed to onMount. However, that would not be correct because lifecycle functions cannot be called conditionally.

To temporarily skip running certain test suites, change their describe function to describe.skip. To temporarily skip running certain tests within a suite, change their test function to test.skip. To temporarily run only certain tests in a suite, change their test function to test.only.