|
| 1 | +/** |
| 2 | + * Please refer to the terms of the license agreement in the root of the project |
| 3 | + * |
| 4 | + * (c) 2024 Feedzai |
| 5 | + */ |
| 6 | +import React, { createRef, useCallback } from "react"; |
| 7 | +import { toggleDataAttribute } from "src/functions"; |
| 8 | +import { useIntersection } from "src/hooks"; |
| 9 | + |
| 10 | +describe("useIntersection", () => { |
| 11 | + beforeEach(() => { |
| 12 | + cy.viewport(1000, 1000); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should be defined", () => { |
| 16 | + expect(useIntersection).to.be.a("function"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should setup an IntersectionObserver targeting the ref element and using the options provided", () => { |
| 20 | + const TestComponent = () => { |
| 21 | + const targetRef = createRef<HTMLButtonElement>(); |
| 22 | + const observerOptions = { root: null, threshold: 0.8 }; |
| 23 | + const intersection = useIntersection(targetRef, observerOptions); |
| 24 | + |
| 25 | + const isVisible = !!(intersection && intersection.isIntersecting); |
| 26 | + |
| 27 | + return ( |
| 28 | + <button |
| 29 | + ref={targetRef} |
| 30 | + data-testid="target" |
| 31 | + data-intersecting={toggleDataAttribute(isVisible)} |
| 32 | + > |
| 33 | + Target Element |
| 34 | + </button> |
| 35 | + ); |
| 36 | + }; |
| 37 | + |
| 38 | + cy.mount(<TestComponent />); |
| 39 | + cy.findByTestId("target") |
| 40 | + .should("exist") |
| 41 | + .and("be.visible") |
| 42 | + .and("have.attr", "data-intersecting"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return null if a ref without a current value is provided", () => { |
| 46 | + const TestComponent = () => { |
| 47 | + const targetRef = createRef<HTMLElement>(); |
| 48 | + const intersection = useIntersection(targetRef, { root: null, threshold: 1 }); |
| 49 | + |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <div>{JSON.stringify(intersection)}</div> |
| 53 | + </div> |
| 54 | + ); |
| 55 | + }; |
| 56 | + |
| 57 | + cy.mount(<TestComponent />); |
| 58 | + cy.contains("null").should("exist"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should reset an intersectionObserverEntry when the ref changes", () => { |
| 62 | + const TestComponent = () => { |
| 63 | + const [key, setKey] = React.useState("805e7392-002c-4172-9afe-4f7f3eb1f94d"); |
| 64 | + const targetRef = createRef<HTMLDivElement>(); |
| 65 | + const intersection = useIntersection(targetRef, { root: null, threshold: 0.8 }); |
| 66 | + const isVisible = !!(intersection && intersection.isIntersecting); |
| 67 | + |
| 68 | + const handleOnClick = useCallback(() => { |
| 69 | + setKey("0709c8d7-63e8-46a7-b0b4-8fbd6debeac9"); |
| 70 | + }, [setKey]); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div> |
| 74 | + <div key={key} ref={targetRef} data-testid="target"> |
| 75 | + Target Element |
| 76 | + </div> |
| 77 | + <button onClick={handleOnClick}>Change Ref</button> |
| 78 | + <div data-testid="intersection" data-intersecting={toggleDataAttribute(isVisible)}> |
| 79 | + {String(isVisible)} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ); |
| 83 | + }; |
| 84 | + |
| 85 | + cy.mount(<TestComponent />); |
| 86 | + |
| 87 | + cy.findByTestId("intersection").should("have.attr", "data-intersecting"); |
| 88 | + cy.findByRole("button").click(); |
| 89 | + cy.findByTestId("intersection").should("not.have.attr", "data-intersecting"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should return the first IntersectionObserverEntry when the IntersectionObserver registers an intersection", () => { |
| 93 | + const TestComponent = () => { |
| 94 | + const targetRef = createRef<HTMLButtonElement>(); |
| 95 | + const intersection = useIntersection(targetRef, { root: null, threshold: 0.8 }); |
| 96 | + const isVisible = !!(intersection && intersection.isIntersecting); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div style={{ height: "200vh" }}> |
| 100 | + <button style={{ marginTop: "100vh" }} ref={targetRef} data-testid="target"> |
| 101 | + Target Element |
| 102 | + </button> |
| 103 | + <div data-testid="intersection" data-intersecting={toggleDataAttribute(isVisible)} /> |
| 104 | + </div> |
| 105 | + ); |
| 106 | + }; |
| 107 | + |
| 108 | + cy.mount(<TestComponent />); |
| 109 | + |
| 110 | + cy.findByTestId("intersection").should("not.have.attr", "data-intersecting"); |
| 111 | + cy.findByTestId("target").scrollIntoView(); |
| 112 | + cy.findByTestId("intersection").should("have.attr", "data-intersecting"); |
| 113 | + }); |
| 114 | +}); |
0 commit comments