Skip to content
the poor coder

Jest DOM Manipulation

Poor Man
Published
Reading time
2 min read
On this page

Jest DOM Manipulation#

If you're a developer, you might have come across the term "DOM Manipulation". In simple words, it is the process of changing the HTML document structure using JavaScript. Jest is a popular testing framework that can be used to test DOM manipulation functions.

Method 1: Using Jest's built-in functions#

Jest provides built-in functions that can be used to test DOM manipulation functions. These functions include:

  • .toBeInTheDocument() - This function checks if an element is present in the document.
  • .toHaveTextContent() - This function checks if an element contains a specific text.
  • .toHaveAttribute() - This function checks if an element has a specific attribute.
  • .toHaveClass() - This function checks if an element has a specific class.

Here's an example of how to use these functions:

Text

  import { getByTestId } from '@testing-library/dom';

  test('test DOM manipulation function', () => {
    const container = document.createElement('div');
    container.innerHTML = '<input data-testid="input-element" type="text" />';
    const input = getByTestId(container, 'input-element');

    expect(input).toBeInTheDocument();
    expect(input).toHaveAttribute('type', 'text');
    expect(input).not.toHaveClass('some-class');
  });

Method 2: Using Vanilla JS#

You can also test DOM manipulation functions using Vanilla JS. Here's an example:

Text

  test('test DOM manipulation function', () => {
    const container = document.createElement('div');
    container.innerHTML = '<input data-testid="input-element" type="text" />';
    const input = container.querySelector('[data-testid="input-element"]');

    expect(input).toBeTruthy();
    expect(input.getAttribute('type')).toBe('text');
    expect(input.classList.contains('some-class')).toBeFalsy();
  });

In this example, we used Vanilla JS to create a container and add an input element to it. We then used the querySelector() method to select the input element and test its attributes using Vanilla JS.

Both of these methods are valid ways to test DOM manipulation functions with Jest. It's up to you to decide which one works better for your specific use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC