Back to Repositories

Validating Fragment Detection Implementation in Preact

This test suite validates the isFragment functionality in Preact’s compatibility layer, ensuring proper detection of Fragment components across both Preact and React-compatible implementations. The tests verify correct Fragment identification while handling various invalid input cases.

Test Coverage Overview

The test suite provides comprehensive coverage of the isFragment utility function, focusing on both positive and negative test cases.

  • Tests invalid argument handling with various data types
  • Validates Fragment detection in Preact vnodes
  • Verifies Fragment detection in compat layer vnodes
  • Covers edge cases with null, boolean, and primitive values

Implementation Analysis

The testing approach employs Jest’s describe/it pattern to organize test cases logically. The implementation validates Fragment detection across both Preact’s native createElement and the React compatibility layer.

  • Uses expect().to.equal() assertions for boolean checks
  • Tests both preactCreateElement and React.createElement implementations
  • Maintains consistent test structure across different scenarios

Technical Details

  • Testing Framework: Jest
  • Key Dependencies: preact, preact/compat
  • Test Environment: Browser-based
  • Import Patterns: Destructured imports from core and compat packages

Best Practices Demonstrated

The test suite exemplifies strong testing practices by maintaining clear separation of concerns and thorough edge case coverage.

  • Comprehensive invalid input testing
  • Consistent assertion patterns
  • Clear test case isolation
  • Proper import organization
  • Descriptive test naming conventions

preactjs/preact

compat/test/browser/isFragment.test.js

            
import { createElement as preactCreateElement, Fragment } from 'preact';
import React, { isFragment } from 'preact/compat';

describe('isFragment', () => {
	it('should check return false for invalid arguments', () => {
		expect(isFragment(null)).to.equal(false);
		expect(isFragment(false)).to.equal(false);
		expect(isFragment(true)).to.equal(false);
		expect(isFragment('foo')).to.equal(false);
		expect(isFragment(123)).to.equal(false);
		expect(isFragment([])).to.equal(false);
		expect(isFragment({})).to.equal(false);
	});

	it('should detect a preact vnode', () => {
		expect(isFragment(preactCreateElement(Fragment, {}))).to.equal(true);
	});

	it('should detect a compat vnode', () => {
		expect(isFragment(React.createElement(Fragment, {}))).to.equal(true);
	});
});