Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*!
* 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.
*/
import type { PropsWithChildren } from "react";

import "@testing-library/jest-dom";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { BaseWrapper } from "src/utils/Wrapper";

import { TriggerDAGButton } from "./TriggerDAGButton";

const wrapper = ({ children }: PropsWithChildren) => (
<BaseWrapper>
<MemoryRouter>{children}</MemoryRouter>
</BaseWrapper>
);

const routeParams: Record<string, string> = {};

vi.mock("react-router-dom", async () => {
const actual = await vi.importActual("react-router-dom");

return {
...actual,
useParams: () => routeParams,
};
});

const useDagRunServiceGetDagRunMock = vi.hoisted(() => vi.fn());

vi.mock("openapi/queries", () => ({
useDagRunServiceGetDagRun: useDagRunServiceGetDagRunMock,
}));

vi.mock("react-i18next", () => ({
useTranslation: () => ({
i18n: { language: "en" },
// eslint-disable-next-line id-length
t: (translationKey: string) =>
({
"triggerDag.button": "Trigger",
"triggerDag.manualRunDenied": "Manual runs are not allowed for this Dag",
"triggerDag.title": "Trigger Dag",
"triggerDag.triggerAgainWithConfig": "Trigger again with this config",
})[translationKey] ?? translationKey,
}),
}));

vi.mock("./TriggerDAGModal", () => ({
default: ({ open }: { readonly open: boolean }) =>
open ? <div data-testid="trigger-modal">Trigger Modal</div> : null,
}));

const props = { dagDisplayName: "My Dag", dagId: "my_dag", isPaused: false };

afterEach(() => {
cleanup();
routeParams.runId = "";
});

beforeEach(() => {
useDagRunServiceGetDagRunMock.mockReset();
useDagRunServiceGetDagRunMock.mockReturnValue({ data: undefined });
});

