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>
This commit is contained in:
Cillian O'Ruanaidh
2022-02-11 10:21:26 +00:00
parent b43a68d717
commit 1b8c1bd27a
6 changed files with 33 additions and 22 deletions

View File

@@ -6,6 +6,11 @@ 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 = `
@@ -17,9 +22,6 @@ describe("RemoteToggleController", () => {
</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", () => {
@@ -36,8 +38,7 @@ describe("RemoteToggleController", () => {
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", () => {
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");