jQuery matchers and fixture loader for Jasmine framework
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
CSS fixture management system for loading external CSS files into test DOM with automatic cleanup and style isolation. Essential for testing components that depend on specific CSS styles and layout behavior.
Loads CSS fixture(s) from files and applies to DOM.
/**
* Loads CSS fixture(s) from one or more files and applies to DOM
* Removes any existing style fixtures first
* @param fixtureUrls - One or more CSS fixture file URLs/paths
*/
function loadStyleFixtures(...fixtureUrls);Usage Examples:
// Load single CSS fixture
loadStyleFixtures('widget-styles.css');
// Load multiple CSS fixtures
loadStyleFixtures('base.css', 'layout.css', 'theme.css');
// Test CSS-dependent behavior
expect($('#widget')).toHaveCss({position: 'absolute'});
expect($('.button')).toHaveCss({display: 'inline-block'});Loads CSS fixtures and appends to existing styles.
/**
* Loads CSS fixture(s) and appends to existing style fixtures
* Does not remove existing style fixtures first
* @param fixtureUrls - One or more CSS fixture file URLs/paths
*/
function appendLoadStyleFixtures(...fixtureUrls);Usage Example:
// Load base styles first
loadStyleFixtures('base.css');
// Add additional styles without removing base
appendLoadStyleFixtures('theme.css', 'responsive.css');Sets CSS directly without loading from files.
/**
* Sets CSS content directly and applies to DOM
* Removes any existing style fixtures first
* @param css - CSS string content
*/
function setStyleFixtures(css);Usage Examples:
// Set CSS string directly
setStyleFixtures(`
.test-element {
position: absolute;
left: 300px;
top: 100px;
}
.hidden { display: none; }
`);
// Test CSS-dependent positioning
loadFixtures('<div class="test-element">Test</div>');
expect($('.test-element')).toHaveCss({left: '300px'});Sets CSS directly and appends to existing styles.
/**
* Sets CSS content directly and appends to existing style fixtures
* Does not remove existing style fixtures first
* @param css - CSS string content
*/
function appendSetStyleFixtures(css);Pre-loads CSS fixtures into cache without applying to DOM.
/**
* Pre-loads CSS fixture(s) from files into cache without applying to DOM
* Subsequent load calls will use cached content
* @param fixtureUrls - One or more CSS fixture file URLs/paths
*/
function preloadStyleFixtures(...fixtureUrls);Usage Example:
// Pre-load CSS fixtures before test suite runs
beforeAll(function() {
preloadStyleFixtures('layout.css', 'components.css');
});
// Later loads will use cache (faster, no AJAX)
it('should test styled components', function() {
loadStyleFixtures('layout.css'); // Loaded from cache
// ... test code
});Access style fixture management through jasmine.getStyleFixtures():
/**
* CSS Style Fixture management singleton
*/
interface StyleFixtures {
/** File path for CSS fixture files (default: 'spec/javascripts/fixtures') */
fixturesPath: string;
/** Internal cache object for loaded CSS fixtures */
fixturesCache_: object;
/** Array of style DOM nodes added to document head */
fixturesNodes_: HTMLStyleElement[];
/** Load CSS fixtures from files and apply to DOM */
load(...fixtureUrls): void;
/** Load CSS fixtures and append to existing styles */
appendLoad(...fixtureUrls): void;
/** Set CSS directly and apply to DOM */
set(css): void;
/** Set CSS directly and append to existing styles */
appendSet(css): void;
/** Pre-load CSS fixtures into cache */
preload(...fixtureUrls): void;
/** Clear CSS fixture cache */
clearCache(): void;
/** Remove all style fixture nodes from DOM */
cleanUp(): void;
}
// Access the singleton
var styleFixtures = jasmine.getStyleFixtures();Configuration Example:
// Customize CSS fixture path
jasmine.getStyleFixtures().fixturesPath = 'test/fixtures/styles';
// Use style fixture methods directly
jasmine.getStyleFixtures().load('widget.css');
jasmine.getStyleFixtures().clearCache();fixturesPath configuration<style> elements<head><style> elementsclearCache() to purge cache if neededpreload() populates cache without DOM applicationfixturesNodes_ arrayafterEach hookcleanUp() calledCSS fixtures solve browser-specific testing issues:
Problem Example:
// Without CSS fixtures - unreliable across browsers
$('#element').css('left', '300px');
expect($('#element').css('left')).toBe('300px'); // May return 'auto' in some browsersSolution with CSS Fixtures:
// CSS fixture: element-positioning.css
// .positioned { position: absolute; }
// Test with CSS fixture
loadStyleFixtures('element-positioning.css');
loadFixtures('<div id="element" class="positioned"></div>');
$('#element').css('left', '300px');
expect($('#element').css('left')).toBe('300px'); // Reliable across browserssetStyleFixtures(`
.grid { display: grid; grid-template-columns: 1fr 1fr; }
.grid-item { padding: 10px; }
`);
setFixtures(`
<div class="grid">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
</div>
`);
// Test grid layout behavior
expect($('.grid')).toHaveCss({display: 'grid'});
expect($('.grid-item')).toHaveLength(2);loadStyleFixtures('responsive.css');
setFixtures('<div class="responsive-widget">Widget</div>');
// Test responsive breakpoints
// (May require viewport manipulation in tests)
expect($('.responsive-widget')).toHaveCss({display: 'block'});setStyleFixtures(`
.animated { transition: opacity 0.3s ease; }
.fade-out { opacity: 0; }
.fade-in { opacity: 1; }
`);
setFixtures('<div class="animated fade-in">Content</div>');
expect($('.animated')).toHaveCss({opacity: '1'});
$('.animated').removeClass('fade-in').addClass('fade-out');
expect($('.animated')).toHaveCss({opacity: '0'});The CSS fixture system throws errors for:
/**
* Specific error messages thrown by CSS fixture system:
* - "Fixture could not be loaded: [url] (status: [status], message: [message])"
*
* These errors occur when:
* - CSS fixture file doesn't exist at specified path
* - Network/file access issues during AJAX loading
* - Invalid file paths or permission issues
* - CSS files are corrupted or inaccessible
*/{
fixturesPath: 'spec/javascripts/fixtures' // Default CSS fixture directory (same as HTML fixtures)
}// Set custom CSS fixture path
jasmine.getStyleFixtures().fixturesPath = 'test/css-fixtures';
// Verify configuration
expect(jasmine.getStyleFixtures().fixturesPath).toBe('test/css-fixtures');// Load both HTML and CSS fixtures together
loadFixtures('modal.html');
loadStyleFixtures('modal.css');
// Test styled HTML behavior
expect($('#modal')).toBeVisible();
expect($('#modal')).toHaveCss({position: 'fixed'});// Load framework CSS for testing
loadStyleFixtures('bootstrap.css', 'custom-overrides.css');
setFixtures('<button class="btn btn-primary">Click me</button>');
expect($('.btn')).toHaveCss({display: 'inline-block'});
expect($('.btn-primary')).toHaveCss({backgroundColor: 'rgb(0, 123, 255)'});describe('Cross-browser CSS behavior', function() {
beforeEach(function() {
loadStyleFixtures('cross-browser-fixes.css');
});
it('should position elements consistently', function() {
setFixtures('<div class="positioned-element">Test</div>');
$('.positioned-element').css('left', '100px');
// Reliable across IE, Firefox, Chrome, Safari
expect($('.positioned-element')).toHaveCss({left: '100px'});
});
});