Back to Repositories

Testing Queue Data Structure Implementation in javascript-algorithms

This test suite validates the implementation of a Queue data structure, ensuring proper FIFO (First-In-First-Out) operations and object handling. The tests verify core queue functionality including enqueue, dequeue, peek operations, and empty state checks.

Test Coverage Overview

The test suite provides comprehensive coverage of Queue operations, including:

  • Queue initialization and empty state verification
  • Basic enqueue and dequeue operations
  • Object handling with custom stringifier functions
  • Peek functionality without removing elements
  • Empty state checking
  • FIFO order validation

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for clear test organization. Each test case isolates specific queue behaviors, employing expect assertions to validate outcomes. The implementation demonstrates proper setup/teardown patterns with new Queue instances for each test.

  • Modular test structure with isolated test cases
  • Comprehensive assertion patterns
  • Clean instance management

Technical Details

Testing Framework: Jest
Test Style: Unit Tests

  • Uses Jest’s expect assertions
  • Implements describe/it blocks for test organization
  • Employs toBeNull, toBe, and custom matchers
  • Supports custom object stringification

Best Practices Demonstrated

The test suite exemplifies strong testing practices through clear arrangement of test cases and thorough validation of edge cases.

  • Single responsibility principle in test cases
  • Consistent setup patterns
  • Edge case coverage (empty queue, null handling)
  • Clear test naming conventions
  • Proper isolation between tests

trekhleb/javascript-algorithms

src/data-structures/queue/__test__/Queue.test.js

            
import Queue from '../Queue';

describe('Queue', () => {
  it('should create empty queue', () => {
    const queue = new Queue();
    expect(queue).not.toBeNull();
    expect(queue.linkedList).not.toBeNull();
  });

  it('should enqueue data to queue', () => {
    const queue = new Queue();

    queue.enqueue(1);
    queue.enqueue(2);

    expect(queue.toString()).toBe('1,2');
  });

  it('should be possible to enqueue/dequeue objects', () => {
    const queue = new Queue();

    queue.enqueue({ value: 'test1', key: 'key1' });
    queue.enqueue({ value: 'test2', key: 'key2' });

    const stringifier = (value) => `${value.key}:${value.value}`;

    expect(queue.toString(stringifier)).toBe('key1:test1,key2:test2');
    expect(queue.dequeue().value).toBe('test1');
    expect(queue.dequeue().value).toBe('test2');
  });

  it('should peek data from queue', () => {
    const queue = new Queue();

    expect(queue.peek()).toBeNull();

    queue.enqueue(1);
    queue.enqueue(2);

    expect(queue.peek()).toBe(1);
    expect(queue.peek()).toBe(1);
  });

  it('should check if queue is empty', () => {
    const queue = new Queue();

    expect(queue.isEmpty()).toBe(true);

    queue.enqueue(1);

    expect(queue.isEmpty()).toBe(false);
  });

  it('should dequeue from queue in FIFO order', () => {
    const queue = new Queue();

    queue.enqueue(1);
    queue.enqueue(2);

    expect(queue.dequeue()).toBe(1);
    expect(queue.dequeue()).toBe(2);
    expect(queue.dequeue()).toBeNull();
    expect(queue.isEmpty()).toBe(true);
  });
});