diff --git a/app/webpacker/controllers/updateinput_controller.js b/app/webpacker/controllers/updateinput_controller.js new file mode 100644 index 0000000000..aa72ae2086 --- /dev/null +++ b/app/webpacker/controllers/updateinput_controller.js @@ -0,0 +1,13 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["input"]; + + update(event) { + const value = event.currentTarget.dataset.updateinputValue; + + this.inputTargets.forEach((t) => { + t.value = value; + }); + } +} diff --git a/spec/javascripts/stimulus/update_controller_test.js b/spec/javascripts/stimulus/update_controller_test.js new file mode 100644 index 0000000000..33c85645c7 --- /dev/null +++ b/spec/javascripts/stimulus/update_controller_test.js @@ -0,0 +1,30 @@ +/** + * @jest-environment jsdom + */ + +import { Application } from "stimulus"; +import updateinput_controller from "../../../app/webpacker/controllers/updateinput_controller"; + +describe("updateInput controller", () => { + describe("#update", () => { + beforeEach(() => { + document.body.innerHTML = `
`; + + const application = Application.start(); + application.register("updateinput", updateinput_controller); + }); + + it("update the input value", () => { + const submit = document.getElementById("submit"); + const input = document.getElementById("input"); + expect(input.value).toBe("false"); + + submit.click(); + + expect(input.value).toBe("true"); + }); + }); +});