Back to Repositories

Testing Slot Transformation and Fallback Content in uni-app Compiler

This test suite validates slot transformation functionality in the uni-app compiler, focusing on fallback content handling and named slots. The tests ensure proper compilation of slot elements with various configurations in miniProgram components.

Test Coverage Overview

The test suite provides comprehensive coverage of slot transformation scenarios.

Key areas tested include:
  • Basic slot implementation without content
  • Slots with fallback content
  • Named slots functionality
  • Named slots with fallback content
The suite verifies both simple and complex slot configurations to ensure reliable component composition.

Implementation Analysis

The testing approach uses Jest’s describe/test pattern with a custom assert utility for comparing input and output code snippets.

Key implementation patterns include:
  • Consistent test structure using assert() helper
  • Miniprogram-specific configuration options
  • Template transformation verification
  • Runtime code generation testing

Technical Details

Testing infrastructure includes:
  • Jest test framework
  • Custom assert utility function
  • MiniProgram configuration options
  • Slot fallback content flag enabled
  • Template transformation validation
  • Runtime code generation verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for distinct functionality
  • Consistent test structure and naming
  • Clear input/output expectations
  • Configuration separation
  • Comprehensive edge case coverage
  • Maintainable test organization

dcloudio/uni-app

packages/uni-mp-compiler/__tests__/slot.fallback.spec.ts

            
import { assert, miniProgram } from './testUtils'

const options = {
  miniProgram: {
    ...miniProgram,
    slot: {
      fallbackContent: true,
    },
  },
}
describe('compiler: transform slot', () => {
  test('basic', () => {
    assert(
      `<button><slot/></button>`,
      `<button><slot/></button>`,
      `(_ctx, _cache) => {
  return {}
}`,
      options
    )
  })
  test('fallback content', () => {
    assert(
      `<button><slot>Submit</slot></button>`,
      `<button><slot>Submit</slot></button>`,
      `(_ctx, _cache) => {
  return {}
}`,
      options
    )
  })
  test('names slots', () => {
    assert(
      `<button><slot name="text"/></button>`,
      `<button><slot name="text"/></button>`,
      `(_ctx, _cache) => {
  return {}
}`,
      options
    )
  })
  test('names slots with fallback content', () => {
    assert(
      `<button><slot name="text">Submit</slot></button>`,
      `<button><slot name="text">Submit</slot></button>`,
      `(_ctx, _cache) => {
  return {}
}`,
      options
    )
  })
})