Back to Repositories

Testing Stack Data Structure Implementation in javascript-algorithms

This test suite validates the implementation of a Stack data structure in JavaScript, covering core stack operations and edge cases. The tests ensure proper functionality of push, pop, peek operations along with empty state handling and object storage capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of Stack operations including:

  • Stack initialization and empty state verification
  • Push and pop operations for both primitive and object data
  • Peek functionality without modifying stack state
  • Empty state checking
  • Array conversion capabilities
Edge cases are addressed through null checks and empty stack operations.

Implementation Analysis

Tests follow the AAA (Arrange-Act-Assert) pattern using Jest’s testing framework. Each test case isolates specific stack functionality, with clear setup and expectations.

The implementation leverages Jest’s expect API for assertions and uses custom stringifier functions to handle object serialization.

Technical Details

  • Testing Framework: Jest
  • Test Structure: Describe/It blocks
  • Assertion Methods: expect(), toBe(), toEqual(), toBeNull()
  • Custom Utilities: toString() with stringifier support
  • Data Types Tested: Numbers, Objects, Null values

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolation of test cases and comprehensive coverage.

  • Single responsibility per test case
  • Clear test descriptions
  • Proper setup and teardown
  • Edge case handling
  • Complex object testing

trekhleb/javascript-algorithms

src/data-structures/stack/__test__/Stack.test.js

            
import Stack from '../Stack';

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

  it('should stack data to stack', () => {
    const stack = new Stack();

    stack.push(1);
    stack.push(2);

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

  it('should peek data from stack', () => {
    const stack = new Stack();

    expect(stack.peek()).toBeNull();

    stack.push(1);
    stack.push(2);

    expect(stack.peek()).toBe(2);
    expect(stack.peek()).toBe(2);
  });

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

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

    stack.push(1);

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

  it('should pop data from stack', () => {
    const stack = new Stack();

    stack.push(1);
    stack.push(2);

    expect(stack.pop()).toBe(2);
    expect(stack.pop()).toBe(1);
    expect(stack.pop()).toBeNull();
    expect(stack.isEmpty()).toBe(true);
  });

  it('should be possible to push/pop objects', () => {
    const stack = new Stack();

    stack.push({ value: 'test1', key: 'key1' });
    stack.push({ value: 'test2', key: 'key2' });

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

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

  it('should be possible to convert stack to array', () => {
    const stack = new Stack();

    expect(stack.peek()).toBeNull();

    stack.push(1);
    stack.push(2);
    stack.push(3);

    expect(stack.toArray()).toEqual([3, 2, 1]);
  });
});