Back to Repositories

Testing SublinkPresenter Navigation Hierarchy in 30-seconds-of-code

This test suite validates the SublinkPresenter functionality for managing navigation links in a hierarchical collection structure. It ensures proper handling of main, primary, and secondary collection relationships, with specific focus on sublink generation and selection states.

Test Coverage Overview

The test suite provides comprehensive coverage of the SublinkPresenter class across different collection hierarchies.

  • Tests main collection sublink generation with primary collections
  • Validates primary collection sublinks including child collections
  • Verifies secondary collection sublink behavior with parent references
  • Handles edge cases like orphaned secondary collections

Implementation Analysis

The testing approach uses Jest’s describe/it blocks to organize test cases logically by functionality. The implementation leverages factory patterns to create test fixtures, with CollectionFactory generating different collection types. Each test case validates specific sublink structures and selection states using expect assertions.

Technical Details

  • Testing Framework: Jest/Vitest
  • Test Utilities: CollectionFactory for fixture creation
  • Key Dependencies: Loader, SublinkPresenter, settings configuration
  • Setup: Module loading via Loader.loadModules()

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, descriptive test cases, and comprehensive scenario coverage.

  • Clear test case organization using nested describe blocks
  • Consistent setup of test fixtures
  • Explicit expected vs actual value comparisons
  • Coverage of both normal and edge cases

chalarangelo/30-seconds-of-code

spec/presenters/sublinkPresenter.test.js

            
/* eslint-disable no-unused-vars */
import { describe, it, expect } from 'vitest';
import Loader from '#src/lib/loader.js';
import SublinkPresenter from '#src/presenters/sublinkPresenter.js';
import settings from '#src/config/settings.js';

describe('SublinkPresenter', () => {
  Loader.loadModules();
  const { CollectionFactory } = Loader.buildFactories();

  const mainCollection = CollectionFactory.create('main');
  const primaryCollection = CollectionFactory.create('primary', {
    id: 'js',
    miniTitle: 'JavaScript',
  });
  const secondaryCollection = CollectionFactory.create('secondary', {
    id: 'js/c/array',
    parentId: 'js',
    miniTitle: 'Array',
  });
  const otherSecondaryCollection = CollectionFactory.create('secondary', {
    id: 'js/c/object',
    parentId: 'js',
    miniTitle: 'Object',
  });
  const orphanedSecondaryCollection = CollectionFactory.create('secondary', {
    id: 'js/c/number',
    miniTitle: 'Number',
  });

  const mainPresenter = new SublinkPresenter(mainCollection);
  const primaryPresenter = new SublinkPresenter(primaryCollection);
  const secondaryPresenter = new SublinkPresenter(secondaryCollection);
  const orphanedSecondaryPresenter = new SublinkPresenter(
    orphanedSecondaryCollection
  );

  describe('sublinks', () => {
    it('returns all primary collections for the main collection', () => {
      expect(mainPresenter.sublinks).toEqual([
        {
          title: 'JavaScript',
          url: '/js/p/1',
          selected: false,
        },
        settings.sublinks.moreCollections,
      ]);
    });

    it('returns all children for the primary collection', () => {
      expect(primaryPresenter.sublinks).toEqual([
        { title: 'All', url: '/js/p/1', selected: true },
        { title: 'Array', url: '/js/c/array/p/1', selected: false },
        { title: 'Object', url: '/js/c/object/p/1', selected: false },
      ]);
    });

    it('returns the parent collection sublink for the secondary collection', () => {
      expect(secondaryPresenter.sublinks).toEqual([
        { title: 'All', url: '/js/p/1', selected: false },
        { title: 'Array', url: '/js/c/array/p/1', selected: true },
        { title: 'Object', url: '/js/c/object/p/1', selected: false },
      ]);
    });

    it('returns the parent collection sublink for orphaned secondary collections', () => {
      expect(orphanedSecondaryPresenter.sublinks).toEqual([]);
    });
  });
});