mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-10 23:07:47 +00:00
Add a remote-toggle controller that can toggle elements outside of its scope
This is instead of adding the :data-controller attribute to the div#wrapper because that will wrap pretty much all content on the admin pages. Co-authored-by: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com>
This commit is contained in:
58
spec/javascripts/stimulus/remote_toggle_controller_test.js
Normal file
58
spec/javascripts/stimulus/remote_toggle_controller_test.js
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { Application } from "stimulus";
|
||||
import remote_toggle_controller from "../../../app/webpacker/controllers/remote_toggle_controller";
|
||||
|
||||
describe("RemoteToggleController", () => {
|
||||
describe("#toggle", () => {
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = `
|
||||
<div data-controller="remote-toggle" data-remote-toggle-selector-value="#content">
|
||||
<button id="remote-toggle" data-action="click->remote-toggle#toggle"></button>
|
||||
<button id="remote-toggle-with-chevron" data-action="click->remote-toggle#toggle">
|
||||
<i class="icon-chevron-down" data-remote-toggle-target="chevron"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div id="content">...</div>
|
||||
`;
|
||||
|
||||
const application = Application.start();
|
||||
application.register("remote-toggle", remote_toggle_controller);
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
/* skipping, this test passes when it's the only test in this file but not otherwise? */
|
||||
it.skip("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");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user