|
| 1 | +import * as carouselStories from "@stories/carousel/carousel.stories"; |
| 2 | +import { composeStories } from "@storybook/react"; |
| 3 | +import { checkAccessibility } from "../../../../../../cypress/tests/checkAccessibility"; |
| 4 | + |
| 5 | +const composedStories = composeStories(carouselStories); |
| 6 | +const { Default, WithActions, Controlled } = composedStories; |
| 7 | +describe("GIVEN a 100% width slides carousel", () => { |
| 8 | + checkAccessibility(composedStories); |
| 9 | + describe("WHEN the default is rendered with slides", () => { |
| 10 | + it("SHOULD render carousel", () => { |
| 11 | + cy.mount(<Default />); |
| 12 | + cy.findByRole("region").should("exist"); |
| 13 | + }); |
| 14 | + }); |
| 15 | + describe("WHEN moving slides with buttons", () => { |
| 16 | + it("SHOULD move to the next slide with button", () => { |
| 17 | + cy.mount(<Default />); |
| 18 | + cy.findAllByText("1 of 4").should("exist"); |
| 19 | + cy.findAllByRole("button", { name: "Previous slide" }).should( |
| 20 | + "have.attr", |
| 21 | + "aria-disabled", |
| 22 | + "true", |
| 23 | + ); |
| 24 | + cy.findAllByRole("button", { name: "Next slide" }).click(); |
| 25 | + cy.findAllByText("2 of 4").should("exist"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("SHOULD move to the previous slide with button", () => { |
| 29 | + cy.mount(<Default />); |
| 30 | + cy.findAllByText("1 of 4").should("exist"); |
| 31 | + // move one to the left |
| 32 | + cy.findAllByRole("button", { name: "Next slide" }).click(); |
| 33 | + cy.findAllByText("2 of 4").should("exist"); |
| 34 | + // test back button |
| 35 | + cy.findAllByRole("button", { name: "Previous slide" }).click(); |
| 36 | + cy.findAllByText("1 of 4").should("exist"); |
| 37 | + }); |
| 38 | + it("SHOULD disable previous button when reaching far left", () => { |
| 39 | + cy.mount(<Default defaultActiveSlideIndex={1} />); |
| 40 | + cy.findAllByText("2 of 4").should("exist"); |
| 41 | + cy.findAllByRole("button", { name: "Previous slide" }).click(); |
| 42 | + cy.findAllByRole("button", { name: "Previous slide" }).should( |
| 43 | + "have.attr", |
| 44 | + "aria-disabled", |
| 45 | + "true", |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it("SHOULD disable next button when reaching far right", () => { |
| 50 | + cy.mount(<Default defaultActiveSlideIndex={2} />); |
| 51 | + cy.findAllByText("3 of 4").should("exist"); |
| 52 | + cy.findAllByRole("button", { name: "Next slide" }).click(); |
| 53 | + cy.findAllByText("4 of 4").should("exist"); |
| 54 | + cy.findAllByRole("button", { name: "Next slide" }).should( |
| 55 | + "have.attr", |
| 56 | + "aria-disabled", |
| 57 | + "true", |
| 58 | + ); |
| 59 | + }); |
| 60 | + it.skip("SHOULD update labels when scrolling", () => { |
| 61 | + // flaky with react 16/17 |
| 62 | + cy.mount(<Default />); |
| 63 | + cy.findAllByText("1 of 4").should("exist"); |
| 64 | + cy.get(".saltCarouselSlider").scrollTo("right"); |
| 65 | + cy.wait(100); |
| 66 | + cy.findAllByText("4 of 4").should("exist"); |
| 67 | + }); |
| 68 | + it("SHOULD support keyboard navigation", () => { |
| 69 | + cy.mount(<Default />); |
| 70 | + cy.findAllByText("1 of 4").should("exist"); |
| 71 | + cy.findAllByRole("group").get('[tabindex="0"]').focus(); |
| 72 | + cy.findAllByRole("group").get('[tabindex="0"]').realPress("ArrowRight"); |
| 73 | + cy.findAllByText("2 of 4").should("exist"); |
| 74 | + cy.wait(100); |
| 75 | + cy.findAllByRole("group").get('[tabindex="0"]').realPress("ArrowLeft"); |
| 76 | + cy.findAllByText("1 of 4").should("exist"); |
| 77 | + }); |
| 78 | + describe("WHEN navigating with keyboard keys", () => { |
| 79 | + it("SHOULD NOT move to hidden slides when using tab navigation", () => { |
| 80 | + cy.mount(<WithActions />); |
| 81 | + cy.findAllByText("1 - 2 of 4").should("exist"); |
| 82 | + cy.findAllByRole("button", { name: "Next slides" }).focus(); |
| 83 | + // tab through visible elements |
| 84 | + cy.realPress("Tab"); |
| 85 | + cy.realPress("Tab"); |
| 86 | + cy.findByText("Open an account").should("be.focused"); |
| 87 | + cy.realPress("Tab"); |
| 88 | + cy.realPress("Tab"); |
| 89 | + cy.findByText("Go to dashboard").should("be.focused"); |
| 90 | + // next tab should exit the carousel |
| 91 | + cy.realPress("Tab"); |
| 92 | + // slides should not have been changed |
| 93 | + cy.findAllByText("1 - 2 of 4").should("exist"); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
| 98 | +describe("GIVEN a carousel with responsive visibleItems", () => { |
| 99 | + it("SHOULD render properly with different visible items based on viewport", () => { |
| 100 | + cy.viewport(590, 900); // xs viewport |
| 101 | + cy.mount(<Default visibleSlides={{ xs: 1, sm: 2, md: 3 }} />); |
| 102 | + cy.findAllByText("1 of 4").should("exist"); |
| 103 | + |
| 104 | + cy.viewport(700, 900); // sm viewport |
| 105 | + cy.mount(<Default visibleSlides={{ xs: 1, sm: 2, md: 3 }} />); |
| 106 | + cy.findAllByText("1 - 2 of 4").should("exist"); |
| 107 | + |
| 108 | + cy.viewport(961, 1200); // md viewport |
| 109 | + cy.mount(<Default visibleSlides={{ xs: 1, sm: 2, md: 3 }} />); |
| 110 | + cy.findAllByText("1 - 3 of 4").should("exist"); |
| 111 | + }); |
| 112 | +}); |
| 113 | +describe("WHEN mounted as an uncontrolled component", () => { |
| 114 | + it("THEN should move slide when controlled slide index is passed", () => { |
| 115 | + cy.mount(<Controlled />); |
| 116 | + cy.findAllByRole("button", { name: "Right" }).click(); |
| 117 | + cy.findByText("Current slide: 2").should("exist"); |
| 118 | + cy.findAllByRole("button", { name: "Left" }).click(); |
| 119 | + cy.findByText("Current slide: 1").should("exist"); |
| 120 | + }); |
| 121 | + it("THEN should be able to update controlled value when navigating slider", () => { |
| 122 | + cy.mount(<Controlled />); |
| 123 | + cy.findByText("Current slide: 1").should("exist"); |
| 124 | + cy.findAllByRole("group").get('[tabindex="0"]').focus(); |
| 125 | + cy.findAllByRole("group").get('[tabindex="0"]').realPress("ArrowRight"); |
| 126 | + cy.findByText("Current slide: 2").should("exist"); |
| 127 | + }); |
| 128 | +}); |
0 commit comments