CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jasmine-jquery

jQuery matchers and fixture loader for Jasmine framework

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

html-fixtures.mddocs/

HTML Fixtures

HTML fixture management system that allows loading external HTML files into test DOM with automatic cleanup, caching, and container management. Perfect for testing components that require specific DOM structure.

Capabilities

Global Fixture Functions

loadFixtures

Loads HTML fixture(s) from files and appends to DOM.

/**
 * Loads fixture(s) from one or more files and automatically appends to DOM
 * Cleans up any existing fixtures first
 * @param fixtureUrls - One or more fixture file URLs/paths
 */
function loadFixtures(...fixtureUrls);

Usage Examples:

// Load single fixture
loadFixtures('user-form.html');

// Load multiple fixtures  
loadFixtures('header.html', 'navigation.html', 'footer.html');

// Test the loaded content
expect($('#user-form')).toExist();
expect($('#submit-button')).toBeVisible();

appendLoadFixtures

Loads fixtures and appends to existing fixture container.

/**
 * Loads fixture(s) and appends to existing fixture container
 * Does not clean up existing fixtures first
 * @param fixtureUrls - One or more fixture file URLs/paths
 */
function appendLoadFixtures(...fixtureUrls);

readFixtures

Loads fixtures from files and returns as string without DOM insertion.

/**
 * Loads fixture(s) from files and returns combined HTML as string
 * Does not append to DOM - useful for processing HTML directly
 * @param fixtureUrls - One or more fixture file URLs/paths
 * @returns Combined HTML content as string
 */
function readFixtures(...fixtureUrls);

Usage Example:

var htmlContent = readFixtures('template.html', 'partial.html');
var processedHtml = someProcessingFunction(htmlContent);
setFixtures(processedHtml);

setFixtures

Sets fixture HTML directly without loading from files.

/**
 * Sets fixture HTML directly and appends to DOM
 * Cleans up any existing fixtures first
 * @param html - HTML string or jQuery element
 * @returns jQuery element representing the fixture container
 */
function setFixtures(html);

Usage Examples:

// Set HTML string
setFixtures('<div id="test-container"><p>Test content</p></div>');

// Set jQuery element
var $element = $('<form><input name="email" /></form>');
setFixtures($element);

// Chain with jQuery operations
var $fixture = setFixtures('<div class="widget"></div>');
$fixture.find('.widget').addClass('active');

appendSetFixtures

Sets fixture HTML and appends to existing container.

/**
 * Sets fixture HTML and appends to existing fixture container  
 * Does not clean up existing fixtures first
 * @param html - HTML string or jQuery element
 */
function appendSetFixtures(html);

preloadFixtures

Pre-loads fixtures into cache without DOM insertion.

/**
 * Pre-loads fixture(s) from files into cache without DOM insertion
 * Subsequent load/read calls will use cached content
 * @param fixtureUrls - One or more fixture file URLs/paths
 */
function preloadFixtures(...fixtureUrls);

Usage Example:

// Pre-load fixtures before test suite runs
beforeAll(function() {
  preloadFixtures('user-form.html', 'modal.html', 'table.html');
});

// Later loads will use cache (faster, no AJAX)
it('should test user form', function() {
  loadFixtures('user-form.html'); // Loaded from cache
  // ... test code
});

sandbox

Creates empty DIV element with optional attributes.

/**
 * Creates empty DIV element with optional attributes for quick fixture creation
 * @param attributes - Optional object with HTML attributes to set
 * @returns jQuery element representing the sandbox DIV
 */
function sandbox(attributes);

Usage Examples:

// Basic sandbox
var $sandbox = sandbox();
// Returns: <div id="sandbox"></div>

// Sandbox with custom attributes
var $custom = sandbox({
  id: 'my-test-area',
  class: 'test-container',
  'data-test': 'true'
});
// Returns: <div id="my-test-area" class="test-container" data-test="true"></div>

// Use with setFixtures for quick test setup
setFixtures(sandbox({class: 'widget-container'}));
$('#sandbox').append('<div class="widget">Content</div>');

Fixture Management Class

Access fixture management through jasmine.getFixtures():

/**
 * HTML Fixture management singleton
 */
