Back to Repositories

Testing Extension API Parameter Normalization in uni-app

This test suite validates the extension API normalization functionality in uni-app, focusing on parameter handling and module organization. It ensures proper formatting and mapping of API configurations across different platform implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of extension API handling:

  • Parameter normalization for various UI and system APIs
  • Module mapping and organization across different platforms
  • Default parameter handling for multiple API endpoints
  • Edge cases for null values and platform-specific implementations

Implementation Analysis

The testing approach employs Jest’s expect assertions to validate API normalization functions. It uses structured test cases to verify the transformation of complex nested objects into normalized formats, with particular attention to platform-specific implementations in JavaScript, Kotlin, and Swift.

Technical Details

Testing infrastructure includes:

  • Jest testing framework
  • TypeScript for type-safe testing
  • Normalized object comparison using toEqual matcher
  • Module-level test organization with describe blocks

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Systematic validation of input/output transformations
  • Comprehensive coverage of edge cases
  • Clear test case organization
  • Platform-specific implementation verification

dcloudio/uni-app

packages/uni-uts-v1/__tests__/extApi.spec.ts

            
import {
  normalizeExtApiDefaultParameters,
  normalizeExtApiModules,
  // parseExtApiModules,
} from '../src/utils'

describe('ext-api', () => {
  test('default parameters', () => {
    expect(
      normalizeExtApiDefaultParameters({
        'uni-route': {
          navigateBack: 'null',
        },
        'uni-ui': {
          startPullDownRefresh: 'null',
          hideTabBar: 'null',
          showTabBar: 'null',
        },
        'uni-exit': {
          exit: 'null',
        },
        'uni-getAppBaseInfo': {
          getAppBaseInfo: 'null',
        },
        'uni-getDeviceInfo': {
          getDeviceInfo: 'null',
        },
        'uni-storage': {
          clearStorage: 'null',
        },
      })
    ).toEqual({
      navigateBack: ['null'],
      startPullDownRefresh: ['null'],
      hideTabBar: ['null'],
      showTabBar: ['null'],
      exit: ['null'],
      getAppBaseInfo: ['null'],
      getDeviceInfo: ['null'],
      clearStorage: ['null'],
    })
    expect(
      normalizeExtApiDefaultParameters({
        'uni-route': {
          navigateBack: ['null'],
        },
        'uni-ui': {
          startPullDownRefresh: ['null'],
          hideTabBar: ['null'],
          showTabBar: ['null'],
        },
        'uni-exit': {
          exit: ['null'],
        },
        'uni-getAppBaseInfo': {
          getAppBaseInfo: ['null'],
        },
        'uni-getDeviceInfo': {
          getDeviceInfo: ['null'],
        },
        'uni-storage': {
          clearStorage: ['null'],
        },
      })
    ).toEqual({
      navigateBack: ['null'],
      startPullDownRefresh: ['null'],
      hideTabBar: ['null'],
      showTabBar: ['null'],
      exit: ['null'],
      getAppBaseInfo: ['null'],
      getDeviceInfo: ['null'],
      clearStorage: ['null'],
    })
    expect(
      normalizeExtApiDefaultParameters({
        'uni-route': {
          navigateBack: ['null'],
        },
        'uni-ui': {
          startPullDownRefresh: ['null'],
          hideTabBar: ['null'],
          showTabBar: ['null'],
        },
        'uni-exit': {
          exit: ['null'],
        },
        'uni-getAppBaseInfo': {
          getAppBaseInfo: ['null'],
        },
        'uni-getDeviceInfo': {
          getDeviceInfo: ['null'],
        },
        'uni-storage': {
          clearStorage: ['null'],
        },
        'uni-video': {
          createVideoContext: [null, 'null'],
        },
      })
    ).toEqual({
      navigateBack: ['null'],
      startPullDownRefresh: ['null'],
      hideTabBar: ['null'],
      showTabBar: ['null'],
      exit: ['null'],
      getAppBaseInfo: ['null'],
      getDeviceInfo: ['null'],
      clearStorage: ['null'],
      createVideoContext: ['', 'null'],
    })
  })
  test('modules', () => {
    expect(
      normalizeExtApiModules({
        'uni-getLocation-system': {
          uni: ['getLocation'],
        },
        'uni-video': {
          uni: {
            createVideoContext: {
              name: 'createVideoContext',
              app: {
                js: false,
                kotlin: true,
                swift: false,
              },
            },
          },
          components: ['video'],
        },
        'uni-storage': {
          uni: {
            getStorage: {
              name: 'getStorage',
              app: {
                js: false,
                kotlin: true,
                swift: true,
              },
            },
            getStorageInfo: {
              name: 'getStorageInfo',
              app: {
                js: false,
                kotlin: true,
                swift: true,
              },
            },
            clearStorage: {
              name: 'clearStorage',
              app: {
                js: false,
                kotlin: true,
                swift: true,
              },
            },
            clearStorageSync: {
              name: 'clearStorageSync',
              app: {
                js: false,
                kotlin: true,
                swift: true,
              },
            },
          },
        },
      })
    ).toEqual({
      'component.video': 'uni-video',
      'uni.clearStorage': 'uni-storage',
      'uni.clearStorageSync': 'uni-storage',
      'uni.createVideoContext': 'uni-video',
      'uni.getLocation': 'uni-getLocation-system',
      'uni.getStorage': 'uni-storage',
      'uni.getStorageInfo': 'uni-storage',
    })
    // TODO 目前暂时屏蔽了 uts-video 的编译
    // expect(parseExtApiModules()).toMatchObject({
    //   'uni.createVideoContext': 'uni-video',
    //   'component.video': 'uni-video',
    // })
  })
})