Back to Repositories

Testing DAG Filter Hook Implementation in Apache Airflow

This test suite validates the useFilters hook functionality in Apache Airflow’s DAG interface, focusing on filter state management and URL query parameter handling. The tests ensure proper initialization, state updates, and filter clearing operations.

Test Coverage Overview

The test suite provides comprehensive coverage of the useFilters hook functionality.

  • Tests initial filter values when no URL query parameters exist
  • Validates state changes for baseDate, numRuns, runType, and runState filters
  • Verifies task filtering with upstream/downstream options
  • Tests filter clearing and reset functionality

Implementation Analysis

The testing approach utilizes Jest and React Testing Library’s renderHook functionality to validate the custom hook behavior. The implementation employs act() for state updates and RouterWrapper for simulating routing context.

Key patterns include:
  • Parameterized testing using test.each()
  • Mocked timer implementation
  • TypeScript interface validation
  • Async/await pattern for state updates

Technical Details

Testing infrastructure includes:

  • Jest as the primary testing framework
  • @testing-library/react for hook testing
  • TypeScript for type safety
  • Custom RouterWrapper utility
  • Moment.js for date handling
  • Global type declarations for DAG run configurations

Best Practices Demonstrated

The test suite exemplifies several testing best practices for React hooks.

  • Isolated test cases for each filter type
  • Comprehensive state management validation
  • Clear test case organization and naming
  • Proper cleanup and reset procedures
  • Type-safe testing with TypeScript
  • Effective use of test utilities and helpers

apache/airflow

airflow/www/static/js/dag/useFilters.test.tsx

            
/*!
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* global describe, expect, jest, test, moment */
import { act, renderHook } from "@testing-library/react";

import { RouterWrapper } from "src/utils/testUtils";
import type { DagRun, RunState } from "src/types";

declare global {
  namespace NodeJS {
    interface Global {
      defaultDagRunDisplayNumber: number;
      filtersOptions: {
        dagStates: RunState[];
        runTypes: DagRun["runType"][];
      };
    }
  }
}

const date = new Date();
date.setMilliseconds(0);
jest.useFakeTimers().setSystemTime(date);

// eslint-disable-next-line import/first
import useFilters, {
  FilterHookReturn,
  Filters,
  FilterTasksProps,
  UtilFunctions,
} from "./useFilters";

describe("Test useFilters hook", () => {
  test("Initial values when url does not have query params", async () => {
    const { result } = renderHook<FilterHookReturn, undefined>(
      () => useFilters(),
      { wrapper: RouterWrapper }
    );
    const {
      filters: {
        baseDate,
        numRuns,
        runType,
        runState,
        root,
        filterUpstream,
        filterDownstream,
      },
    } = result.current;

    expect(baseDate).toBe(date.toISOString());
    expect(numRuns).toBe(global.defaultDagRunDisplayNumber.toString());
    expect(runType).toEqual([]);
    expect(runState).toEqual([]);
    expect(root).toBeUndefined();
    expect(filterUpstream).toBeUndefined();
    expect(filterDownstream).toBeUndefined();
  });

  test.each([
    {
      fnName: "onBaseDateChange" as keyof UtilFunctions,
      paramName: "baseDate" as keyof Filters,
      // @ts-ignore
      paramValue: moment.utc().format(),
    },
    {
      fnName: "onNumRunsChange" as keyof UtilFunctions,
      paramName: "numRuns" as keyof Filters,
      paramValue: "10",
    },
    {
      fnName: "onRunTypeChange" as keyof UtilFunctions,
      paramName: "runType" as keyof Filters,
      paramValue: ["manual"],
    },
    {
      fnName: "onRunTypeChange" as keyof UtilFunctions,
      paramName: "runType" as keyof Filters,
      paramValue: ["manual", "backfill"],
    },
    {
      fnName: "onRunStateChange" as keyof UtilFunctions,
      paramName: "runState" as keyof Filters,
      paramValue: ["success"],
    },
    {
      fnName: "onRunStateChange" as keyof UtilFunctions,
      paramName: "runState" as keyof Filters,
      paramValue: ["success", "failed", "queued"],
    },
  ])("Test $fnName functions", async ({ fnName, paramName, paramValue }) => {
    const { result } = renderHook<FilterHookReturn, undefined>(
      () => useFilters(),
      { wrapper: RouterWrapper }
    );

    await act(async () => {
      result.current[fnName](
        paramValue as "string" & string[] & FilterTasksProps
      );
    });

    expect(result.current.filters[paramName]).toEqual(paramValue);

    // clearFilters
    await act(async () => {
      result.current.clearFilters();
    });

    if (paramName === "baseDate") {
      expect(result.current.filters[paramName]).toBe(date.toISOString());
    } else if (paramName === "numRuns") {
      // Number of runs isn't a filter so it should persist
      expect(result.current.filters[paramName]).toBe("10");
    } else {
      expect(result.current.filters[paramName]).toEqual([]);
    }
  });

  test("Test onFilterTasksChange ", async () => {
    const { result } = renderHook<FilterHookReturn, undefined>(
      () => useFilters(),
      { wrapper: RouterWrapper }
    );

    await act(async () => {
      result.current.onFilterTasksChange({
        root: "test",
        filterUpstream: true,
        filterDownstream: false,
      });
    });

    expect(result.current.filters.root).toBe("test");
    expect(result.current.filters.filterUpstream).toBe(true);
    expect(result.current.filters.filterDownstream).toBe(false);

    // sending same info clears filters
    await act(async () => {
      result.current.onFilterTasksChange({
        root: "test",
        filterUpstream: true,
        filterDownstream: false,
      });
    });

    expect(result.current.filters.root).toBeUndefined();
    expect(result.current.filters.filterUpstream).toBeUndefined();
    expect(result.current.filters.filterDownstream).toBeUndefined();

    await act(async () => {
      result.current.onFilterTasksChange({
        root: "test",
        filterUpstream: true,
        filterDownstream: false,
      });
    });

    expect(result.current.filters.root).toBe("test");
    expect(result.current.filters.filterUpstream).toBe(true);
    expect(result.current.filters.filterDownstream).toBe(false);

    // clearFilters
    await act(async () => {
      result.current.resetRoot();
    });

    expect(result.current.filters.root).toBeUndefined();
    expect(result.current.filters.filterUpstream).toBeUndefined();
    expect(result.current.filters.filterDownstream).toBeUndefined();
  });
});