From 786b198f4d41fd97e940d6a215fd789f4460c018 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 22 Sep 2021 10:31:53 +0200 Subject: [PATCH] Create an updateinput controller - that update the targets input value to the value stored in data attribute - Add tests --- .../controllers/updateinput_controller.js | 13 ++++++++ .../stimulus/update_controller_test.js | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 app/webpacker/controllers/updateinput_controller.js create mode 100644 spec/javascripts/stimulus/update_controller_test.js 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"); + }); + }); +});