|
1 | 1 | import React from 'react'; |
2 | 2 |
|
3 | | -import {DefaultNavigationPresenceProvider} from 'widgets/defaultNavigation/DefaultNavigationPresenceProvider'; |
| 3 | +import { |
| 4 | + DefaultNavigationPresenceProvider, |
| 5 | + useDefaultNavigationState |
| 6 | +} from 'widgets/defaultNavigation/DefaultNavigationPresenceProvider'; |
4 | 7 |
|
5 | 8 | import styles from 'widgets/defaultNavigation/presenceClassNames.module.css'; |
6 | 9 |
|
7 | 10 | import {renderInEntry} from 'pageflow-scrolled/testHelpers'; |
8 | 11 | import {act} from '@testing-library/react'; |
9 | 12 | import '@testing-library/jest-dom/extend-expect'; |
10 | 13 |
|
| 14 | +function LockNavButton() { |
| 15 | + const {lockNavExpanded} = useDefaultNavigationState(); |
| 16 | + return <button onClick={lockNavExpanded}>Lock</button>; |
| 17 | +} |
| 18 | + |
11 | 19 | describe('DefaultNavigationPresenceProvider', () => { |
12 | 20 | afterEach(() => jest.restoreAllMocks()); |
| 21 | + |
13 | 22 | it('renders wrapper with class setting --widget-margin-top-max by default', () => { |
14 | 23 | const {container} = renderInEntry( |
15 | 24 | <DefaultNavigationPresenceProvider configuration={{}}> |
@@ -66,41 +75,114 @@ describe('DefaultNavigationPresenceProvider', () => { |
66 | 75 | expect(container.firstChild).toHaveClass(styles.expanded); |
67 | 76 | }); |
68 | 77 |
|
69 | | - it('toggles expanded class based on scroll direction', () => { |
70 | | - Object.defineProperty(window, 'scrollY', { |
71 | | - writable: true, |
72 | | - value: 0 |
| 78 | + describe('scroll direction', () => { |
| 79 | + beforeEach(() => { |
| 80 | + jest.useFakeTimers(); |
| 81 | + |
| 82 | + Object.defineProperty(window, 'scrollY', { |
| 83 | + writable: true, |
| 84 | + value: 0 |
| 85 | + }); |
| 86 | + |
| 87 | + jest.spyOn(document.body, 'getBoundingClientRect').mockImplementation(() => ({ |
| 88 | + top: -window.scrollY, |
| 89 | + left: 0, |
| 90 | + right: 1024, |
| 91 | + bottom: 768 - window.scrollY, |
| 92 | + width: 1024, |
| 93 | + height: 768 |
| 94 | + })); |
73 | 95 | }); |
74 | 96 |
|
75 | | - jest.spyOn(document.body, 'getBoundingClientRect').mockImplementation(() => ({ |
76 | | - top: -window.scrollY, |
77 | | - left: 0, |
78 | | - right: 1024, |
79 | | - bottom: 768 - window.scrollY, |
80 | | - width: 1024, |
81 | | - height: 768 |
82 | | - })); |
| 97 | + afterEach(() => { |
| 98 | + jest.useRealTimers(); |
| 99 | + }); |
83 | 100 |
|
84 | | - const {container} = renderInEntry( |
85 | | - <DefaultNavigationPresenceProvider configuration={{}}> |
86 | | - <div /> |
87 | | - </DefaultNavigationPresenceProvider> |
88 | | - ); |
| 101 | + it('toggles expanded class based on scroll direction', () => { |
| 102 | + const {container} = renderInEntry( |
| 103 | + <DefaultNavigationPresenceProvider configuration={{}}> |
| 104 | + <div /> |
| 105 | + </DefaultNavigationPresenceProvider> |
| 106 | + ); |
89 | 107 |
|
90 | | - expect(container.firstChild).toHaveClass(styles.expanded); |
| 108 | + expect(container.firstChild).toHaveClass(styles.expanded); |
| 109 | + |
| 110 | + act(() => { |
| 111 | + window.scrollY = 100; |
| 112 | + window.dispatchEvent(new Event('scroll')); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(container.firstChild).not.toHaveClass(styles.expanded); |
| 116 | + |
| 117 | + act(() => { |
| 118 | + window.scrollY = 50; |
| 119 | + window.dispatchEvent(new Event('scroll')); |
| 120 | + }); |
| 121 | + |
| 122 | + expect(container.firstChild).toHaveClass(styles.expanded); |
| 123 | + }); |
91 | 124 |
|
92 | | - act(() => { |
93 | | - window.scrollY = 100; |
94 | | - window.dispatchEvent(new Event('scroll')); |
| 125 | + it('stays expanded during scroll lock', () => { |
| 126 | + const {container, getByText} = renderInEntry( |
| 127 | + <DefaultNavigationPresenceProvider configuration={{}}> |
| 128 | + <LockNavButton /> |
| 129 | + </DefaultNavigationPresenceProvider> |
| 130 | + ); |
| 131 | + |
| 132 | + act(() => { |
| 133 | + getByText('Lock').click(); |
| 134 | + }); |
| 135 | + |
| 136 | + act(() => { |
| 137 | + window.scrollY = 100; |
| 138 | + window.dispatchEvent(new Event('scroll')); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(container.firstChild).toHaveClass(styles.expanded); |
95 | 142 | }); |
96 | 143 |
|
97 | | - expect(container.firstChild).not.toHaveClass(styles.expanded); |
| 144 | + it('resumes collapsing after scroll lock timeout', () => { |
| 145 | + const {container, getByText} = renderInEntry( |
| 146 | + <DefaultNavigationPresenceProvider configuration={{}}> |
| 147 | + <LockNavButton /> |
| 148 | + </DefaultNavigationPresenceProvider> |
| 149 | + ); |
| 150 | + |
| 151 | + act(() => { |
| 152 | + getByText('Lock').click(); |
| 153 | + }); |
| 154 | + |
| 155 | + act(() => { |
| 156 | + jest.advanceTimersByTime(200); |
| 157 | + }); |
98 | 158 |
|
99 | | - act(() => { |
100 | | - window.scrollY = 50; |
101 | | - window.dispatchEvent(new Event('scroll')); |
| 159 | + act(() => { |
| 160 | + window.scrollY = 100; |
| 161 | + window.dispatchEvent(new Event('scroll')); |
| 162 | + }); |
| 163 | + |
| 164 | + expect(container.firstChild).not.toHaveClass(styles.expanded); |
102 | 165 | }); |
103 | 166 |
|
104 | | - expect(container.firstChild).toHaveClass(styles.expanded); |
| 167 | + it('re-expands nav when lockNavExpanded is called while collapsed', () => { |
| 168 | + const {container, getByText} = renderInEntry( |
| 169 | + <DefaultNavigationPresenceProvider configuration={{}}> |
| 170 | + <LockNavButton /> |
| 171 | + </DefaultNavigationPresenceProvider> |
| 172 | + ); |
| 173 | + |
| 174 | + act(() => { |
| 175 | + window.scrollY = 100; |
| 176 | + window.dispatchEvent(new Event('scroll')); |
| 177 | + }); |
| 178 | + |
| 179 | + expect(container.firstChild).not.toHaveClass(styles.expanded); |
| 180 | + |
| 181 | + act(() => { |
| 182 | + getByText('Lock').click(); |
| 183 | + }); |
| 184 | + |
| 185 | + expect(container.firstChild).toHaveClass(styles.expanded); |
| 186 | + }); |
105 | 187 | }); |
106 | 188 | }); |
0 commit comments