Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(carousel): Prevent unexpected keyboard autoplay activation #9621

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,78 @@ describe("calcite-carousel", () => {
expect(await carousel.getProperty("paused")).toBe(false);
expect(selectedItem.id).toEqual("three");
});

it("does not begin autoplay after keyboard interaction if not enabled via property", async () => {
const page = await newE2EPage();

await page.setContent(
`<calcite-carousel label="Carousel example" id="example-carousel>
<calcite-carousel-item label="Carousel Item 1" id="one"><p>no pre-selected attribute</p></calcite-carousel-item>
<calcite-carousel-item label="Carousel Item 2" id="two" selected><p>pre-selected and not first</p></calcite-carousel-item>
<calcite-carousel-item label="Carousel Item 3" id="three"><p>no pre-selected attribute</p></calcite-carousel-item>
</calcite-carousel>`,
);

const carousel = await page.find("calcite-carousel");
const playSpy = await page.spyOnEvent("calciteCarouselPlay");
const stopSpy = await page.spyOnEvent("calciteCarouselStop");
const pauseSpy = await page.spyOnEvent("calciteCarouselPause");
const resumeSpy = await page.spyOnEvent("calciteCarouselResume");

let selectedItem = await carousel.find(`calcite-carousel-item[selected]`);
expect(selectedItem.id).toEqual("two");
expect(playSpy).not.toHaveReceivedEvent();
expect(stopSpy).not.toHaveReceivedEvent();
expect(pauseSpy).not.toHaveReceivedEvent();
expect(resumeSpy).not.toHaveReceivedEvent();
expect(await carousel.getProperty("paused")).toBe(undefined);

await page.keyboard.press("Tab");
await page.waitForChanges();
expect(selectedItem.id).toEqual("two");
expect(playSpy).not.toHaveReceivedEvent();
expect(stopSpy).not.toHaveReceivedEvent();
expect(pauseSpy).not.toHaveReceivedEvent();
expect(resumeSpy).not.toHaveReceivedEvent();
expect(await page.evaluate(() => document.activeElement.id)).toEqual(carousel.id);

await page.waitForTimeout(slideDurationWaitTimer);
selectedItem = await carousel.find(`calcite-carousel-item[selected]`);
expect(selectedItem.id).toEqual("two");

await page.keyboard.press("Enter");
await page.waitForChanges();
expect(selectedItem.id).toEqual("two");
expect(playSpy).not.toHaveReceivedEvent();
expect(stopSpy).not.toHaveReceivedEvent();
expect(pauseSpy).not.toHaveReceivedEvent();
expect(resumeSpy).not.toHaveReceivedEvent();
expect(await carousel.getProperty("paused")).toBe(undefined);
expect(selectedItem.id).toEqual("two");

await page.waitForTimeout(slideDurationWaitTimer);
selectedItem = await carousel.find(`calcite-carousel-item[selected]`);
expect(selectedItem.id).toEqual("two");

await page.keyboard.press("Space");
await page.waitForChanges();
expect(playSpy).not.toHaveReceivedEvent();
expect(stopSpy).not.toHaveReceivedEvent();
expect(pauseSpy).not.toHaveReceivedEvent();
expect(resumeSpy).not.toHaveReceivedEvent();
expect(await carousel.getProperty("paused")).toBe(undefined);

expect(selectedItem.id).toEqual("two");

await page.keyboard.press("Space");
await page.waitForChanges();
expect(playSpy).not.toHaveReceivedEvent();
expect(stopSpy).not.toHaveReceivedEvent();
expect(pauseSpy).not.toHaveReceivedEvent();
expect(resumeSpy).not.toHaveReceivedEvent();
expect(await carousel.getProperty("paused")).toBe(undefined);
expect(selectedItem.id).toEqual("two");
});
});

describe("pagination", () => {
Expand Down Expand Up @@ -773,6 +845,7 @@ describe("calcite-carousel", () => {
expect(selectedItem.id).toEqual("two");
});
});

describe("public methods", () => {
it("plays and stops correctly when autoplay", async () => {
const page = await newE2EPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ export class Carousel
case " ":
case "Enter":
event.preventDefault();
this.toggleRotation();
if (this.autoplay === "" || this.autoplay || this.autoplay === "paused") {
this.toggleRotation();
}
break;
case "ArrowRight":
this.direction = "forward";
Expand Down
Loading