react jest and enzyme, find 'option' with hidden attribute

Jeppe Christensen

I'm testing a combobox, which has a placeholder value like so:

<option key='blankChoice' hidden value>{blankChoice}</option>

I want to run a test, that checks if the blank choice is actually appended to my combobox, therefore I did the following:

beforeEach(() => {
    wrapper = mount(<ComboBox options={options} />)
});

afterEach(() => {
    wrapper.unmount();
})
test('Combobox has 1 hidden options', () => {
    const elem = wrapper.find('option')
    expect(elem).toHaveAttribute("hidden").toBe(1)
})

The above returns an error to me:

received value must be an HTMLElement or an SVGElement.

I sort of understand this error, but I'm in doubt if my general approach is correct at all. Therefore, how do I test if my comobobx has 1 hidden <option>

slideshowp2

wrapper.find() returns ReactWrapper which is an object. You need to use .getDOMNode() => DOMComponent to return the outer most DOMComponent of the current wrapper.

E.g.

ComboBox.tsx:

import React from 'react';

export default function ComboBox() {
  return (
    <div>
      <select>
        <option key="blankChoice" hidden></option>
      </select>
    </div>
  );
}

ComboBox.test.tsx:

import { mount } from 'enzyme';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import ComboBox from './ComboBox';

describe('68393118', () => {
  let wrapper;
  beforeEach(() => {
    wrapper = mount(<ComboBox />);
  });

  afterEach(() => {
    wrapper.unmount();
  });
  test('Combobox has 1 hidden options', () => {
    const elem = wrapper.find('option');
    expect(elem.getDOMNode()).toHaveAttribute('hidden');
  });
});

test result:

 PASS  examples/68393118/ComboBox.test.tsx (10.678 s)
  68393118
    ✓ Combobox has 1 hidden options (53 ms)

--------------|---------|----------|---------|---------|-------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------|---------|----------|---------|---------|-------------------
All files     |     100 |      100 |     100 |     100 |                   
 ComboBox.tsx |     100 |      100 |     100 |     100 |                   
--------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        11.539 s

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error shallow Jest Enzyme React cannot find "store"

Cannot read properties of undefined (reading 'find') in Jest and Enzyme React 17

How to find count of a div containing specific value in React, Jest and Enzyme

React jest test with enzyme error

Testing React Router with Jest and Enzyme

Test a form in React with jest and enzyme

How to solve an Enzyme/Jest error "Cannot find module 'react' from 'react-router.js'" in a React project

How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?

How to assert on Element 'Array attribute' in Jest/Enzyme?

Setting up Jest and Enzyme to test React 15 cannot find module react/lib/ReactTestUtils

Jest/Enzyme shallow testing React stateless component - wrapper.find() not working

Jest/Enzyme - TypeError: div.find is not a function

Find component element by name in jest enzyme

React Native + Jest + Enzyme: Why does Enzyme not recognize some components?

React / Enzyme: Invariant Violation error when running Jest / Enzyme test

React/Jest/Enzyme - await not waiting long enough

Globally mock a function in Jest/Enzyme in React

testing custom react methods with jest and enzyme

stub fetch unit test react with enzyme and jest

React testing state of component with Jest and Enzyme

React jest enzyme function called test

How to mock React component methods with jest and enzyme

Jest,Enzyme,React - Testing Iframe OnLoad

Testing react-router Redirects with Jest and Enzyme

Mock a custom service with jest and enzyme for a react component

React - Jest/Enzyme changed trigger function

React Jest, Enzyme remove or hide Received part

React - Jest - Enzyme: How to mock ref properties

Jest & Enzyme test error with React Context API

TOP Ranking

HotTag

Archive