Back to Repositories

Testing Text Truncation Utilities in GatsbyJS

This test suite validates the text truncation utility functions in the Gatsby preload fonts plugin. It specifically focuses on the ellipses function behavior, ensuring proper text handling and truncation with ellipsis.

Test Coverage Overview

The test suite provides comprehensive coverage of the ellipses utility function.

Key areas tested include:
  • Text handling below maximum length
  • Edge case handling at exact maximum length
  • Proper truncation with ellipsis for text exceeding limits

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for clear test organization. The implementation follows BDD-style testing with explicit test cases for different text length scenarios.

Technical patterns include:
  • Nested describe blocks for logical grouping
  • Direct function import testing
  • Expect assertions with exact matching

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • Direct module imports for utility functions
  • String manipulation testing
  • Boundary value testing methodology

Best Practices Demonstrated

The test suite exemplifies several testing best practices for utility functions.

Notable practices include:
  • Clear test case separation
  • Descriptive test naming
  • Boundary value testing
  • Single responsibility principle in test cases
  • Consistent assertion patterns

gatsbyjs/gatsby

packages/gatsby-plugin-preload-fonts/src/__tests__/utils.test.js

            
const { ellipses } = require(`../prepare/utils`)

describe(`utils`, () => {
  describe(`ellipses`, () => {
    it(`does nothing to text under max length`, () => {
      expect(ellipses(`some text`, 10)).toBe(`some text`)
    })

    it(`does nothing to text at max length`, () => {
      expect(ellipses(`some text`, 9)).toBe(`some text`)
    })

    it(`truncates text over max length with \`...\``, () => {
      expect(ellipses(`some text`, 4)).toBe(`some...`)
    })
  })
})