K
K
kirillleogky2020-01-02 12:44:29
JavaScript
kirillleogky, 2020-01-02 12:44:29

How to check DOM element with jest?

there is a function that adds or removes a class from an element:

export default function activeButtonClick(button) {
  if (button.classList.contains('active')) {
    button.classList.remove('active');
  } else {
    button.classList.add('active');
  }
}
module.exports = activeButtonClick;

What is the correct way to write a test that checks that the input DOM element now has the 'active' class?
my sketch:
const activeBtn = require('./activeBtnClick');
const pencil = document.getElementById('pencil');

describe("activeBtnClick", () => {
  it("adds class to the button", () => {
    ??
  });

});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2020-01-02
@kirillleogky

const activeBtn = require('./activeBtnClick');
const pencil = document.getElementById('pencil');

describe("activeBtnClick", () => {
  it("adds class to the button", () => {
     except(button.classList.contains('active')).toEqual(true);
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question