Files
openfoodnetwork/spec/javascripts/stimulus/remote_toggle_controller_test.js
Cillian O'Ruanaidh 1b8c1bd27a Only initialise Stimulus app once in Jest tests in :beforeAll rather than :beforeEach
Before there was an issue with the remote_toggle_controller tests, which contains two tests. If you commented out one test and ran the other it would pass and vice versa but if both tests were uncommented only the first test would ever pass, the second one would fail. See https://github.com/openfoodfoundation/openfoodnetwork/pull/8805#discussion_r801464322

Co-authored-by: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com>
2022-02-11 10:21:26 +00:00

60 lines
2.0 KiB
JavaScript

/**
* @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 = `
<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>
`;
});
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");
});
});
});