Skip to content

Commit

Permalink
12632 - add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
chahmedejaz committed Jul 2, 2024
1 parent 02c0c6a commit d52a559
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion spec/javascripts/stimulus/toggle_control_controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ describe("ToggleControlController", () => {
</div>`;
});

it("Enables when input is filled", () => {
it("Enables when input is filled and focuses the control", () => {
input.value = "a"
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(false);
expect(document.activeElement).toBe(control);
});

it("Disables when input is emptied", () => {
Expand All @@ -88,6 +89,37 @@ describe("ToggleControlController", () => {
expect(control.disabled).toBe(true);
});
});
describe("with button as control target", () => {
beforeEach(() => {
document.body.innerHTML = `<div data-controller="toggle-control">
<input id="input" value="" data-action="input->toggle-control#enableIfPresent" />
<button id="control" data-toggle-control-target="control">
</div>`;
});

it("Enables the button control when input is filled, focus remains on input", () => {
// Simulating click on input to focus it
input.focus();
input.value = "test"
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(false);
expect(document.activeElement).toBe(input);
});

it("Disables the button control when input is filled, focus remains on input", () => {
// Simulating click on input to focus it
input.focus();
input.value = "test"
input.dispatchEvent(new Event("input"));

input.value = ""
input.dispatchEvent(new Event("input"));

expect(control.disabled).toBe(true);
expect(document.activeElement).toBe(input);
});
})
});

describe("#displayIfMatch", () => {
Expand Down

0 comments on commit d52a559

Please sign in to comment.