Back to Repositories

Testing Title Filter Implementation in DevDocs Documentation System

This test suite validates the TitleFilter functionality in the DevDocs documentation system, focusing on heading generation and title handling across different contexts. The tests ensure proper title rendering based on entries, context settings, and root page conditions.

Test Coverage Overview

The test suite provides comprehensive coverage of title filtering scenarios in the DevDocs documentation system.

  • Tests empty entries handling
  • Validates title generation from entry names
  • Covers root title and context title interactions
  • Verifies dynamic title generation via blocks

Implementation Analysis

The implementation uses Minitest for unit testing, employing a behavior-driven development approach with nested contexts. The tests utilize stub methods for root page validation and leverage OpenStruct for entry simulation.

The testing pattern follows arrange-act-assert methodology with extensive use of before blocks for test setup and context-specific configurations.

Technical Details

  • Testing Framework: Minitest::Spec
  • Helper Modules: FilterTestHelper
  • Test Dependencies: test_helper, docs library
  • Mocking Approach: Stub methods for isolated testing
  • Data Structure: OpenStruct for mock entries

Best Practices Demonstrated

The test suite exemplifies strong testing practices through organized context blocks and clear test descriptions.

  • Isolated test contexts for different scenarios
  • Consistent setup using before blocks
  • Clear test naming conventions
  • Comprehensive edge case coverage
  • DRY principle application through helper methods

freecodecamp/devdocs

test/lib/docs/filters/core/title_test.rb

            
require_relative '../../../../test_helper'
require_relative '../../../../../lib/docs'

class TitleFilterTest < Minitest::Spec
  include FilterTestHelper
  self.filter_class = Docs::TitleFilter

  before do
    @body = '<div>Test</div>'
  end

  def output_with_title(title)
    "<h1>#{title}</h1>#{@body}"
  end

  context "when result[:entries] is empty" do
    it "does nothing" do
      assert_equal @body, filter_output.inner_html
    end

    context "and context[:title] is a string" do
      it "prepends a heading containing the title" do
        context[:title] = 'title'
        assert_equal output_with_title('title'), filter_output.inner_html
      end
    end
  end

  context "when result[:entries] is an array" do
    before do
      result[:entries] = [OpenStruct.new(name: 'name'), OpenStruct.new(name: 'name2')]
    end

    it "prepends a heading containing the first entry's name" do
      assert_equal output_with_title('name'), filter_output.inner_html
    end

    context "and context[:title] is a string" do
      it "prepends a heading containing the title" do
        context[:title] = 'title'
        assert_equal output_with_title('title'), filter_output.inner_html
      end
    end

    context "and context[:title] is nil" do
      it "prepends a heading containing the first entry's name" do
        context[:title] = nil
        assert_equal output_with_title('name'), filter_output.inner_html
      end
    end

    context "and context[:title] is false" do
      it "does nothing" do
        context[:title] = false
        assert_equal @body, filter_output.inner_html
      end
    end
  end

  context "when context[:root_title] is a string" do
    before do
      context[:root_title] = 'root'
    end

    context "and context[:title] is a string" do
      before do
        context[:title] = 'title'
      end

      it "prepends a heading containing the root title when #root_page? is true" do
        stub(filter).root_page? { true }
        assert_equal output_with_title('root'), filter_output.inner_html
      end

      it "prepends a heading containing the title when #root_page? is false" do
        stub(filter).root_page? { false }
        assert_equal output_with_title('title'), filter_output.inner_html
      end
    end
  end

  context "when context[:title] is a string" do
    before do
      context[:title] = 'title'
    end

    context "and context[:root_title] is false" do
      it "does nothing when #root_page? is true" do
        context[:root_title] = false
        stub(filter).root_page? { true }
        assert_equal @body, filter_output.inner_html
      end
    end
  end

  context "when context[:title] is a block" do
    it "calls the block with itself" do
      context[:title] = ->(arg) { @arg = arg; nil }
      filter.call
      assert_equal filter, @arg
    end

    it "prepends a heading tag containing the title returned by the block" do
      context[:title] = ->(_) { 'title' }
      assert_equal output_with_title('title'), filter_output.inner_html
    end

    it "does nothing when the block returns nil" do
      context[:title] = ->(_) { nil }
      assert_equal @body, filter_output.inner_html
    end
  end
end