Back to Repositories

Testing String Utility Functions in 30-seconds-of-code

This test suite validates the StringUtils library’s string manipulation functions in the 30-seconds-of-code repository. It covers essential string operations including case conversion, HTML/Markdown stripping, and SEO slug generation using Jest testing framework.

Test Coverage Overview

The test suite provides comprehensive coverage of string utility functions:

  • Case manipulation (capitalize, kebab case)
  • ID and slug generation
  • HTML/Markdown content stripping
  • Text normalization and escaping
  • Tag formatting
Edge cases include handling of special characters, HTML entities, and mixed content types.

Implementation Analysis

The testing approach employs Jest’s describe/it blocks for clear test organization and isolation. Each utility function is tested independently using expect assertions with precise input/output validation.

The implementation leverages Jest’s toBe and toEqual matchers for strict equality checking, with well-structured test cases demonstrating function behavior.

Technical Details

  • Testing Framework: Jest
  • Test Structure: Nested describe blocks
  • Assertion Style: expect().toBe()
  • Import System: ES Modules
  • File Organization: Modular test suite

Best Practices Demonstrated

The test suite exemplifies several testing best practices including descriptive test cases, isolation of functionality, and comprehensive coverage of edge cases.

  • Clear test descriptions
  • Single responsibility principle
  • Consistent naming conventions
  • Modular test organization
  • Comprehensive function coverage

chalarangelo/30-seconds-of-code

spec/lib/stringUtils.test.js

            
import { describe, it, expect } from 'vitest';
import StringUtils from '#src/lib/stringUtils.js';

describe('StringUtils', () => {
  describe('capitalize', () => {
    it('should capitalize the first letter of a string', () => {
      expect(StringUtils.capitalize('hello')).toBe('Hello');
    });

    it('should capitalize the first letter and lowercase the rest of a string', () => {
      expect(StringUtils.capitalize('hello', true)).toBe('Hello');
    });
  });

  describe('toKebabCase', () => {
    it('should convert a string to kebab case', () => {
      expect(StringUtils.toKebabCase('Hello World')).toBe('hello-world');
    });
  });

  describe('convertToValidId', () => {
    it('should convert a string to a valid id name', () => {
      const htmlString = '<p>This is a <strong>paragraph</strong>.</p>';
      const validId = 'this-is-a-paragraph';

      expect(StringUtils.convertToValidId(htmlString)).toBe(validId);
    });
  });

  describe('convertToSeoSlug', () => {
    it('should convert a string to a SEO slug', () => {
      expect(StringUtils.convertToSeoSlug('Hello World')).toBe('/hello-world');
    });
  });

  describe('stripMarkdownFormat', () => {
    const markdownString =
      'This is `code` and this is **bold** and this is [a link](https://example.com) and this is _italic_.';
    const strippedString =
      'This is code and this is bold and this is a link and this is italic.';

    it('should strip markdown format from a string', () => {
      expect(StringUtils.stripMarkdown(markdownString)).toBe(strippedString);
    });
  });

  describe('stripHtmlParagraphsAndLinks', () => {
    it('should strip HTML paragraphs and links', () => {
      expect(
        StringUtils.stripHtmlParagraphsAndLinks(
          '<p><a href="http://example.com">Hello</a></p>'
        )
      ).toBe('Hello');
    });
  });

  describe('stripHtmlTags', () => {
    const htmlString = '<p>This is a <strong>paragraph</strong>.</p>';
    const strippedString = 'This is a paragraph.';

    it('should strip HTML tags from a string', () => {
      expect(StringUtils.stripHtmlTags(htmlString)).toBe(strippedString);
    });
  });

  describe('stripHtml', () => {
    it('should strip HTML', () => {
      expect(StringUtils.stripHtml('<p>Hello & hi!</p>')).toBe(
        'Hello & hi!'
      );
    });
  });

  describe('normalizedTokens', () => {
    it('should normalize tokens', () => {
      expect(StringUtils.normalizedTokens('Hello a World!')).toEqual([
        'hello',
        'world',
      ]);
    });
  });

  describe('escapeHtml', () => {
    it('should escape HTML', () => {
      expect(StringUtils.escapeHtml('Hello & "world"')).toBe(
        'Hello & "world"'
      );
    });
  });

  describe('formatTag', () => {
    it('should format a tag', () => {
      expect(StringUtils.formatTag('hello')).toBe('Hello');
    });
  });
});