Back to Repositories

Testing Snippet Recommendation Logic in 30-seconds-of-code

This test suite validates the RecommendationPresenter class functionality in the 30-seconds-of-code repository, focusing on snippet recommendation logic and filtering capabilities. The tests ensure proper handling of snippet recommendations based on language, tags, and listing status.

Test Coverage Overview

The test suite provides comprehensive coverage of the RecommendationPresenter’s recommendation system.

Key areas tested include:
  • Filtering out current snippets from recommendations
  • Handling cross-language snippet exclusion
  • Unlisted snippet filtering
  • Tag-based recommendation matching
  • Language-specific snippet filtering

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for clear test organization and factory patterns for test data setup.

Notable implementation features:
  • Factory pattern for snippet creation
  • Modular test structure using describe blocks
  • Expectation chaining for precise assertions
  • Mock snippet data with varied attributes

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • Vitest for test execution
  • Custom Loader utility for module management
  • Factory classes for test data generation
  • ESLint configuration for code quality

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and thorough coverage.

Notable practices include:
  • Descriptive test case naming
  • Isolated test scenarios
  • Comprehensive edge case handling
  • Consistent test data setup
  • Clear assertion patterns

chalarangelo/30-seconds-of-code

spec/presenters/recommendationPresenter.test.js

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

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

  const snippet = SnippetFactory.create({
    languageId: 'javascript',
    id: 'js/s/my-snippet',
    shortTitle: 'My snippet',
    tags: 'array;object',
  });

  const sameSnippetOtherLanguage = SnippetFactory.create({
    languageId: 'python',
    id: 'py/s/my-snippet',
    shortTitle: 'My snippet',
    tags: 'array;object',
  });

  const unlistedSnippet = SnippetFactory.create('unlisted', {
    languageId: 'javascript',
    id: 'js/s/unrecommendable-snippet',
    shortTitle: 'Unrecommendable snippet',
    tags: 'array;object',
  });

  const highlyRecommendableSnippet = SnippetFactory.create({
    languageId: 'javascript',
    id: 'js/s/highly-recommendable-snippet',
    shortTitle: 'Highly recommendable snippet',
    tags: 'array;object',
  });

  const somewhatRecommendableSnippet = SnippetFactory.create({
    languageId: 'javascript',
    id: 'js/s/somewhat-recommendable-snippet',
    shortTitle: 'Somewhat recommendable snippet',
    tags: 'browser',
  });

  const presenter = new RecommendationPresenter(snippet);

  describe('#recommend_snippets', () => {
    const subject = presenter.recommendSnippets();

    it('returns snippets that are not the current snippet', () => {
      expect(subject).not.toContainEqual(snippet);
    });

    it('does not return the same snippet in another language', () => {
      expect(subject).not.toContainEqual(sameSnippetOtherLanguage);
    });

    it('does not return unlisted snippets', () => {
      expect(subject).not.toContainEqual(unlistedSnippet);
    });

    it('returns snippets in the same language with the same tags', () => {
      expect(subject).toContainEqual(highlyRecommendableSnippet);
    });

    it('returns snippets in the same language with similar tags', () => {
      expect(subject).toContainEqual(somewhatRecommendableSnippet);
    });
  });
});