Back to Repositories

Validating MediaError Construction and Handling in video.js

This test suite validates the MediaError class functionality in video.js, ensuring proper error handling and message construction for media playback issues. The tests verify various initialization methods and error code handling scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of MediaError object construction and handling.

Key areas tested include:
  • Error construction from numeric codes
  • String-based error message handling
  • Object-based error initialization
  • Native MediaError object compatibility
  • Redundant construction scenarios

Implementation Analysis

The testing approach uses QUnit’s module and test structure to validate MediaError functionality. Tests employ strict equality assertions to verify error codes and messages.

Implementation features:
  • Browser compatibility detection
  • Native MediaError object creation helper
  • Multiple construction patterns validation

Technical Details

Testing infrastructure includes:
  • QUnit as the testing framework
  • ESLint configuration for code quality
  • Global window object mocking
  • Object.create and Object.defineProperty utilization
  • Conditional test execution based on browser support

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and comprehensive error scenario coverage.

Notable practices:
  • Modular test organization
  • Browser capability detection
  • Clear test case naming
  • Consistent assertion patterns
  • Edge case handling for different input types

videojs/videoJs

test/unit/media-error.test.js

            
/* eslint-env qunit */
import window from 'global/window';
import MediaError from '../../src/js/media-error';

const isModernBrowser = window.MediaError && Object.create && Object.defineProperty;

/**
 * Creates a real native MediaError object.
 *
 * @param  {number} code
 * @param  {string} [message]
 * @return {MediaError}
 */
const createNativeMediaError = (code, message) => {
  const err = Object.create(window.MediaError);

  Object.defineProperty(err, 'code', {value: code});

  if (message) {
    err.message = message;
  }

  return err;
};

QUnit.module('MediaError');

QUnit.test('can be constructed from a number', function(assert) {
  const mediaError = new MediaError(1);

  assert.strictEqual(mediaError.code, 1);
  assert.strictEqual(mediaError.message, MediaError.defaultMessages['1']);
});

QUnit.test('can be constructed from a string', function(assert) {
  const mediaError = new MediaError('hello, world');

  assert.strictEqual(mediaError.code, 0);
  assert.strictEqual(mediaError.message, 'hello, world');
});

QUnit.test('can be constructed from an object', function(assert) {
  const mediaError = new MediaError({code: 2});
  const mediaErrorMsg = new MediaError({
    code: 2,
    message: 'hello, world',
    metadata: {
      errorType: 'test-error'
    }
  });

  assert.strictEqual(mediaError.code, 2);
  assert.strictEqual(mediaError.message, MediaError.defaultMessages['2']);
  assert.strictEqual(mediaErrorMsg.code, 2);
  assert.strictEqual(mediaErrorMsg.message, 'hello, world');
  assert.strictEqual(mediaErrorMsg.metadata.errorType, 'test-error');
});

if (isModernBrowser) {
  QUnit.test('can be constructed from a native MediaError object', function(assert) {
    const mediaError = new MediaError(createNativeMediaError(3));
    const mediaErrorMsg = new MediaError(createNativeMediaError(4, 'hello, world'));

    assert.strictEqual(mediaError.code, 3);
    assert.strictEqual(mediaError.message, MediaError.defaultMessages['3']);
    assert.strictEqual(mediaErrorMsg.code, 4);
    assert.strictEqual(mediaErrorMsg.message, 'hello, world');
  });
}

QUnit.test('can be constructed redundantly', function(assert) {
  const mediaError = new MediaError(2);
  const redundantMediaError = new MediaError(mediaError);

  assert.strictEqual(redundantMediaError, mediaError);
});