Back to Repositories

Testing Plugin Options Preset Merging in GatsbyJS WordPress Source

This test suite validates the plugin options preset functionality in Gatsby’s WordPress source plugin, focusing on how preset configurations merge with default and user-defined settings.

Test Coverage Overview

The test coverage focuses on the plugin options merging behavior, specifically examining how preset configurations interact with user-defined settings.

  • Verifies correct merging of preset data with default configurations
  • Tests override behavior when user options conflict with preset values
  • Validates inheritance of preset-only options

Implementation Analysis

The testing approach utilizes Jest’s test framework with a custom store implementation based on Redux patterns.

Key implementation aspects include:
  • Custom withGlobalStore wrapper for state management
  • Async local storage implementation for test isolation
  • Redux-style dispatch and state management patterns

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • Custom store implementation using createStore and getStore utilities
  • asyncLocalStorage for managing test state
  • Redux-pattern state management

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Isolated test state management through asyncLocalStorage
  • Clear test case organization with descriptive titles
  • Explicit expectations for both overridden and inherited values
  • Proper setup and teardown of test state

gatsbyjs/gatsby

packages/gatsby-source-wordpress/src/models/__tests__/gatsby-api.test.js

            
import { getStore, createStore, asyncLocalStorage } from "../../../dist/store"
const store = { store: createStore(), key: `test` }

const withGlobalStore = fn => () => asyncLocalStorage.run(store, fn)
test(
  `Plugin options presets merge preset data into default and user data`,
  withGlobalStore(() => {
    getStore().dispatch.gatsbyApi.setState({
      pluginOptions: {
        url: `test.com`,
        type: {
          ExistingType: {
            limit: 3,
          },
          FakeType: {
            exclude: true,
          },
        },
        presets: [
          {
            presetName: `TEST_PRESET`,
            useIf: () => true,
            options: {
              type: {
                FakeType: {
                  exclude: false,
                  limit: 1,
                },
                ExistingType: {
                  limit: 2,
                },
              },
            },
          },
        ],
      },
      helpers: null,
    })

    const { pluginOptions } = getStore().getState().gatsbyApi

    // our top level options override preset options
    expect(pluginOptions.type.ExistingType.limit).toBe(3)
    expect(pluginOptions.type.FakeType.exclude).toBe(true)

    // this option wasn't defined at the top level but is part of the preset
    expect(pluginOptions.type.FakeType.limit).toBe(1)
  })
)