|
1 | | -import pattern, { |
2 | | - TIMEOUT_CALLBACK, |
3 | | - TIMEOUT_FIRST_CALLBACK, |
4 | | - TIMEOUT_PAUSE, |
5 | | -} from "./scroll-box"; |
| 1 | +import pattern from "./scroll-box"; |
6 | 2 | import utils from "../../core/utils"; |
| 3 | +import events from "../../core/events"; |
7 | 4 |
|
8 | 5 | describe("pat-scroll-box", function () { |
9 | 6 | afterEach(function () { |
10 | 7 | jest.restoreAllMocks(); |
11 | 8 | document.body.innerHTML = ""; |
12 | 9 | }); |
13 | 10 |
|
14 | | - it("1 - First callback invoked early", async function () { |
| 11 | + it("1 - Basic functionality", async function () { |
15 | 12 | document.body.innerHTML = ` |
16 | 13 | <div id="el1" style="overflow: scroll"></div> |
17 | 14 | `; |
18 | 15 | const el = document.querySelector("#el1"); |
19 | 16 |
|
20 | | - const instance = new pattern(el); |
| 17 | + // mocks |
| 18 | + Object.defineProperty(el, "clientHeight", { value: 100, writable: false }); |
| 19 | + Object.defineProperty(el, "scrollHeight", { value: 300, writable: false }); |
| 20 | + Object.defineProperty(el, "scrollTop", { value: 0, writable: true }); |
| 21 | + |
| 22 | + new pattern(el); |
21 | 23 | await utils.timeout(1); |
22 | 24 |
|
23 | | - const spy_set_scroll_classes = jest.spyOn(instance, "set_scroll_classes"); |
| 25 | + el.scrollTop = 0; |
| 26 | + el.dispatchEvent(events.scroll_event()); |
| 27 | + expect(el.classList).toContain("scroll-position-top"); |
| 28 | + expect(el.classList).not.toContain("scroll-position-bottom"); |
| 29 | + expect(el.classList).not.toContain("scroll-up"); |
| 30 | + expect(el.classList).not.toContain("scroll-down"); |
24 | 31 |
|
25 | | - // First invocation is after TIMEOUT_FIRST_CALLBACK |
26 | | - el.dispatchEvent(new Event("scroll")); |
27 | | - await utils.timeout(1); |
28 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(0); |
29 | | - await utils.timeout(TIMEOUT_FIRST_CALLBACK - 1); |
30 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(1); |
31 | | - // No other callback invocations with just one scroll event. |
32 | | - await utils.timeout(TIMEOUT_CALLBACK - TIMEOUT_FIRST_CALLBACK - 1); |
33 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(1); |
| 32 | + el.scrollTop = 100; |
| 33 | + el.dispatchEvent(events.scroll_event()); |
| 34 | + await utils.animation_frame(); |
| 35 | + expect(el.classList).not.toContain("scroll-position-top"); |
| 36 | + expect(el.classList).not.toContain("scroll-position-bottom"); |
| 37 | + expect(el.classList).not.toContain("scroll-up"); |
| 38 | + expect(el.classList).toContain("scroll-down"); |
34 | 39 |
|
35 | | - // Now, subsequent scroll events invoke the callback after TIMEOUT_CALLBACK |
36 | | - // But multiple scroll events don't lead to multiple callback invocations. |
37 | | - el.dispatchEvent(new Event("scroll")); |
38 | | - await utils.timeout(1); |
39 | | - el.dispatchEvent(new Event("scroll")); |
40 | | - await utils.timeout(1); |
41 | | - el.dispatchEvent(new Event("scroll")); |
42 | | - // After TIMEOUT_FIRST_CALLBACK no NEW cb invocation |
43 | | - await utils.timeout(TIMEOUT_FIRST_CALLBACK); |
44 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(1); |
45 | | - // After TIMEOUT_CALLBACK there should be another cb invocation |
46 | | - await utils.timeout(TIMEOUT_CALLBACK - TIMEOUT_FIRST_CALLBACK); |
47 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(2); |
48 | | - // But without a new scroll event no more callback invocations. |
49 | | - await utils.timeout(TIMEOUT_CALLBACK); |
50 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(2); |
| 40 | + el.scrollTop = 50; |
| 41 | + el.dispatchEvent(events.scroll_event()); |
| 42 | + await utils.animation_frame(); |
| 43 | + expect(el.classList).not.toContain("scroll-position-top"); |
| 44 | + expect(el.classList).not.toContain("scroll-position-bottom"); |
| 45 | + expect(el.classList).toContain("scroll-up"); |
| 46 | + expect(el.classList).not.toContain("scroll-down"); |
51 | 47 |
|
52 | | - // Another scroll event, another callback invocation after 200ms |
53 | | - // We have to dispatch again |
54 | | - el.dispatchEvent(new Event("scroll")); |
55 | | - // After TIMEOUT_FIRST_CALLBACK no NEW cb invocation |
56 | | - await utils.timeout(TIMEOUT_FIRST_CALLBACK); |
57 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(2); |
58 | | - // After TIMEOUT_CALLBACK there should be another cb invocation |
59 | | - await utils.timeout(TIMEOUT_CALLBACK - TIMEOUT_FIRST_CALLBACK); |
60 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(3); |
| 48 | + el.scrollTop = 200; |
| 49 | + el.dispatchEvent(events.scroll_event()); |
| 50 | + await utils.animation_frame(); |
| 51 | + expect(el.classList).not.toContain("scroll-position-top"); |
| 52 | + expect(el.classList).toContain("scroll-position-bottom"); |
| 53 | + expect(el.classList).not.toContain("scroll-up"); |
| 54 | + expect(el.classList).toContain("scroll-down"); |
61 | 55 |
|
62 | | - // Let's wait for TIMEOUT_PAUSE to reset and start again with the |
63 | | - // first call after TIMEOUT_FIRST_CALLBACK. |
64 | | - // The user probably stopped scrolling and starts over again |
65 | | - await utils.timeout(TIMEOUT_PAUSE); |
66 | | - el.dispatchEvent(new Event("scroll")); |
67 | | - await utils.timeout(TIMEOUT_FIRST_CALLBACK); |
68 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(4); |
69 | | - el.dispatchEvent(new Event("scroll")); |
70 | | - await utils.timeout(TIMEOUT_CALLBACK); |
71 | | - expect(spy_set_scroll_classes).toHaveBeenCalledTimes(5); |
| 56 | + el.scrollTop = 0; |
| 57 | + el.dispatchEvent(events.scroll_event()); |
| 58 | + await utils.animation_frame(); |
| 59 | + expect(el.classList).toContain("scroll-position-top"); |
| 60 | + expect(el.classList).not.toContain("scroll-position-bottom"); |
| 61 | + expect(el.classList).toContain("scroll-up"); |
| 62 | + expect(el.classList).not.toContain("scroll-down"); |
72 | 63 | }); |
73 | 64 | }); |
0 commit comments