Back to Repositories

Validating AudioTrack Component Implementation in video.js

This test suite validates the AudioTrack functionality in video.js, focusing on track properties, initialization defaults, and event handling. The tests ensure proper behavior of audio track properties and event triggering in the video player implementation.

Test Coverage Overview

The test suite provides comprehensive coverage of AudioTrack functionality including:

  • Track property initialization and validation
  • Default value handling for unspecified properties
  • Kind property validation against AudioTrackKind enumeration
  • Enabled state management and validation
  • Event triggering for enabled state changes

Implementation Analysis

The testing approach uses QUnit framework to systematically verify AudioTrack behavior. Tests employ baseline track testing patterns and specific test cases for property validation, utilizing modular test organization with clear separation of concerns.

The implementation leverages QUnit’s assertion methods and event handling verification to ensure robust audio track functionality.

Technical Details

Testing infrastructure includes:

  • QUnit test framework
  • ESLint for code quality
  • TrackBaseline utility for common track testing
  • AudioTrackKind enumeration validation
  • Event listener testing mechanisms

Best Practices Demonstrated

The test suite exemplifies testing best practices through:

  • Comprehensive property validation
  • Edge case handling for invalid inputs
  • Event handling verification
  • Modular test organization
  • Clear test case isolation
  • Consistent assertion patterns

videojs/videoJs

test/unit/tracks/audio-track.test.js

            
/* eslint-env qunit */
import AudioTrack from '../../../src/js/tracks/audio-track.js';
import {AudioTrackKind} from '../../../src/js/tracks/track-enums.js';
import TrackBaseline from './track-baseline';

QUnit.module('Audio Track');

// do baseline track testing
TrackBaseline(AudioTrack, {
  id: '1',
  language: 'en',
  label: 'English',
  kind: 'main'
});

QUnit.test('can create an enabled property on an AudioTrack', function(assert) {
  const enabled = true;
  const track = new AudioTrack({
    enabled
  });

  assert.equal(track.enabled, enabled, 'enabled value matches what we passed in');
});

QUnit.test('defaults when items not provided', function(assert) {
  const track = new AudioTrack();

  assert.equal(track.kind, '', 'kind defaulted to empty string');
  assert.equal(track.enabled, false, 'enabled defaulted to true since there is one track');
  assert.equal(track.label, '', 'label defaults to empty string');
  assert.equal(track.language, '', 'language defaults to empty string');
  assert.ok(track.id.match(/vjs_track_\d+/), 'id defaults to vjs_track_GUID');
});

QUnit.test('kind can only be one of several options, defaults to empty string', function(assert) {
  const track1 = new AudioTrack({
    kind: 'foo'
  });

  assert.equal(track1.kind, '', 'the kind is set to empty string, not foo');
  assert.notEqual(track1.kind, 'foo', 'the kind is set to empty string, not foo');

  // loop through all possible kinds to verify
  for (const key in AudioTrackKind) {
    const currentKind = AudioTrackKind[key];
    const track = new AudioTrack({
      kind: currentKind
    });

    assert.equal(track.kind, currentKind, 'the kind is set to ' + currentKind);
  }
});

QUnit.test('enabled can only be instantiated to true or false, defaults to false', function(assert) {
  let track = new AudioTrack({
    enabled: 'foo'
  });

  assert.equal(track.enabled, false, 'the enabled value is set to false, not foo');
  assert.notEqual(track.enabled, 'foo', 'the enabled value is not set to foo');

  track = new AudioTrack({
    enabled: true
  });

  assert.equal(track.enabled, true, 'the enabled value is set to true');

  track = new AudioTrack({
    enabled: false
  });

  assert.equal(track.enabled, false, 'the enabled value is set to false');
});

QUnit.test('enabled can only be changed to true or false', function(assert) {
  const track = new AudioTrack();

  track.enabled = 'foo';
  assert.notEqual(track.enabled, 'foo', 'enabled not set to invalid value, foo');
  assert.equal(track.enabled, false, 'enabled remains on the old value, false');

  track.enabled = true;
  assert.equal(track.enabled, true, 'enabled was set to true');

  track.enabled = 'baz';
  assert.notEqual(track.enabled, 'baz', 'enabled not set to invalid value, baz');
  assert.equal(track.enabled, true, 'enabled remains on the old value, true');

  track.enabled = false;
  assert.equal(track.enabled, false, 'enabled was set to false');
});

QUnit.test('when enabled is changed enabledchange event is fired', function(assert) {
  const track = new AudioTrack({
    tech: this.tech,
    enabled: false
  });
  let eventsTriggered = 0;

  track.addEventListener('enabledchange', () => {
    eventsTriggered++;
  });

  // two events
  track.enabled = true;
  track.enabled = false;
  assert.equal(eventsTriggered, 2, 'two enabled changes');

  // no event here
  track.enabled = false;
  track.enabled = false;
  assert.equal(eventsTriggered, 2, 'still two enabled changes');

  // one event
  track.enabled = true;
  assert.equal(eventsTriggered, 3, 'three enabled changes');

  // no events
  track.enabled = true;
  track.enabled = true;
  assert.equal(eventsTriggered, 3, 'still three enabled changes');

  track.off();
});