|
1 | 1 | import $ from "jquery"; |
2 | 2 | import events from "../../core/events"; |
3 | 3 | import Stacks from "./stacks"; |
| 4 | +import utils from "../../core/utils"; |
4 | 5 | import { jest } from "@jest/globals"; |
5 | 6 |
|
6 | 7 | describe("pat-stacks", function () { |
@@ -164,4 +165,65 @@ describe("pat-stacks", function () { |
164 | 165 | expect($("#l2").hasClass("current")).toBe(true); |
165 | 166 | }); |
166 | 167 | }); |
| 168 | + |
| 169 | + describe("5 - Scrolling support.", function () { |
| 170 | + beforeEach(function () { |
| 171 | + document.body.innerHTML = ""; |
| 172 | + this.spy_scrollTo = jest |
| 173 | + .spyOn(window, "scrollTo") |
| 174 | + .mockImplementation(() => null); |
| 175 | + }); |
| 176 | + |
| 177 | + afterEach(function () { |
| 178 | + this.spy_scrollTo.mockRestore(); |
| 179 | + }); |
| 180 | + |
| 181 | + it("5.1 - Scrolls to self.", async function () { |
| 182 | + document.body.innerHTML = ` |
| 183 | + <a href='#s51'>1</a> |
| 184 | + <div class="pat-stacks" data-pat-stacks="scroll-selector: self"> |
| 185 | + <section id="s51"> |
| 186 | + </section> |
| 187 | + </div> |
| 188 | + `; |
| 189 | + const el = document.querySelector(".pat-stacks"); |
| 190 | + |
| 191 | + const instance = new Stacks(el); |
| 192 | + await events.await_pattern_init(instance); |
| 193 | + |
| 194 | + const s51 = document.querySelector("[href='#s51']"); |
| 195 | + $(s51).click(); |
| 196 | + await utils.timeout(10); |
| 197 | + |
| 198 | + expect(this.spy_scrollTo).toHaveBeenCalled(); |
| 199 | + }); |
| 200 | + |
| 201 | + it("5.2 - Does clear scroll setting from parent config.", async function () { |
| 202 | + // NOTE: We give the stack section a different id. |
| 203 | + // The event handler which is registered on the document in the |
| 204 | + // previous test is still attached. Two event handlers are run when |
| 205 | + // clicking here and if the anchor-targets would have the same id |
| 206 | + // the scrolling would happen as it was set up in the previous |
| 207 | + // test. |
| 208 | + document.body.innerHTML = ` |
| 209 | + <div data-pat-stacks="scroll-selector: self"> |
| 210 | + <a href='#s52'>1</a> |
| 211 | + <div class="pat-stacks" data-pat-stacks="scroll-selector: none"> |
| 212 | + <section id="s52"> |
| 213 | + </section> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | + `; |
| 217 | + const el = document.querySelector(".pat-stacks"); |
| 218 | + |
| 219 | + const instance = new Stacks(el); |
| 220 | + await events.await_pattern_init(instance); |
| 221 | + |
| 222 | + const s52 = document.querySelector("[href='#s52']"); |
| 223 | + $(s52).click(); |
| 224 | + await utils.timeout(10); |
| 225 | + |
| 226 | + expect(this.spy_scrollTo).not.toHaveBeenCalled(); |
| 227 | + }); |
| 228 | + }); |
167 | 229 | }); |
0 commit comments