diff --git a/app/views/admin/order_cycles/_order_cycle_top_buttons.html.haml b/app/views/admin/order_cycles/_order_cycle_top_buttons.html.haml index 86ca6b2ae4..2cc89117b0 100644 --- a/app/views/admin/order_cycles/_order_cycle_top_buttons.html.haml +++ b/app/views/admin/order_cycles/_order_cycle_top_buttons.html.haml @@ -1,19 +1,8 @@ - content_for :page_actions do - :javascript - function toggleSettings(){ - if( $('#advanced_settings').is(":visible") ){ - $('button#toggle_settings i').switchClass("icon-chevron-up","icon-chevron-down") - } - else { - $('button#toggle_settings i').switchClass("icon-chevron-down","icon-chevron-up") - } - $("#advanced_settings").slideToggle() - } - - %li - %button#toggle_settings{ onClick: 'toggleSettings()' } + %li{ "data-controller": "remote-toggle", "data-remote-toggle-selector-value": "#advanced_settings" } + %button#toggle_settings{ "data-action": "click->remote-toggle#toggle" } = t('.advanced_settings') - %i.icon-chevron-down + %i.icon-chevron-down{ "data-remote-toggle-target": "chevron" } -#advanced_settings{ hidden: true } +#advanced_settings{ style: "display: none" } = render partial: "/admin/order_cycles/advanced_settings" diff --git a/app/webpacker/controllers/remote_toggle_controller.js b/app/webpacker/controllers/remote_toggle_controller.js new file mode 100644 index 0000000000..25006d8651 --- /dev/null +++ b/app/webpacker/controllers/remote_toggle_controller.js @@ -0,0 +1,17 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["chevron"]; + static values = { selector: String } + + toggle(event) { + if (this.hasChevronTarget) { + this.chevronTarget.classList.toggle("icon-chevron-down") + this.chevronTarget.classList.toggle("icon-chevron-up") + } + + const element = document.querySelector(this.selectorValue) + element.style.display = element.style.display === "none" ? "block" : "none" + } +} + diff --git a/spec/javascripts/stimulus/paymentmethod_controller_test.js b/spec/javascripts/stimulus/paymentmethod_controller_test.js index 09180ac066..b81dbbf605 100644 --- a/spec/javascripts/stimulus/paymentmethod_controller_test.js +++ b/spec/javascripts/stimulus/paymentmethod_controller_test.js @@ -6,13 +6,18 @@ import { Application } from "stimulus"; import paymentmethod_controller from "../../../app/webpacker/controllers/paymentmethod_controller"; describe("PaymentmethodController", () => { + beforeAll(() => { + const application = Application.start(); + application.register("paymentmethod", paymentmethod_controller); + }); + describe("#selectPaymentMethod", () => { beforeEach(() => { document.body.innerHTML = `
- +
`; - - const application = Application.start(); - application.register("paymentmethod", paymentmethod_controller); }); it("fill the right payment container", () => { diff --git a/spec/javascripts/stimulus/remote_toggle_controller_test.js b/spec/javascripts/stimulus/remote_toggle_controller_test.js new file mode 100644 index 0000000000..507ce3827d --- /dev/null +++ b/spec/javascripts/stimulus/remote_toggle_controller_test.js @@ -0,0 +1,59 @@ +/** + * @jest-environment jsdom + */ + +import { Application } from "stimulus"; +import remote_toggle_controller from "../../../app/webpacker/controllers/remote_toggle_controller"; + +describe("RemoteToggleController", () => { + beforeAll(() => { + const application = Application.start(); + application.register("remote-toggle", remote_toggle_controller); + }); + + describe("#toggle", () => { + beforeEach(() => { + document.body.innerHTML = ` +
+ + +
+
...
+ `; + }); + + it("clicking a toggle switches the visibility of the :data-remote-toggle-selector element", () => { + const button = document.getElementById("remote-toggle"); + const content = document.getElementById("content"); + expect(content.style.display).toBe(""); + + button.click(); + + expect(content.style.display).toBe("none"); + + button.click(); + + expect(content.style.display).toBe("block"); + }); + + it("clicking a toggle with a chevron icon switches the visibility of content and the direction of the icon", () => { + const button = document.getElementById("remote-toggle-with-chevron"); + const chevron = button.querySelector("i"); + const content = document.getElementById("content"); + expect(content.style.display).toBe(""); + expect(chevron.className).toBe("icon-chevron-down"); + + button.click(); + + expect(content.style.display).toBe("none"); + expect(chevron.className).toBe("icon-chevron-up"); + + button.click(); + + expect(content.style.display).toBe("block"); + expect(chevron.className).toBe("icon-chevron-down"); + }); + }); +}); diff --git a/spec/javascripts/stimulus/stripe_cards_controller_test.js b/spec/javascripts/stimulus/stripe_cards_controller_test.js index 44679a1e81..36d74ea314 100644 --- a/spec/javascripts/stimulus/stripe_cards_controller_test.js +++ b/spec/javascripts/stimulus/stripe_cards_controller_test.js @@ -6,6 +6,11 @@ import { Application } from "stimulus"; import stripe_cards_controller from "../../../app/webpacker/controllers/stripe_cards_controller"; describe("StripeCardsController", () => { + beforeAll(() => { + const application = Application.start(); + application.register("stripe-cards", stripe_cards_controller); + }); + beforeEach(() => { document.body.innerHTML = `
`; - - const application = Application.start(); - application.register("stripe-cards", stripe_cards_controller); }); describe("#connect", () => { it("initialize with the right display state", () => { diff --git a/spec/javascripts/stimulus/tabs_controller_test.js b/spec/javascripts/stimulus/tabs_controller_test.js index a67296bd0b..fa4ab5d737 100644 --- a/spec/javascripts/stimulus/tabs_controller_test.js +++ b/spec/javascripts/stimulus/tabs_controller_test.js @@ -6,6 +6,11 @@ import { Application } from "stimulus"; import tabs_controller from "../../../app/webpacker/controllers/tabs_controller"; describe("TabsController", () => { + beforeAll(() => { + const application = Application.start(); + application.register("tabs", tabs_controller); + }); + describe("#select", () => { beforeEach(() => { document.body.innerHTML = ` @@ -13,7 +18,7 @@ describe("TabsController", () => { - +
Dogs content
@@ -25,9 +30,6 @@ describe("TabsController", () => { `; - - const application = Application.start(); - application.register("tabs", tabs_controller); }); it("shows the corresponding content when a tab button is clicked", () => { diff --git a/spec/javascripts/stimulus/toggle_controller_test.js b/spec/javascripts/stimulus/toggle_controller_test.js index 7dcd527e16..c60669d1b8 100644 --- a/spec/javascripts/stimulus/toggle_controller_test.js +++ b/spec/javascripts/stimulus/toggle_controller_test.js @@ -6,6 +6,11 @@ import { Application } from "stimulus"; import toggle_controller from "../../../app/webpacker/controllers/toggle_controller"; describe("ToggleController", () => { + beforeAll(() => { + const application = Application.start(); + application.register("toggle", toggle_controller); + }); + describe("#toggle", () => { beforeEach(() => { document.body.innerHTML = `
@@ -14,9 +19,6 @@ describe("ToggleController", () => { content
`; - - const application = Application.start(); - application.register("toggle", toggle_controller); }); it("toggle the content", () => { diff --git a/spec/javascripts/stimulus/update_controller_test.js b/spec/javascripts/stimulus/update_controller_test.js index 33c85645c7..a6c4463944 100644 --- a/spec/javascripts/stimulus/update_controller_test.js +++ b/spec/javascripts/stimulus/update_controller_test.js @@ -6,15 +6,17 @@ import { Application } from "stimulus"; import updateinput_controller from "../../../app/webpacker/controllers/updateinput_controller"; describe("updateInput controller", () => { + beforeAll(() => { + const application = Application.start(); + application.register("updateinput", updateinput_controller); + }); + describe("#update", () => { beforeEach(() => { document.body.innerHTML = `
`; - - const application = Application.start(); - application.register("updateinput", updateinput_controller); }); it("update the input value", () => {