Back to Repositories

Testing Page Route Metadata Configuration in uni-app

This test suite validates the page route metadata initialization functionality in uni-app, focusing on navigation bar configuration and pull-to-refresh features. The tests ensure proper handling of default configurations and custom overrides for page-specific settings.

Test Coverage Overview

The test coverage focuses on the initRouteMeta function’s ability to handle various page configuration scenarios.

  • Tests default navigation bar configuration inheritance
  • Verifies custom title text and color overrides
  • Validates pull-to-refresh settings integration
  • Ensures proper snapshot matching for different configurations

Implementation Analysis

The testing approach utilizes Jest’s snapshot testing capabilities for comparing complex object structures. The implementation follows a systematic pattern of testing increasingly complex configuration scenarios, from basic defaults to specific overrides.

  • Uses global configuration mocking
  • Implements JSON-based configuration handling
  • Leverages TypeScript type safety for configuration objects

Technical Details

  • Testing Framework: Jest
  • Language: TypeScript
  • Key Functions: initRouteMeta, initDefaultUniConfig
  • Configuration: Global __uniConfig setup
  • Testing Methods: Snapshot matching, object configuration

Best Practices Demonstrated

The test suite demonstrates several testing best practices for configuration-heavy applications.

  • Proper test isolation through configuration reset
  • Progressive complexity in test cases
  • Snapshot testing for complex object validation
  • Clear test case organization and structure
  • Effective global state management in tests

dcloudio/uni-app

packages/uni-core/__tests__/helpers/page.spec.ts

            
import { initRouteMeta } from '../../src/helpers/page'

function initDefaultUniConfig() {
  return JSON.parse(
    JSON.stringify({
      globalStyle: {
        navigationBar: {},
      },
    })
  )
}

describe('page', () => {
  test('initRouteMeta', () => {
    global.__uniConfig = initDefaultUniConfig()
    global.__uniConfig.globalStyle.navigationBar.titleText = 'uni-app'
    expect(
      initRouteMeta(
        {
          route: '',
          navigationBar: {},
        },
        1
      )
    ).toMatchSnapshot()
    expect(
      initRouteMeta(
        {
          route: '',
          navigationBar: { titleText: 'hello', titleColor: '#000000' },
          enablePullDownRefresh: true,
        },
        1
      )
    ).toMatchSnapshot()
    expect(
      initRouteMeta(
        {
          route: '',
          navigationBar: { titleColor: '#000000' },
          enablePullDownRefresh: true,
          pullToRefresh: {
            offset: 100,
          },
        },
        1
      )
    ).toMatchSnapshot()
  })
})