Back to Repositories

Testing Comparator Utility Operations in javascript-algorithms

This test suite validates the Comparator utility class functionality in javascript-algorithms, covering both default and custom comparison operations. The tests ensure proper implementation of equality, less than, greater than, and their inclusive variants across different data types.

Test Coverage Overview

The test suite provides comprehensive coverage of the Comparator class functionality:

  • Default comparison operations for numbers and strings
  • Custom comparison logic based on string length
  • Reverse comparison functionality
  • Edge cases including equality checks and boundary conditions

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks to organize test cases logically. The implementation demonstrates both default and custom comparator scenarios, with particular attention to:

  • Method chaining patterns
  • Comparison operator validation
  • Type-agnostic comparison handling

Technical Details

Testing infrastructure includes:

  • Jest testing framework
  • Expect assertions for validation
  • ES6 class instantiation
  • Custom comparison function implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for different functionalities
  • Clear test descriptions
  • Comprehensive assertion coverage
  • Proper test organization and structure

trekhleb/javascript-algorithms

src/utils/comparator/__test__/Comparator.test.js

            
import Comparator from '../Comparator';

describe('Comparator', () => {
  it('should compare with default comparator function', () => {
    const comparator = new Comparator();

    expect(comparator.equal(0, 0)).toBe(true);
    expect(comparator.equal(0, 1)).toBe(false);
    expect(comparator.equal('a', 'a')).toBe(true);
    expect(comparator.lessThan(1, 2)).toBe(true);
    expect(comparator.lessThan(-1, 2)).toBe(true);
    expect(comparator.lessThan('a', 'b')).toBe(true);
    expect(comparator.lessThan('a', 'ab')).toBe(true);
    expect(comparator.lessThan(10, 2)).toBe(false);
    expect(comparator.lessThanOrEqual(10, 2)).toBe(false);
    expect(comparator.lessThanOrEqual(1, 1)).toBe(true);
    expect(comparator.lessThanOrEqual(0, 0)).toBe(true);
    expect(comparator.greaterThan(0, 0)).toBe(false);
    expect(comparator.greaterThan(10, 0)).toBe(true);
    expect(comparator.greaterThanOrEqual(10, 0)).toBe(true);
    expect(comparator.greaterThanOrEqual(10, 10)).toBe(true);
    expect(comparator.greaterThanOrEqual(0, 10)).toBe(false);
  });

  it('should compare with custom comparator function', () => {
    const comparator = new Comparator((a, b) => {
      if (a.length === b.length) {
        return 0;
      }

      return a.length < b.length ? -1 : 1;
    });

    expect(comparator.equal('a', 'b')).toBe(true);
    expect(comparator.equal('a', '')).toBe(false);
    expect(comparator.lessThan('b', 'aa')).toBe(true);
    expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(false);
    expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(true);
    expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);

    comparator.reverse();

    expect(comparator.equal('a', 'b')).toBe(true);
    expect(comparator.equal('a', '')).toBe(false);
    expect(comparator.lessThan('b', 'aa')).toBe(false);
    expect(comparator.greaterThanOrEqual('a', 'aa')).toBe(true);
    expect(comparator.greaterThanOrEqual('aa', 'a')).toBe(false);
    expect(comparator.greaterThanOrEqual('a', 'a')).toBe(true);
  });
});