liveBook · Manning

Component testing

As explained in Web Components in Action

Component testing is the practice of verifying a reusable interface component's behavior, rendering, and public API in isolation.

Component testing checks a reusable interface component through its public API, rendered output, and visible DOM behavior. Web Component Tester (WCT) combines browser automation, Mocha test organization, and Chai assertions so tests run in real browsers and report results in the terminal. section 13.2

A practical component test can verify:

  • Initial state, such as a slider's value, corresponding attribute, and thumb position. section 13.2
  • Public-API changes, such as assigning a value through JavaScript and checking the resulting DOM. section 13.2
  • Boundary behavior, such as clamping values above 100 to 100 and values below 0 to 0. section 13.2

Testing a slider

Place the component in an HTML test fixture, load its module, then organize assertions in a Mocha suite. For example: assert.equal(slider.value, 50); assert.equal(slider.getAttribute('value'), 50); assert.equal(slider.root.querySelector('.thumb').style.left, expectedPosition). Add boundary tests to expose missing min/max behavior. section 13.2

Listing 13.2. WCT test file setup
<html>
<head>
  <script src="../../../../node_modules/web-component-tester/browser.js">
  </script>                                                               #1
  <script                                                                 #2
       type="module"
       src="../src/slider.js">
  </script>
   <style>
       wcia-slider {
           width: 500px;                                                  #3
       }
   </style>
</head>
<body>
<wcia-slider value="50"></wcia-slider>                                    #4
<script>
// tests go here                                                          #5
</script>
</body>
</html>

Shows the HTML fixture, component import, styling, and test insertion point used to start WCT tests. section 13.2

How Web Components in Action covers it

How deep it goes
Builds fixtures, suites, assertions, and boundary tests without descending into browser automation internals.
Examples
Uses a slider fixture to test initial values, JavaScript setters, rendered thumb position, and min/max clamping.
What it assumes
Assumes an HTML fixture, a component module, and familiarity with JavaScript assertions and DOM access.
What you can do after
Apply it via Web Component Tester, Mocha, and Chai to run component tests in automated browsers.
How it connects
Connects Web Components with browser automation, Selenium, Mocha, Chai, assertions, and test-driven development.

Also explained in

Compare how 3 books explain component testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.