diff --git a/app/client/cypress/e2e/Regression/ClientSide/Widgets/Switch/Switch_spec.js b/app/client/cypress/e2e/Regression/ClientSide/Widgets/Switch/Switch_spec.js index 30b890e77051..6b0aa7983b09 100644 --- a/app/client/cypress/e2e/Regression/ClientSide/Widgets/Switch/Switch_spec.js +++ b/app/client/cypress/e2e/Regression/ClientSide/Widgets/Switch/Switch_spec.js @@ -154,6 +154,41 @@ describe( _.deployMode.NavigateBacktoEditor(); }); + it("6a. Regression: programmatic defaultSwitchState changes sync isSwitchedOn for dependents (#41566)", function () { + // Bind dependent text widget to Switch1.isSwitchedOn + cy.openPropertyPane("textwidget"); + cy.updateCodeInput( + ".t--property-control-text", + `{{Toggler.isSwitchedOn}}`, + ); + + // Seed default to true and assert dependent reflects it + cy.openPropertyPane("switchwidget"); + cy.get(".t--property-control-defaultstate input").last().then(($el) => { + if (!$el.is(":checked")) cy.wrap($el).click({ force: true }); + }); + cy.get(".t--widget-textwidget") + .scrollIntoView() + .should("contain", "true"); + + // User toggles the Switch once so meta.isSwitchedOn starts overriding the default + cy.get(`${formWidgetsPage.switchWidget} label`).first().click(); + cy.get(".t--widget-textwidget") + .scrollIntoView() + .should("contain", "false"); + + // Programmatic change to defaultSwitchState should now propagate to dependents + cy.openPropertyPane("switchwidget"); + cy.get(".t--property-control-defaultstate input").last().click(); + cy.get(".t--widget-textwidget") + .scrollIntoView() + .should("contain", "true"); + + // Restore binding used by the next test + cy.openPropertyPane("textwidget"); + cy.updateCodeInput(".t--property-control-text", `{{Toggler.isDirty}}`); + }); + it("6. Check isDirty meta property", function () { cy.openPropertyPane("textwidget"); cy.updateCodeInput(".t--property-control-text", `{{Toggler.isDirty}}`); diff --git a/app/client/src/widgets/SwitchWidget/widget/index.tsx b/app/client/src/widgets/SwitchWidget/widget/index.tsx index 58fadb61558a..ee84bc277414 100644 --- a/app/client/src/widgets/SwitchWidget/widget/index.tsx +++ b/app/client/src/widgets/SwitchWidget/widget/index.tsx @@ -432,6 +432,14 @@ class SwitchWidget extends BaseWidget { ) { this.props.updateWidgetMetaProperty("isDirty", false); } + + // Sync meta when isSwitchedOn changes programmatically so dependents don't see a stale value. + if (this.props.isSwitchedOn !== prevProps.isSwitchedOn) { + this.props.updateWidgetMetaProperty( + "isSwitchedOn", + this.props.isSwitchedOn, + ); + } } onChange = (isSwitchedOn: boolean) => {