|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { render } from "@testing-library/react"; |
| 20 | +import { describe, expect, it, vi } from "vitest"; |
| 21 | + |
| 22 | +import { Wrapper } from "src/utils/Wrapper"; |
| 23 | + |
| 24 | +import { DagVersionSelect } from "./DagVersionSelect"; |
| 25 | + |
| 26 | +const dagVersionV1 = { |
| 27 | + bundle_name: "dags-folder", |
| 28 | + bundle_version: null, |
| 29 | + created_at: "2025-01-01T00:00:00Z", |
| 30 | + dag_id: "test_dag", |
| 31 | + version_number: 1, |
| 32 | +}; |
| 33 | +const dagVersionV2 = { |
| 34 | + bundle_name: "dags-folder", |
| 35 | + bundle_version: null, |
| 36 | + created_at: "2025-01-02T00:00:00Z", |
| 37 | + dag_id: "test_dag", |
| 38 | + version_number: 2, |
| 39 | +}; |
| 40 | +const dagVersionV3 = { |
| 41 | + bundle_name: "dags-folder", |
| 42 | + bundle_version: null, |
| 43 | + created_at: "2025-01-03T00:00:00Z", |
| 44 | + dag_id: "test_dag", |
| 45 | + version_number: 3, |
| 46 | +}; |
| 47 | + |
| 48 | +const allVersions = [dagVersionV3, dagVersionV2, dagVersionV1]; |
| 49 | + |
| 50 | +let mockParams: Record<string, string> = { dagId: "test_dag" }; |
| 51 | + |
| 52 | +vi.mock("react-router-dom", async () => { |
| 53 | + const actual = await vi.importActual("react-router-dom"); |
| 54 | + |
| 55 | + return { |
| 56 | + ...actual, |
| 57 | + useParams: () => mockParams, |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +vi.mock("openapi/queries", () => ({ |
| 62 | + useDagRunServiceGetDagRun: vi.fn(() => ({ |
| 63 | + data: undefined, |
| 64 | + isLoading: false, |
| 65 | + })), |
| 66 | + useDagVersionServiceGetDagVersions: vi.fn(() => ({ |
| 67 | + data: { dag_versions: allVersions, total_entries: 3 }, |
| 68 | + isLoading: false, |
| 69 | + })), |
| 70 | +})); |
| 71 | + |
| 72 | +vi.mock("src/hooks/useSelectedVersion", () => ({ |
| 73 | + default: vi.fn(() => undefined), |
| 74 | +})); |
| 75 | + |
| 76 | +const { useDagRunServiceGetDagRun } = await import("openapi/queries"); |
| 77 | + |
| 78 | +const mockRunData = { |
| 79 | + bundle_version: null, |
| 80 | + conf: null, |
| 81 | + dag_display_name: "test_dag", |
| 82 | + dag_id: "test_dag", |
| 83 | + dag_versions: [dagVersionV1, dagVersionV2], |
| 84 | + end_date: null, |
| 85 | + has_missed_deadline: false, |
| 86 | + logical_date: null, |
| 87 | + note: null, |
| 88 | + partition_key: null, |
| 89 | + queued_at: null, |
| 90 | + run_after: "2025-01-01T00:00:00Z", |
| 91 | + run_id: "run_1", |
| 92 | + run_type: "manual" as const, |
| 93 | + start_date: null, |
| 94 | + state: "success" as const, |
| 95 | + triggered_by: "ui" as const, |
| 96 | + triggering_user_name: null, |
| 97 | +}; |
| 98 | + |
| 99 | +const getItems = (container: HTMLElement) => container.querySelectorAll(".chakra-select__item"); |
| 100 | + |
| 101 | +describe("DagVersionSelect", () => { |
| 102 | + it("shows all versions when no DagRun is selected", () => { |
| 103 | + mockParams = { dagId: "test_dag" }; |
| 104 | + const { container } = render(<DagVersionSelect />, { wrapper: Wrapper }); |
| 105 | + |
| 106 | + expect(getItems(container)).toHaveLength(3); |
| 107 | + }); |
| 108 | + |
| 109 | + it("shows only the selected run's versions when a DagRun is selected", () => { |
| 110 | + mockParams = { dagId: "test_dag", runId: "run_1" }; |
| 111 | + vi.mocked(useDagRunServiceGetDagRun).mockReturnValue({ |
| 112 | + data: mockRunData, |
| 113 | + isLoading: false, |
| 114 | + } as ReturnType<typeof useDagRunServiceGetDagRun>); |
| 115 | + |
| 116 | + const { container } = render(<DagVersionSelect />, { wrapper: Wrapper }); |
| 117 | + |
| 118 | + expect(getItems(container)).toHaveLength(2); |
| 119 | + }); |
| 120 | +}); |
0 commit comments