Files
openfoodnetwork/spec/javascripts/stimulus/tabs_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

70 lines
2.5 KiB
JavaScript

/**
* @jest-environment jsdom
*/
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 = `
<div data-controller="tabs">
<button data-tabs-target="tab" data-action="click->tabs#select">Dogs</button>
<button data-tabs-target="tab" data-action="click->tabs#select">Cats</button>
<button data-tabs-target="tab" data-action="click->tabs#select">Birds</button>
<div class="content-area" data-tabs-target="content" >
Dogs content
</div>
<div class="content-area" data-tabs-target="content" >
Cats content
</div>
<div class="content-area" data-tabs-target="content" >
Birds content
</div>
</div>
`;
});
it("shows the corresponding content when a tab button is clicked", () => {
const dogs_button = document.querySelectorAll('button')[0];
const cats_button = document.querySelectorAll('button')[1];
const birds_button = document.querySelectorAll('button')[2];
const dogs_content = document.querySelectorAll('.content-area')[0];
const cats_content = document.querySelectorAll('.content-area')[1];
const birds_content = document.querySelectorAll('.content-area')[2];
expect(dogs_content.hidden).toBe(false);
expect(cats_content.hidden).toBe(true);
expect(birds_content.hidden).toBe(true);
expect(document.querySelectorAll('button.active').length).toBe(1);
expect(document.querySelectorAll('button.active')[0]).toBe(dogs_button);
birds_button.click();
expect(dogs_content.hidden).toBe(true);
expect(cats_content.hidden).toBe(true);
expect(birds_content.hidden).toBe(false);
expect(document.querySelectorAll('button.active').length).toBe(1);
expect(document.querySelectorAll('button.active')[0]).toBe(birds_button);
cats_button.click();
expect(dogs_content.hidden).toBe(true);
expect(cats_content.hidden).toBe(false);
expect(birds_content.hidden).toBe(true);
expect(document.querySelectorAll('button.active').length).toBe(1);
expect(document.querySelectorAll('button.active')[0]).toBe(cats_button);
});
});
});