interface Fixtures {
  /** DOM container ID for fixtures (default: 'jasmine-fixtures') */
  containerId: string;
  
  /** File path for fixture files (default: 'spec/javascripts/fixtures') */
  fixturesPath: string;
  
  /** Internal cache object for loaded fixtures */
  fixturesCache_: object;

  /** Load fixtures from files and create/replace container */
  load(...fixtureUrls): void;
  
  /** Load fixtures and append to existing container */
  appendLoad(...fixtureUrls): void;
  
  /** Load fixtures from files and return as string */
  read(...fixtureUrls): string;
  
  /** Set HTML directly and create/replace container */
  set(html): jQuery;
  
  /** Set HTML directly and append to existing container */
  appendSet(html): void;
  
  /** Pre-load fixtures into cache */
  preload(...fixtureUrls): void;
  
  /** Clear fixture cache */
  clearCache(): void;
  
  /** Remove fixture container from DOM */
  cleanUp(): void;
  
  /** Create sandbox DIV element */
  sandbox(attributes): jQuery;
}

// Access the singleton
var fixtures = jasmine.getFixtures();

Configuration Examples:

// Customize fixture path
jasmine.getFixtures().fixturesPath = 'test/fixtures/html';

// Customize container ID  
jasmine.getFixtures().containerId = 'my-test-container';

// Use fixture methods directly
jasmine.getFixtures().load('modal.html');
jasmine.getFixtures().clearCache();

Fixture Loading Behavior

File Loading

  • Fixtures loaded via synchronous AJAX requests
  • Files loaded relative to fixturesPath configuration
  • Multiple files concatenated in order specified
  • Automatic script tag processing in HTML fixtures

Caching System

  • All loaded fixtures automatically cached by filename
  • Cache persists across multiple test runs
  • Use clearCache() to purge cache if needed
  • preload() populates cache without DOM insertion

DOM Container Management

  • Fixtures inserted into container with ID specified by containerId
  • Container automatically created if it doesn't exist
  • Container appended to document.body
  • Automatic cleanup between tests via afterEach hook

Script Processing

HTML fixtures can contain <script src=""> tags that will be:

  • Automatically loaded and executed
  • Included in the cached fixture content
  • Processed synchronously during fixture loading

Example HTML Fixture with Scripts:

<!-- user-widget.html -->
<div id="user-widget">
  <h3>User Profile</h3>
  <div class="user-info"></div>
</div>
<script src="user-widget-behavior.js"></script>

Error Handling

The fixture system throws errors for:

/**
 * Specific error messages thrown by fixture system:
 * - "Fixture could not be loaded: [url] (status: [status], message: [message])"
 * - "Script could not be loaded: [url] (status: [status], message: [message])"
 * 
 * These errors occur when:
 * - Fixture file doesn't exist at specified path
 * - Network/file access issues during AJAX loading
 * - Script files referenced in HTML fixtures fail to load
 * - Invalid file paths or permission issues
 */

Error Handling Example:

try {
  loadFixtures('missing-file.html');
} catch (error) {
  console.log('Fixture loading failed:', error.message);
}

Configuration

Default Configuration

{
  fixturesPath: 'spec/javascripts/fixtures',  // Default HTML fixture directory
  containerId: 'jasmine-fixtures'             // Default DOM container ID
}

Custom Configuration

// Set custom fixture path
jasmine.getFixtures().fixturesPath = 'test/html-fixtures';

// Set custom container ID
jasmine.getFixtures().containerId = 'test-workspace';

// Verify configuration
expect(jasmine.getFixtures().fixturesPath).toBe('test/html-fixtures');

Integration with jasmine-ajax

When using jasmine-ajax (which overrides XMLHttpRequest), preload fixtures before jasmine-ajax loads:

// Preload before jasmine-ajax interferes with AJAX
beforeAll(function() {
  preloadFixtures('form.html', 'modal.html', 'table.html');
});

// Later fixture loads will use cache instead of AJAX
it('should work with jasmine-ajax', function() {
  loadFixtures('form.html'); // Uses cache, not blocked by jasmine-ajax
  // ... test code
});

docs

css-fixtures.md

event-spying.md

html-fixtures.md

index.md

jquery-matchers.md

json-fixtures.md

tile.json