@@ -17,29 +17,69 @@ describe('useIsInViewport', () => {
1717 cy . get ( '[data-testid="box"]' ) . should ( 'contain' , 'In viewport' )
1818 } )
1919
20- // it('should report correctly when ')
21-
22- // it('should work for element with a custom viewport', () => {
23- // cy.get('[data-testid="div-viewport-parent-el"]').should(
24- // 'contain',
25- // 'Not intersecting with parent'
26- // )
27- // cy.get('[data-testid="toggle-container-size"]').click()
28- // cy.get('[data-testid="div-viewport-parent-el"]').should('contain', 'Intersecting with parent')
29- // cy.get('[data-testid="toggle-container-size"]').click()
30- // cy.get('[data-testid="div-viewport-parent-el"]').should(
31- // 'contain',
32- // 'Not intersecting with parent'
33- // )
34- // })
20+ it ( 'should report correctly when target ref is forwarded from a parent' , ( ) => {
21+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-doc-test"]' ) . should (
22+ 'contain' ,
23+ 'Show ref fwd parent doc test'
24+ )
25+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-doc-test"]' ) . click ( )
26+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-doc-test"]' ) . should (
27+ 'contain' ,
28+ 'Hide ref fwd parent doc test'
29+ )
30+ cy . get ( '[data-testid="box"]' ) . should ( 'contain' , 'In viewport' )
31+ cy . get ( '[data-testid="toggle-box-position"]' ) . click ( )
32+ cy . get ( '[data-testid="box"]' ) . should ( 'contain' , 'Out of viewport' )
33+ cy . scrollTo ( 0 , 413 ) // just about greater than 50% of target element intersects with parent doc viewport
34+ cy . get ( '[data-testid="box"]' ) . should ( 'contain' , 'In viewport' )
35+ cy . scrollTo ( 0 , 412 ) // just about less than 50% of target element intersects with parent doc viewport
36+ cy . get ( '[data-testid="box"]' ) . should ( 'contain' , 'Out of viewport' )
37+ // test side effect from the target ref that was forwarded to the element to watch
38+ cy . window ( ) . then ( window => {
39+ expect ( window . forwardedTargetRef ) . length ( 1 )
40+ expect ( window . forwardedTargetRef [ 0 ] . classList . contains ( 'target-div' ) ) . to . be . true
41+ } )
42+ } )
3543
36- // it('should work for elements that intersect with their parent document', () => {
37- // cy.get('[data-testid="div-viewport-window"]')
38- // .should('contain', 'Hidden')
39- // .scrollIntoView()
40- // .should('contain', 'Visible')
44+ it ( 'should report correctly when a parent element is used as viewport' , ( ) => {
45+ cy . get ( '[data-testid="toggle-simple-parent-element-test"]' ) . should (
46+ 'contain' ,
47+ 'Show simple parent element test'
48+ )
49+ cy . get ( '[data-testid="toggle-simple-parent-element-test"]' ) . click ( )
50+ cy . get ( '[data-testid="toggle-simple-parent-element-test"]' ) . should (
51+ 'contain' ,
52+ 'Hide simple parent element test'
53+ )
54+ cy . get ( '[data-testid="child"]' ) . should ( 'contain' , 'In viewport' )
55+ cy . get ( '[data-testid="toggle-box-position"]' ) . click ( )
56+ cy . get ( '[data-testid="child"]' ) . should ( 'contain' , 'Out of viewport' )
57+ cy . get ( '[data-testid="viewport"]' ) . scrollTo ( 0 , 68 ) // just 1px of target intersecting with the parent viewport
58+ cy . get ( '[data-testid="child"]' ) . should ( 'contain' , 'In viewport' )
59+ cy . get ( '[data-testid="viewport"]' ) . scrollTo ( 0 , 67 ) // just 1px of target outside of the parent viewport
60+ cy . get ( '[data-testid="child"]' ) . should ( 'contain' , 'Out of viewport' )
61+ } )
4162
42- // cy.scrollTo(0, 0)
43- // cy.get('[data-testid="div-viewport-window"]').should('contain', 'Hidden')
44- // })
63+ it ( 'should report correctly when a parent element ref is forwarded from a parent' , ( ) => {
64+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-element-test"]' ) . should (
65+ 'contain' ,
66+ 'Show ref fwd parent element test'
67+ )
68+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-element-test"]' ) . click ( )
69+ cy . get ( '[data-testid="toggle-ref-forwarding-parent-element-test"]' ) . should (
70+ 'contain' ,
71+ 'Hide ref fwd parent element test'
72+ )
73+ cy . get ( '[data-testid="first-child"]' ) . should ( 'contain' , 'First child >= 75% in viewport' )
74+ cy . get ( '[data-testid="second-child"]' ) . should ( 'contain' , 'Second child >= 75% in viewport' )
75+ cy . get ( '[data-testid="toggle-boxes-positions"]' ) . click ( )
76+ cy . get ( '[data-testid="first-child"]' ) . should ( 'contain' , 'First child out of viewport' )
77+ cy . get ( '[data-testid="second-child"]' ) . should ( 'contain' , 'Second child out of viewport' )
78+ cy . get ( '[data-testid="viewport"]' ) . scrollTo ( 0 , 190 ) // 75% or more of first child in viewport
79+ cy . get ( '[data-testid="first-child"]' ) . should ( 'contain' , 'First child >= 75% in viewport' )
80+ cy . get ( '[data-testid="second-child"]' ) . should ( 'contain' , 'Second child out of viewport' )
81+ cy . get ( '[data-testid="viewport"]' ) . scrollTo ( 0 , 250 ) // 75% or more of second child in viewport
82+ cy . get ( '[data-testid="first-child"]' ) . should ( 'contain' , 'First child >= 75% in viewport' )
83+ cy . get ( '[data-testid="second-child"]' ) . should ( 'contain' , 'Second child >= 75% in viewport' )
84+ } )
4585} )
0 commit comments