diff --git a/airbyte-webapp/src/components/connection/ConnectionForm/SyncCatalogTable/SyncCatalogTable.tsx b/airbyte-webapp/src/components/connection/ConnectionForm/SyncCatalogTable/SyncCatalogTable.tsx index fe46c1b449e..5304105f022 100644 --- a/airbyte-webapp/src/components/connection/ConnectionForm/SyncCatalogTable/SyncCatalogTable.tsx +++ b/airbyte-webapp/src/components/connection/ConnectionForm/SyncCatalogTable/SyncCatalogTable.tsx @@ -41,7 +41,7 @@ import { TableControls } from "./components/TableControls"; import { useInitialRowIndex } from "./hooks/useInitialRowIndex"; import styles from "./SyncCatalogTable.module.scss"; import { getRowChangeStatus, getSyncCatalogRows, isStreamRow } from "./utils"; -import { FormConnectionFormValues, SyncStreamFieldWithId } from "../formConfig"; +import { FormConnectionFormValues, SyncStreamFieldWithId, useInitialFormValues } from "../formConfig"; export interface SyncCatalogTableProps { /** @@ -93,7 +93,8 @@ export interface SyncCatalogUIModel { export const SyncCatalogTable: FC = ({ scrollParentContainer }) => { const { formatMessage } = useIntl(); - const { mode, initialValues } = useConnectionFormService(); + const { mode, connection } = useConnectionFormService(); + const initialValues = useInitialFormValues(connection, mode); const { control, trigger } = useFormContext(); const { fields: streams, update } = useFieldArray({ name: "syncCatalog.streams", diff --git a/airbyte-webapp/src/components/connection/CreateConnectionForm/CreateConnectionForm.tsx b/airbyte-webapp/src/components/connection/CreateConnectionForm/CreateConnectionForm.tsx index 6d68bda57fc..a2cccae05a1 100644 --- a/airbyte-webapp/src/components/connection/CreateConnectionForm/CreateConnectionForm.tsx +++ b/airbyte-webapp/src/components/connection/CreateConnectionForm/CreateConnectionForm.tsx @@ -21,7 +21,11 @@ import { SchemaError } from "./SchemaError"; import { SimplifiedConnectionConfiguration } from "./SimplifiedConnectionCreation/SimplifiedConnectionConfiguration"; import { useAnalyticsTrackFunctions } from "./useAnalyticsTrackFunctions"; import { ScrollableContainer } from "../../ScrollableContainer"; -import { FormConnectionFormValues, useConnectionValidationSchema } from "../ConnectionForm/formConfig"; +import { + FormConnectionFormValues, + useConnectionValidationSchema, + useInitialFormValues, +} from "../ConnectionForm/formConfig"; export const CREATE_CONNECTION_FORM_ID = "create-connection-form"; @@ -29,7 +33,8 @@ const CreateConnectionFormInner: React.FC = () => { const navigate = useNavigate(); const { clearAllFormChanges } = useFormChangeTrackerService(); const { mutateAsync: createConnection } = useCreateConnection(); - const { connection, initialValues, setSubmitError } = useConnectionFormService(); + const { connection, mode, setSubmitError } = useConnectionFormService(); + const initialValues = useInitialFormValues(connection, mode); const { registerNotification } = useNotificationService(); const { formatMessage } = useIntl(); useExperimentContext("source-definition", connection.source?.sourceDefinitionId); diff --git a/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.test.tsx b/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.test.tsx index c0134daa9c7..27fe73e3ada 100644 --- a/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.test.tsx +++ b/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.test.tsx @@ -1,9 +1,9 @@ import { renderHook } from "@testing-library/react"; -import { FormConnectionFormValues } from "components/connection/ConnectionForm/formConfig"; +import { FormConnectionFormValues, useInitialFormValues } from "components/connection/ConnectionForm/formConfig"; +import { mocked } from "test-utils"; import { AirbyteStreamAndConfiguration } from "core/api/types/AirbyteClient"; -import * as connectionFormService from "hooks/services/ConnectionForm/ConnectionFormService"; import { useStreamsConfigTableRowProps } from "./useStreamsConfigTableRowProps"; @@ -15,7 +15,9 @@ const mockStream: Partial = { config: { selected: true, syncMode: "full_refresh", destinationSyncMode: "overwrite" }, }; -const mockInitialValues: Partial = { +const mockInitialValues: FormConnectionFormValues = { + name: "connection_name", + scheduleType: "manual", syncCatalog: { streams: [ { @@ -27,30 +29,34 @@ const mockInitialValues: Partial = { }, ], }, + namespaceDefinition: "source", + prefix: "", }; -const mockDisabledInitialValues: Partial = { +const mockDisabledInitialValues: FormConnectionFormValues = { + ...mockInitialValues, syncCatalog: { streams: [ { ...mockInitialValues.syncCatalog?.streams[0], // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - config: { ...mockInitialValues.syncCatalog!.streams[0].config!, selected: false }, + config: { ...mockInitialValues.syncCatalog.streams[0].config!, selected: false }, }, ], }, }; -const testSetup = (initialValues: Partial) => { - jest.spyOn(connectionFormService, "useConnectionFormService").mockImplementation(() => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return { initialValues, mode: "edit" } as any; - }); -}; +jest.mock("components/connection/ConnectionForm/formConfig", () => ({ + useInitialFormValues: jest.fn().mockReturnValue({}), +})); + +jest.mock("hooks/services/ConnectionForm/ConnectionFormService", () => ({ + useConnectionFormService: () => ({ mode: "edit" }), +})); describe(`${useStreamsConfigTableRowProps.name}`, () => { it("should return default styles for a row that starts enabled", () => { - testSetup(mockInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps(mockStream)); @@ -58,7 +64,7 @@ describe(`${useStreamsConfigTableRowProps.name}`, () => { expect(result.current.pillButtonVariant).toEqual("grey"); }); it("should return disabled styles for a row that starts disabled", () => { - testSetup(mockDisabledInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockDisabledInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps({ @@ -72,7 +78,7 @@ describe(`${useStreamsConfigTableRowProps.name}`, () => { expect(result.current.pillButtonVariant).toEqual("grey"); }); it("should return added styles for a row that is added", () => { - testSetup(mockDisabledInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockDisabledInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps(mockStream)); @@ -80,7 +86,7 @@ describe(`${useStreamsConfigTableRowProps.name}`, () => { expect(result.current.pillButtonVariant).toEqual("green"); }); it("should return removed styles for a row that is removed", () => { - testSetup(mockInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps({ @@ -94,7 +100,7 @@ describe(`${useStreamsConfigTableRowProps.name}`, () => { expect(result.current.pillButtonVariant).toEqual("red"); }); it("should return updated styles for a row that is updated", () => { - testSetup(mockInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps({ @@ -109,7 +115,7 @@ describe(`${useStreamsConfigTableRowProps.name}`, () => { }); it("should return added styles for a row that is both added and updated", () => { - testSetup(mockDisabledInitialValues); + mocked(useInitialFormValues).mockReturnValueOnce(mockDisabledInitialValues); const { result } = renderHook(() => useStreamsConfigTableRowProps({ diff --git a/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.tsx b/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.tsx index 733b6b3122f..fec04c5e368 100644 --- a/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.tsx +++ b/airbyte-webapp/src/components/connection/syncCatalog/StreamsConfigTable/useStreamsConfigTableRowProps.tsx @@ -2,6 +2,7 @@ import classNames from "classnames"; import { useMemo } from "react"; +import { useInitialFormValues } from "components/connection/ConnectionForm/formConfig"; import { PillButtonVariant } from "components/ui/PillListBox/PillButton"; import { AirbyteStreamAndConfiguration, AirbyteStreamConfiguration } from "core/api/types/AirbyteClient"; @@ -14,7 +15,8 @@ import { compareObjectsByFields } from "../utils"; export type StatusToDisplay = "disabled" | "added" | "removed" | "changed" | "unchanged"; export const useStreamsConfigTableRowProps = (stream: AirbyteStreamAndConfiguration) => { - const { initialValues, mode } = useConnectionFormService(); + const { connection, mode } = useConnectionFormService(); + const initialValues = useInitialFormValues(connection, mode); const isStreamEnabled = stream.config?.selected; diff --git a/airbyte-webapp/src/hooks/services/ConnectionForm/ConnectionFormService.tsx b/airbyte-webapp/src/hooks/services/ConnectionForm/ConnectionFormService.tsx index 698db52e627..3c39d0fcdd6 100644 --- a/airbyte-webapp/src/hooks/services/ConnectionForm/ConnectionFormService.tsx +++ b/airbyte-webapp/src/hooks/services/ConnectionForm/ConnectionFormService.tsx @@ -2,7 +2,7 @@ import React, { createContext, useCallback, useContext, useState } from "react"; import { FieldErrors } from "react-hook-form"; import { useIntl } from "react-intl"; -import { FormConnectionFormValues, useInitialFormValues } from "components/connection/ConnectionForm/formConfig"; +import { FormConnectionFormValues } from "components/connection/ConnectionForm/formConfig"; import { ExternalLink } from "components/ui/Link"; import { HttpProblem } from "core/api"; @@ -27,7 +27,6 @@ interface ConnectionServiceProps { interface ConnectionFormHook { connection: ConnectionOrPartialConnection; mode: ConnectionFormMode; - initialValues: FormConnectionFormValues; schemaError?: Error | null; refreshSchema: () => Promise; setSubmitError: (submitError: FormError | null) => void; @@ -41,7 +40,6 @@ const useConnectionForm = ({ refreshSchema, }: ConnectionServiceProps): ConnectionFormHook => { const formatError = useFormatError(); - const initialValues = useInitialFormValues(connection, mode); const { formatMessage } = useIntl(); const [submitError, setSubmitError] = useState(null); @@ -75,7 +73,6 @@ const useConnectionForm = ({ return { connection, mode, - initialValues, schemaError, refreshSchema, setSubmitError, diff --git a/airbyte-webapp/src/pages/connections/ConnectionReplicationPage/ConnectionReplicationPage.tsx b/airbyte-webapp/src/pages/connections/ConnectionReplicationPage/ConnectionReplicationPage.tsx index 84a9df6e933..57c3d3a34f0 100644 --- a/airbyte-webapp/src/pages/connections/ConnectionReplicationPage/ConnectionReplicationPage.tsx +++ b/airbyte-webapp/src/pages/connections/ConnectionReplicationPage/ConnectionReplicationPage.tsx @@ -8,6 +8,7 @@ import { useUnmount } from "react-use"; import { FormConnectionFormValues, useConnectionValidationSchema, + useInitialFormValues, } from "components/connection/ConnectionForm/formConfig"; import { useRefreshSourceSchemaWithConfirmationOnDirty } from "components/connection/ConnectionForm/refreshSourceSchemaWithConfirmationOnDirty"; import { SchemaChangeBackdrop } from "components/connection/ConnectionForm/SchemaChangeBackdrop"; @@ -89,7 +90,9 @@ export const ConnectionReplicationPage: React.FC = () => { const { openModal } = useModalService(); const { connection, schemaRefreshing, updateConnection, discardRefreshedSchema } = useConnectionEditService(); - const { initialValues, schemaError, setSubmitError, refreshSchema, mode } = useConnectionFormService(); + const { schemaError, setSubmitError, refreshSchema, mode } = useConnectionFormService(); + const initialValues = useInitialFormValues(connection, mode); + const { supportsRefreshes: destinationSupportsRefreshes } = useDestinationDefinitionVersion( connection.destination.destinationId );