liveBook · Manning

Test

As explained in Web Components in Action

Software testing is the systematic execution and evaluation of software to detect defects and verify expected behavior.

Testing helps establish confidence that software behaves as expected and remains maintainable. Unit tests exercise a piece of code and its edge cases; functional tests check behavior users expect. section 13.0

For a component, its API can be treated as a testable unit. Tests can check values set through attributes or JavaScript, and whether the visual state reflects those values. Re-running tests after changes can reveal regressions; coverage reports can highlight untested branches. section 13.1 section 13.2

Testing a slider’s initial value

A test can assert that a slider initialized with value="50" exposes the value 50, reflects it in its attribute, and positions its thumbnail accordingly. section 13.2

Listing 13.4. A single slider test
test('slider get initial value', function () {
   assert.equal(slider.value, 50);                                        #1
   assert.equal(                                                          #2
     slider.getAttribute('value'), 50);
   assert.equal(slider.root.querySelector('.thumb').style.left, sliderWidth *
     50/100 - thumbCenterOffset + 'px');                                  #3
});

See the complete initial-value test, including assertions for the slider’s API, attribute, and thumbnail position. section 13.2

Web Component Tester (WCT) runs browser-based tests with Mocha and Chai. DOM-dependent code may need a browser environment: JSDOM is described as lacking Web Components support. section 13.1 section 13.2

How Web Components in Action covers it

How deep it goes
Defines unit and functional testing, then works through component assertions and coverage; distinguishes browser testing from unsupported JSDOM use.
Examples
Uses RGB-to-hex edge cases and slider value, attribute, and thumbnail-position assertions.
What it assumes
Assumes familiarity with component APIs and DOM-backed behavior; distinguishes testing styles before developing specific tests.
What you can do after
Apply testing with WCT, Mocha, and Chai to check component APIs, reflected attributes, visual state, and edge cases.
How it connects
Connects unit testing with functional testing, test-driven development, code coverage, Web Components, and the DOM.

Also explained in

Compare how 53 books explain test →

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.