describe("TriggerDAGButton", () => {
it("opens the form directly, with no config menu, when the selected run has an empty config", () => {
routeParams.runId = "run_empty_conf";
useDagRunServiceGetDagRunMock.mockReturnValue({
data: { conf: {}, dag_run_id: "run_empty_conf", logical_date: "2026-01-01T00:00:00Z" },
});

render(<TriggerDAGButton {...props} withText />, { wrapper });

fireEvent.click(screen.getByTestId("trigger-dag-button"));

expect(screen.getByTestId("trigger-modal")).toBeInTheDocument();
expect(screen.queryByText("Trigger again with this config")).not.toBeInTheDocument();
});

it("shows the config menu when the selected run has a non-empty config", async () => {
routeParams.runId = "run_with_conf";
useDagRunServiceGetDagRunMock.mockReturnValue({
data: { conf: { country: "FR" }, dag_run_id: "run_with_conf", logical_date: "2026-01-01T00:00:00Z" },
});

render(<TriggerDAGButton {...props} withText />, { wrapper });

fireEvent.click(screen.getByTestId("trigger-dag-button"));

expect(await screen.findByText("Trigger again with this config")).toBeInTheDocument();
expect(screen.queryByTestId("trigger-modal")).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { useDagRunServiceGetDagRun } from "openapi/queries";

import { IconButton, Menu, Tooltip } from "src/system-components";

import { hasDagRunConfig } from "src/utils";

import TriggerDAGModal from "./TriggerDAGModal";

type TriggerDAGButtonProps = {
Expand Down Expand Up @@ -92,8 +94,9 @@ export const TriggerDAGButton = ({
onClose();
};

// If there's a selected DAG Run with config, show menu with options
if (selectedDagRun?.conf !== undefined) {
// If the selected Dag run carries a non-empty config, show the menu with options. A run triggered
// without a config comes back as an empty object `{}`, which must not count as "has config".
if (hasDagRunConfig(selectedDagRun?.conf)) {
return (
<Box>
<Menu.Root>
Expand Down
4 changes: 2 additions & 2 deletions airflow-core/src/airflow/ui/src/pages/DagRuns/DagRuns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { TruncatedText } from "src/components/TruncatedText";
import { SearchParamsKeys, type SearchParamsKeysType } from "src/constants/searchParams";
import { useAdvancedSearchArg } from "src/hooks/useAdvancedSearch";
import { useConfig } from "src/queries/useConfig";
import { useAutoRefresh, isStatePending, useDocumentTitle } from "src/utils";
import { useAutoRefresh, hasDagRunConfig, isStatePending, useDocumentTitle } from "src/utils";

import BulkClearDagRunsButton from "./BulkClearDagRunsButton";
import BulkDeleteDagRunsButton from "./BulkDeleteDagRunsButton";
Expand Down Expand Up @@ -215,7 +215,7 @@ const runColumns = ({ dagId, multiTeam, open, translate }: ColumnProps): Array<C
{
accessorKey: "conf",
cell: ({ row: { original } }) =>
original.conf && Object.keys(original.conf).length > 0 ? (
hasDagRunConfig(original.conf) ? (
<RenderedJsonField collapsed={!open} content={original.conf} />
) : undefined,
header: translate("dagRun.conf"),
Expand Down
4 changes: 2 additions & 2 deletions airflow-core/src/airflow/ui/src/pages/Run/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { TeamName } from "src/components/TeamName";
import Time from "src/components/Time";

import { useShowTeam } from "src/hooks/useShowTeam";
import { isStatePending, useAutoRefresh, useDurationFormat } from "src/utils";
import { hasDagRunConfig, isStatePending, useAutoRefresh, useDurationFormat } from "src/utils";

export const Details = () => {
const { t: translate } = useTranslation(["common", "components"]);
Expand Down Expand Up @@ -196,7 +196,7 @@ export const Details = () => {
<Table.Row>
<Table.Cell>{translate("dagRun.conf")}</Table.Cell>
<Table.Cell>
<RenderedJsonField content={dagRun.conf ?? {}} />
{hasDagRunConfig(dagRun.conf) ? <RenderedJsonField content={dagRun.conf} /> : undefined}
</Table.Cell>
</Table.Row>
</Table.Body>
Expand Down
33 changes: 33 additions & 0 deletions airflow-core/src/airflow/ui/src/utils/hasDagRunConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* 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.
*/
import { describe, it, expect } from "vitest";

import { hasDagRunConfig } from "./hasDagRunConfig";

describe("hasDagRunConfig", () => {
it.each([
{ description: "undefined", expected: false, input: undefined },
{ description: "null", expected: false, input: null },
{ description: "an empty object", expected: false, input: {} },
{ description: "a non-empty object", expected: true, input: { country: "FR" } },
{ description: "an object with several keys", expected: true, input: { batch: 42, env: "prod" } },
])("returns $expected for $description", ({ expected, input }) => {
expect(hasDagRunConfig(input)).toBe(expected);
});
});
24 changes: 24 additions & 0 deletions airflow-core/src/airflow/ui/src/utils/hasDagRunConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* 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.
*/

// A Dag run that was triggered without a config comes back from the API as an empty object `{}`,
// not null/undefined, so a presence check has to treat `{}` as "no config supplied" too.
export const hasDagRunConfig = (
conf: Record<string, unknown> | null | undefined,
): conf is Record<string, unknown> => conf !== null && conf !== undefined && Object.keys(conf).length > 0;
1 change: 1 addition & 0 deletions airflow-core/src/airflow/ui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
export { capitalize } from "./capitalize";
export { createErrorToaster, getErrorStatus } from "./errorHandling";
export { getMetaKey } from "./getMetaKey";
export { hasDagRunConfig } from "./hasDagRunConfig";
export { toNullablePartitionKey } from "./partitionKey";
export { useContainerWidth } from "./useContainerWidth";
export { useDocumentTitle } from "./useDocumentTitle";
Expand Down
Loading