The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix flaky transform inline settings e2e and move its coverage to unit tests (#85603)
Andersona6dd59c845fd
diff --git a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.tsindex e0882701f2d..56d5b277411 100644--- a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts@@ -794,288 +794,24 @@ LIMIT});describe("settings inline editing", () => {- it("should manage ownership, update incremental settings inline, debounce no-op toggles, and roll back on errors", () => {+ it("should update source strategy and checkpoint field inline", () => {createMbqlTransform({ visitTransform: true });H.DataStudio.Transforms.settingsTab().click();- const dismissToast = () => {- H.undoToast().icon("close").click();- H.undoToast().should("not.exist");- };-- cy.log("verify the ownership section is displayed");- getTransformsTargetContent().within(() => {- cy.findByText("Ownership").should("be.visible");- cy.findByText("Specify who is responsible for this transform.").should(- "be.visible",- );- cy.findByText("Owner").should("be.visible");- });-- cy.log("change the owner to another user");- getTransformsTargetContent().within(() => {- cy.findByLabelText("Owner").click();- });- H.popover().findByText("Robert Tableton").click();- cy.wait("@updateTransform");- H.undoToast().findByText("Transform owner updated").should("be.visible");- dismissToast();-- cy.log("set an external email as owner");- getTransformsTargetContent().within(() => {- cy.findByLabelText("Owner").click();- cy.findByLabelText("Owner").clear().type("[email protected]");- });- H.popover().findByText("[email protected]").click();- cy.wait("@updateTransform");- H.undoToast().findByText("Transform owner updated").should("be.visible");- dismissToast();-- cy.log("clear the owner");- getTransformsTargetContent().within(() => {- cy.findByLabelText("Owner").click();- });- H.popover().findByText("No owner").click();- cy.wait("@updateTransform");- H.undoToast().findByText("Transform owner updated").should("be.visible");- dismissToast();-- cy.log("Toggle incremental on");- isIncrementalSwitchDisabled();- getIncrementalSwitch().click();- cy.wait("@updateTransform");- isIncrementalSwitchEnabled();- H.undoToast().should(- "contain.text",- "Incremental transformation settings updated",- );- dismissToast();-- cy.log("Toggle incremental off");- getIncrementalSwitch().click();- cy.wait("@updateTransform");- isIncrementalSwitchDisabled();- H.undoToast().should(- "contain.text",- "Incremental transformation settings updated",- );- dismissToast();-- cy.log("Toggle incremental on and immediately off");- isIncrementalSwitchDisabled();-- // Stub the updateTransform call to track how many times it's called- let updateCallCount = 0;- cy.intercept("PUT", "/api/transform/*", (req) => {- updateCallCount++;- req.continue();- });-- // Toggle on then immediately off (within debounce window)- getIncrementalSwitch().click();- getIncrementalSwitch().click();-- // Wait for debounce period (300ms) plus some buffer- cy.wait(500);-- cy.log(- "Verify no request was made since we toggled back to original value",- );- // The switch should be back to unchecked- isIncrementalSwitchDisabled();- // No request should have been made- cy.wrap(null).then(() => {- expect(updateCallCount).to.equal(0);- });-- cy.log("Intercept and force the update to fail");- cy.intercept("PUT", "/api/transform/*", {- statusCode: 500,- body: { message: "Internal server error" },- }).as("updateTransformError");-- cy.log("Toggle incremental on");+ cy.log("enable incremental once its checkpoint options have loaded");getIncrementalSwitch().findByRole("switch").should("be.enabled").click();-- cy.log("Wait for the failed request");- cy.wait("@updateTransformError");-- cy.log("Verify error toast is shown");- H.undoToast().should(- "contain.text",- "Failed to update incremental transformation settings",- );-- cy.log("Verify the switch rolled back to unchecked state");- isIncrementalSwitchDisabled();- dismissToast();-- cy.log("Intercept and simulate network failure");- cy.intercept("PUT", "/api/transform/*", {- forceNetworkError: true,- }).as("updateTransformNetworkError");-- cy.log("Toggle incremental on");- getIncrementalSwitch().click();- cy.wait("@updateTransformNetworkError");-- cy.log("Verify error toast is shown");- H.undoToast().should(- "contain.text",- "Failed to update incremental transformation settings",- );-- cy.log("Verify the switch rolled back to unchecked state");- isIncrementalSwitchDisabled();- dismissToast();-- let requestCount = 0;- cy.log("Intercept and fail the first request after a delay");- cy.intercept("PUT", "/api/transform/*", (req) => {- requestCount++;- if (requestCount === 1) {- // First request fails after a delay to ensure second change happens while it's in progress- req.reply({- statusCode: 500,- body: { message: "Internal server error" },- delay: 500,- });- } else {- // Subsequent requests should not happen- req.continue();- }- }).as("updateTransformConditional");-- cy.log("Toggle incremental on (first change)");- getIncrementalSwitch().click();-- cy.log("Wait for debounce, then toggle again (second change)");- cy.wait(400);- getIncrementalSwitch().click();-- cy.log("Wait for the error");- cy.wait("@updateTransformConditional");-- cy.log("Wait a bit to ensure no second request is made");- cy.wait(500);-- cy.log("Verify only one request was made");- cy.wrap(null).then(() => {- expect(requestCount).to.equal(1);- });-- cy.log("Verify error toast is shown");- H.undoToast().should(- "contain.text",- "Failed to update incremental transformation settings",- );-- cy.log("Verify the switch is back to unchecked");- isIncrementalSwitchDisabled();- });-- it("should handle sequential changes correctly when first update is in progress", () => {- // Use composite_pk_table which has at least two numeric fields (id1, score)- // so we can reliably select a second checkpoint field- H.resetTestTable({ type: "postgres", table: "composite_pk_table" });- H.resyncDatabase({- dbId: WRITABLE_DB_ID,- tableName: "composite_pk_table",- });- createMbqlTransform({- sourceTable: "composite_pk_table",- visitTransform: true,+ cy.wait("@updateTransform").then(({ request, response }) => {+ expect(response?.statusCode).to.eq(200);+ expect(request.body.target.type).to.eq("table-incremental");+ expect(request.body.source["source-incremental-strategy"]).to.include({+ type: "checkpoint",+ });});- H.DataStudio.Transforms.settingsTab().click();-- cy.log("Verify initial state");- isIncrementalSwitchDisabled();-- let requestCount = 0;-- // Intercept and delay the first request using a Promise- cy.intercept("PUT", "/api/transform/*", (req) => {- requestCount++;- if (requestCount === 1) {- // Delay the first request by 1 second- req.reply({- body: req.body,- delay: 1000,- });- } else {- // Let subsequent requests through normally- req.continue();- }- }).as("updateTransformDelayed");-- cy.log("Toggle incremental on (first change)");- getIncrementalSwitch().click();-- cy.log(- "Wait for debounce plus a bit, then select checkpoint field (second change)",- );- // Wait for first request to start (debounce 300ms + buffer)- cy.wait(400);-- // Make a second change while first is still in progress- // Select the second checkpoint field (composite_pk_table has id1 and score)- getFieldPicker().scrollIntoView().should("be.visible");- getFieldPicker().click();- H.popover().findAllByRole("option").eq(1).click();-- cy.log("Wait for both requests to complete");- cy.wait("@updateTransformDelayed");- cy.wait("@updateTransformDelayed");-- cy.log(- "Verify final state - incremental on with checkpoint field selected",- );+ cy.log("the settings are read back from the server after a reload");+ cy.reload();isIncrementalSwitchEnabled();- // Verify a field was selected (should not show placeholder text)- getFieldPicker().should("not.contain.text", "Pick a field");-- cy.log("Verify both requests were made");- cy.wrap(null).then(() => {- expect(requestCount).to.equal(2);- });- });-- it("should update source strategy and checkpoint field inline", () => {- H.resetTestTable({ type: "postgres", table: "composite_pk_table" });- H.resyncDatabase({- dbId: WRITABLE_DB_ID,- tableName: "composite_pk_table",- });- createMbqlTransform({- sourceTable: "composite_pk_table",- visitTransform: true,- });- H.DataStudio.Transforms.settingsTab().click();-- cy.log("Enable incremental transformation");- getIncrementalSwitch().click();- cy.wait("@updateTransform");-- cy.log("Source strategy and checkpoint field should be visible");- // The source strategy select should be visible- // (Currently only one option "checkpoint" is available, so select might not be shown)- // The checkpoint field select should be visible- getFieldPicker().scrollIntoView().should("be.visible");-- cy.log("Select a checkpoint field");- getFieldPicker().click();- // Click the first available option in the popover- H.popover().findAllByRole("option").eq(1).click();- cy.wait("@updateTransform");- H.undoToast().should(- "contain.text",- "Incremental transformation settings updated",- );-- cy.log("Verify the field was selected");- // Verify a field was selected (should not show placeholder text)- getFieldPicker().should("not.contain.text", "Pick a field");+ getFieldPicker().should("have.value", "Score");});});@@ -3667,9 +3403,6 @@ function getIncrementalSwitch() {function isIncrementalSwitchEnabled() {return getIncrementalSwitch().findByRole("switch").should("be.checked");}-function isIncrementalSwitchDisabled() {- return getIncrementalSwitch().findByRole("switch").should("not.be.checked");-}function getJobTransformTable() {return cy.findByLabelText("Job transforms");diff --git a/frontend/src/metabase/forms/components/FormInlineUpdater/FormInlineUpdater.unit.spec.tsx b/frontend/src/metabase/forms/components/FormInlineUpdater/FormInlineUpdater.unit.spec.tsxindex 65964191bb6..9105b377ba5 100644--- a/frontend/src/metabase/forms/components/FormInlineUpdater/FormInlineUpdater.unit.spec.tsx+++ b/frontend/src/metabase/forms/components/FormInlineUpdater/FormInlineUpdater.unit.spec.tsx@@ -1,10 +1,66 @@import userEvent from "@testing-library/user-event";-import { render, screen, waitFor } from "__support__/ui";+import { act, render, screen, waitFor } from "__support__/ui";import { Form, FormProvider, FormSwitch } from "metabase/forms";+import { defer } from "metabase/utils/promise";import { FormInlineUpdater } from "./FormInlineUpdater";+const DEBOUNCE_MS = 300;++type FormValues = {+ enabled: boolean;+ archived: boolean;+};++type SetupOpts = {+ update: (values: FormValues) => Promise<void>;+};++function setup({ update }: SetupOpts) {+ const onSuccess = jest.fn();+ const onError = jest.fn();+ const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });++ const { unmount } = render(+ <FormProvider+ initialValues={{ enabled: false, archived: false }}+ onSubmit={jest.fn()}+ >+ <Form>+ <FormInlineUpdater+ update={update}+ onSuccess={onSuccess}+ onError={onError}+ debounceMs={DEBOUNCE_MS}+ />+ <FormSwitch name="enabled" label="Enabled" />+ <FormSwitch name="archived" label="Archived" />+ </Form>+ </FormProvider>,+ );++ return {+ user,+ unmount,+ onSuccess,+ onError,+ enabledSwitch: screen.getByRole("switch", { name: "Enabled" }),+ archivedSwitch: screen.getByRole("switch", { name: "Archived" }),+ };+}++function advanceTimers(ms: number) {+ return act(() => jest.advanceTimersByTimeAsync(ms));+}++function settle(settleUpdate: () => void) {+ return act(async () => {+ settleUpdate();+ await jest.advanceTimersByTimeAsync(0);+ });+}+describe("FormInlineUpdater", () => {it("rolls back form values when an update fails", async () => {const error = new Error("Update failed");@@ -120,4 +176,175 @@ describe("FormInlineUpdater", () => {});expect(onError).toHaveBeenCalledWith(error);});++ describe("debounced and queued updates", () => {+ beforeEach(() => {+ jest.useFakeTimers();+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ it("sends one update with the latest values once the debounce delay elapses", async () => {+ const update = jest.fn().mockResolvedValue(undefined);+ const { user, enabledSwitch, archivedSwitch } = setup({ update });++ await user.click(enabledSwitch);+ await user.click(archivedSwitch);+ await advanceTimers(DEBOUNCE_MS - 1);+ expect(update).not.toHaveBeenCalled();++ await advanceTimers(1);+ expect(update).toHaveBeenCalledTimes(1);+ expect(update).toHaveBeenCalledWith({ enabled: true, archived: true });+ });++ it("does not send an update when the values return to the initial values within the debounce delay", async () => {+ const update = jest.fn().mockResolvedValue(undefined);+ const { user, enabledSwitch } = setup({ update });++ await user.click(enabledSwitch);+ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);++ expect(update).not.toHaveBeenCalled();+ expect(enabledSwitch).not.toBeChecked();+ });++ it("sends only the latest values changed while an update is in flight", async () => {+ const firstUpdate = defer<void>();+ const update = jest+ .fn()+ .mockReturnValueOnce(firstUpdate.promise)+ .mockResolvedValue(undefined);+ const { user, onSuccess, enabledSwitch, archivedSwitch } = setup({+ update,+ });++ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(archivedSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ expect(update).toHaveBeenCalledTimes(1);++ await settle(() => firstUpdate.resolve());++ expect(update).toHaveBeenCalledTimes(2);+ expect(update).toHaveBeenLastCalledWith({+ enabled: false,+ archived: true,+ });+ expect(onSuccess).toHaveBeenCalledTimes(2);+ expect(enabledSwitch).not.toBeChecked();+ expect(archivedSwitch).toBeChecked();+ });++ it("discards values queued behind an update that fails", async () => {+ const error = new Error("Update failed");+ const firstUpdate = defer<void>();+ const update = jest+ .fn()+ .mockReturnValueOnce(firstUpdate.promise)+ .mockResolvedValue(undefined);+ const { user, onError, enabledSwitch, archivedSwitch } = setup({+ update,+ });++ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(archivedSwitch);+ await advanceTimers(DEBOUNCE_MS);++ await settle(() => firstUpdate.reject(error));+ await advanceTimers(DEBOUNCE_MS);++ expect(update).toHaveBeenCalledTimes(1);+ expect(onError).toHaveBeenCalledTimes(1);+ expect(enabledSwitch).not.toBeChecked();+ expect(archivedSwitch).not.toBeChecked();+ });++ it("does not send a change still waiting for the debounce delay when the update in flight fails", async () => {+ const firstUpdate = defer<void>();+ const update = jest+ .fn()+ .mockReturnValueOnce(firstUpdate.promise)+ .mockResolvedValue(undefined);+ const { user, enabledSwitch, archivedSwitch } = setup({ update });++ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(archivedSwitch);++ await settle(() => firstUpdate.reject(new Error("Update failed")));+ await advanceTimers(DEBOUNCE_MS);++ expect(update).toHaveBeenCalledTimes(1);+ expect(enabledSwitch).not.toBeChecked();+ expect(archivedSwitch).not.toBeChecked();+ });++ it("does not send a change still waiting for the debounce delay after unmount", async () => {+ const update = jest.fn().mockResolvedValue(undefined);+ const { user, unmount, enabledSwitch } = setup({ update });++ await user.click(enabledSwitch);+ unmount();+ await advanceTimers(DEBOUNCE_MS);++ expect(update).not.toHaveBeenCalled();+ });++ it("discards values queued before unmount when the update in flight fails", async () => {+ const error = new Error("Update failed");+ const firstUpdate = defer<void>();+ const update = jest+ .fn()+ .mockReturnValueOnce(firstUpdate.promise)+ .mockResolvedValue(undefined);+ const { user, unmount, onError, enabledSwitch, archivedSwitch } = setup({+ update,+ });++ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(archivedSwitch);+ await advanceTimers(DEBOUNCE_MS);+ unmount();++ await settle(() => firstUpdate.reject(error));+ await advanceTimers(DEBOUNCE_MS);++ expect(update).toHaveBeenCalledTimes(1);+ expect(onError).toHaveBeenCalledWith(error);+ });++ it("sends values queued before unmount once the update in flight succeeds", async () => {+ const firstUpdate = defer<void>();+ const update = jest+ .fn()+ .mockReturnValueOnce(firstUpdate.promise)+ .mockResolvedValue(undefined);+ const { user, unmount, enabledSwitch, archivedSwitch } = setup({+ update,+ });++ await user.click(enabledSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await user.click(archivedSwitch);+ await advanceTimers(DEBOUNCE_MS);+ unmount();++ await settle(() => firstUpdate.resolve());++ expect(update).toHaveBeenCalledTimes(2);+ expect(update).toHaveBeenLastCalledWith({+ enabled: true,+ archived: true,+ });+ });+ });});diff --git a/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.unit.spec.tsx b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.unit.spec.tsxindex 70c653e9d1c..beb17091a8a 100644--- a/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.unit.spec.tsx+++ b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.unit.spec.tsx@@ -1,11 +1,15 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import {setupDatabaseEndpoints,+ setupUpdateTransformEndpoint,setupUsersEndpoints,} from "__support__/server-mocks";import { renderWithProviders, screen } from "__support__/ui";import { Route } from "metabase/router";import * as Urls from "metabase/urls";-import type { Transform } from "metabase-types/api";+import type { Transform, TransformId } from "metabase-types/api";import {createMockDatabase,createMockTransform,@@ -63,11 +67,20 @@ function setup({/>,{withRouter: true,+ withUndos: true,initialRoute: Urls.transform(transform.id),},);}+function getLastOwnerUpdateBody(transformId: TransformId) {+ const call = fetchMock.callHistory.lastCall(+ `path:/api/transform/${transformId}`,+ { method: "PUT" },+ );+ return JSON.parse(String(call?.options.body));+}+describe("TransformSettingsSection", () => {it("should disable the change target button when the transform is running", () => {setup({@@ -145,4 +158,83 @@ describe("OwnerSection", () => {});expect(screen.getByText("Ownership")).toBeInTheDocument();});++ it("should change the owner to another user", async () => {+ const transform = createMockTransform();+ setupUpdateTransformEndpoint(transform.id, transform);+ setup({ transform });++ await userEvent.click(screen.getByRole("textbox", { name: "Owner" }));+ await userEvent.click(+ await screen.findByRole("option", { name: /Another User/ }),+ );++ expect(+ await screen.findByText("Transform owner updated"),+ ).toBeInTheDocument();+ expect(getLastOwnerUpdateBody(transform.id)).toEqual({+ owner_email: null,+ owner_user_id: 2,+ });+ });++ it("should change the owner to an external email", async () => {+ const transform = createMockTransform();+ setupUpdateTransformEndpoint(transform.id, transform);+ setup({ transform });++ await userEvent.type(+ screen.getByRole("textbox", { name: "Owner" }),+ "[email protected]",+ );+ await userEvent.click(await screen.findByText("[email protected]"));++ expect(+ await screen.findByText("Transform owner updated"),+ ).toBeInTheDocument();+ expect(getLastOwnerUpdateBody(transform.id)).toEqual({+ owner_email: "[email protected]",+ owner_user_id: null,+ });+ });++ it("should clear the owner", async () => {+ const transform = createMockTransform({+ owner_user_id: 1,+ owner: createMockTransformOwner({ id: 1 }),+ });+ setupUpdateTransformEndpoint(transform.id, transform);+ setup({ transform });++ await userEvent.click(screen.getByRole("textbox", { name: "Owner" }));+ await userEvent.click(+ await screen.findByRole("option", { name: /No owner/ }),+ );++ expect(+ await screen.findByText("Transform owner updated"),+ ).toBeInTheDocument();+ expect(getLastOwnerUpdateBody(transform.id)).toEqual({+ owner_email: null,+ owner_user_id: null,+ });+ });++ it("should show an error toast when the owner update fails", async () => {+ const transform = createMockTransform();+ setupUpdateTransformEndpoint(transform.id, { status: 500 });+ setup({ transform });++ await userEvent.click(screen.getByRole("textbox", { name: "Owner" }));+ await userEvent.click(+ await screen.findByRole("option", { name: /Another User/ }),+ );++ expect(+ await screen.findByText("Failed to update transform owner"),+ ).toBeInTheDocument();+ expect(+ screen.queryByText("Transform owner updated"),+ ).not.toBeInTheDocument();+ });});diff --git a/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateIncrementalSettings.unit.spec.tsx b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateIncrementalSettings.unit.spec.tsxnew file mode 100644index 00000000000..cd679bfd8d5--- /dev/null+++ b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateIncrementalSettings.unit.spec.tsx@@ -0,0 +1,269 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupAdhocQueryMetadataEndpoint,+ setupFieldEndpoints,+ setupGetTransformEndpoint,+ setupUpdateTransformEndpoint,+} from "__support__/server-mocks";+import { act, renderWithProviders, screen, waitFor } from "__support__/ui";+import { useGetTransformQuery } from "metabase/api";+import { type Deferred, defer } from "metabase/utils/promise";+import type { Transform, TransformId } from "metabase-types/api";+import {+ createMockCardQueryMetadata,+ createMockStructuredDatasetQuery,+ createMockTransform,+} from "metabase-types/api/mocks";+import {+ ORDERS,+ ORDERS_ID,+ SAMPLE_DB_ID,+ createOrdersTable,+ createSampleDatabase,+} from "metabase-types/api/mocks/presets";++import { UpdateIncrementalSettings } from "./UpdateIncrementalSettings";++const DEBOUNCE_MS = 300;+const TOAST_MOUNT_DELAY_MS = 1;+const SUCCESS_MESSAGE = "Incremental transformation settings updated";+const ERROR_MESSAGE = "Failed to update incremental transformation settings";++type UpdateResponse =+ | Transform+ | { status: number; body: { message: string } }+ | { throws: Error };++type PendingUpdate = {+ body: Partial<Transform>;+ response: Deferred<UpdateResponse>;+};++const SERVER_ERROR_RESPONSE = {+ status: 500,+ body: { message: "Internal server error" },+};++const pendingUpdates: PendingUpdate[] = [];++function TransformIncrementalSettings({+ transformId,+}: {+ transformId: TransformId;+}) {+ const { data: transform } = useGetTransformQuery(transformId);+ return transform != null ? (+ <UpdateIncrementalSettings transform={transform} />+ ) : null;+}++async function setup() {+ const transform = createMockTransform({+ source: {+ type: "query",+ query: createMockStructuredDatasetQuery({+ database: SAMPLE_DB_ID,+ query: { "source-table": ORDERS_ID },+ }),+ },+ });+ let serverTransform = transform;++ setupAdhocQueryMetadataEndpoint(+ createMockCardQueryMetadata({ databases: [createSampleDatabase()] }),+ );+ createOrdersTable().fields?.forEach((field) => setupFieldEndpoints(field));+ setupGetTransformEndpoint(transform, () => serverTransform);+ setupUpdateTransformEndpoint(transform.id, (call) => {+ const response = defer<UpdateResponse>();+ pendingUpdates.push({+ body: JSON.parse(String(call.options.body)),+ response,+ });+ return response.promise;+ });++ const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });+ renderWithProviders(+ <TransformIncrementalSettings transformId={transform.id} />,+ { withUndos: true },+ );++ const incrementalSwitch = await screen.findByRole("switch", {+ name: /Only process new data/,+ });+ await waitFor(() => expect(incrementalSwitch).toBeEnabled());++ const settleNextUpdate = (+ getResponse: (body: Partial<Transform>) => UpdateResponse,+ ) =>+ act(async () => {+ const pendingUpdate = pendingUpdates.shift();+ expect(pendingUpdate).toBeDefined();+ if (pendingUpdate != null) {+ pendingUpdate.response.resolve(getResponse(pendingUpdate.body));+ }+ await jest.advanceTimersByTimeAsync(0);+ });++ return {+ transform,+ user,+ incrementalSwitch,+ succeedNextUpdate: () =>+ settleNextUpdate((body) => {+ serverTransform = { ...serverTransform, ...body };+ return serverTransform;+ }),+ failNextUpdate: () => settleNextUpdate(() => SERVER_ERROR_RESPONSE),+ dropNextUpdate: () =>+ settleNextUpdate(() => ({ throws: new TypeError("Failed to fetch") })),+ };+}++function advanceTimers(ms: number) {+ return act(() => jest.advanceTimersByTimeAsync(ms));+}++function getUpdateRequestBodies(transformId: TransformId) {+ return fetchMock.callHistory+ .calls(`path:/api/transform/${transformId}`, { method: "PUT" })+ .map((call) => JSON.parse(String(call.options.body)));+}++function getCheckpointFieldInput() {+ return screen.getByRole("textbox", {+ name: "Field to check for new values",+ });+}++async function selectCheckpointField(+ user: ReturnType<typeof userEvent.setup>,+ name: string,+) {+ await user.click(getCheckpointFieldInput());+ await user.click(await screen.findByRole("option", { name }));+}++describe("UpdateIncrementalSettings", () => {+ beforeEach(() => {+ jest.useFakeTimers();+ });++ afterEach(() => {+ pendingUpdates+ .splice(0)+ .forEach(({ response }) => response.resolve(SERVER_ERROR_RESPONSE));+ jest.useRealTimers();+ });++ it("saves incremental settings with the first checkpoint field once the debounce delay elapses", async () => {+ const { transform, user, incrementalSwitch, succeedNextUpdate } =+ await setup();++ await user.click(incrementalSwitch);+ await advanceTimers(DEBOUNCE_MS - 1);+ expect(getUpdateRequestBodies(transform.id)).toHaveLength(0);++ await advanceTimers(1);+ expect(getUpdateRequestBodies(transform.id)).toEqual([+ expect.objectContaining({+ source: expect.objectContaining({+ "source-incremental-strategy": expect.objectContaining({+ type: "checkpoint",+ "checkpoint-filter-field-id": ORDERS.CREATED_AT,+ }),+ }),+ target: expect.objectContaining({+ type: "table-incremental",+ "target-incremental-strategy": { type: "append" },+ }),+ }),+ ]);++ await succeedNextUpdate();++ expect(await screen.findByText(SUCCESS_MESSAGE)).toBeInTheDocument();+ expect(incrementalSwitch).toBeChecked();+ expect(getCheckpointFieldInput()).toHaveValue("Created At");+ });++ it("shows the error toast and rolls back the switch together when the update fails", async () => {+ const { user, incrementalSwitch, failNextUpdate } = await setup();++ await user.click(incrementalSwitch);+ await advanceTimers(DEBOUNCE_MS);+ expect(incrementalSwitch).toBeChecked();+ expect(screen.queryByText(ERROR_MESSAGE)).not.toBeInTheDocument();++ await failNextUpdate();+ expect(incrementalSwitch).not.toBeChecked();++ await advanceTimers(TOAST_MOUNT_DELAY_MS);+ expect(screen.getByText(ERROR_MESSAGE)).toBeInTheDocument();+ });++ it("shows the error toast and rolls back the switch together when the network fails", async () => {+ const { user, incrementalSwitch, dropNextUpdate } = await setup();++ await user.click(incrementalSwitch);+ await advanceTimers(DEBOUNCE_MS);+ expect(incrementalSwitch).toBeChecked();+ expect(screen.queryByText(ERROR_MESSAGE)).not.toBeInTheDocument();++ await dropNextUpdate();+ expect(incrementalSwitch).not.toBeChecked();++ await advanceTimers(TOAST_MOUNT_DELAY_MS);+ expect(screen.getByText(ERROR_MESSAGE)).toBeInTheDocument();+ });++ it("does not save a checkpoint field picked while a failing update is in flight", async () => {+ const { transform, user, incrementalSwitch, failNextUpdate } =+ await setup();++ await user.click(incrementalSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await selectCheckpointField(user, "Total");+ await advanceTimers(DEBOUNCE_MS);++ await failNextUpdate();+ await advanceTimers(DEBOUNCE_MS);++ expect(getUpdateRequestBodies(transform.id)).toHaveLength(1);+ expect(screen.getByText(ERROR_MESSAGE)).toBeInTheDocument();+ expect(incrementalSwitch).not.toBeChecked();+ });++ it("saves only the latest checkpoint field picked while an update is in flight", async () => {+ const { transform, user, incrementalSwitch, succeedNextUpdate } =+ await setup();++ await user.click(incrementalSwitch);+ await advanceTimers(DEBOUNCE_MS);+ await selectCheckpointField(user, "Total");+ await advanceTimers(DEBOUNCE_MS);+ await selectCheckpointField(user, "Tax");+ await advanceTimers(DEBOUNCE_MS);+ expect(getUpdateRequestBodies(transform.id)).toHaveLength(1);++ await succeedNextUpdate();+ expect(getUpdateRequestBodies(transform.id)).toHaveLength(2);+ expect(getUpdateRequestBodies(transform.id)[1]).toEqual(+ expect.objectContaining({+ source: expect.objectContaining({+ "source-incremental-strategy": expect.objectContaining({+ "checkpoint-filter-field-id": ORDERS.TAX,+ }),+ }),+ }),+ );++ await succeedNextUpdate();++ expect(getCheckpointFieldInput()).toHaveValue("Tax");+ expect(incrementalSwitch).toBeChecked();+ });+});diff --git a/frontend/test/__support__/server-mocks/transform.ts b/frontend/test/__support__/server-mocks/transform.tsindex d7de2410524..fd36cdcfbac 100644--- a/frontend/test/__support__/server-mocks/transform.ts+++ b/frontend/test/__support__/server-mocks/transform.ts@@ -1,4 +1,4 @@-import fetchMock from "fetch-mock";+import fetchMock, { type RouteResponse } from "fetch-mock";import type {DagTransform,@@ -18,6 +18,8 @@ import type {TransformTag,} from "metabase-types/api";+import type { MockResponse } from "./types";+export function setupListTransformRunsEndpoint(response: ListTransformRunsResponse,) {@@ -34,8 +36,18 @@ export function setupListTransformsEndpoint(transforms: Transform[]) {fetchMock.get(`path:/api/transform`, transforms);}-export function setupGetTransformEndpoint(transform: Transform) {- fetchMock.get(`path:/api/transform/${transform.id}`, transform);+export function setupGetTransformEndpoint(+ transform: Transform,+ response: MockResponse<Transform> = transform,+) {+ fetchMock.get(`path:/api/transform/${transform.id}`, response);+}++export function setupUpdateTransformEndpoint(+ transformId: TransformId,+ response: RouteResponse,+) {+ fetchMock.put(`path:/api/transform/${transformId}`, response);}export function setupDeleteTransformEndpoint(transformId: TransformId) {