|
| 1 | +import Pattern from "./navigationmarker"; |
| 2 | + |
| 3 | +describe("pat-navigationmarker", () => { |
| 4 | + let _window_location; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + _window_location = global.window.location; |
| 8 | + delete global.window.location; |
| 9 | + document.body.innerHTML = ""; |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + global.window.location = _window_location; |
| 14 | + }); |
| 15 | + |
| 16 | + const set_url = (url, portal_url) => { |
| 17 | + global.window.location = { |
| 18 | + href: url, |
| 19 | + }; |
| 20 | + |
| 21 | + portal_url = portal_url || url; |
| 22 | + |
| 23 | + document.body.dataset.portalUrl = portal_url; |
| 24 | + }; |
| 25 | + |
| 26 | + it("navigationmarker roundtrip", () => { |
| 27 | + document.body.innerHTML = ` |
| 28 | + <nav class="pat-navigationmarker"> |
| 29 | + <ul> |
| 30 | + <li> |
| 31 | + <a href="/">Home</a> |
| 32 | + </li> |
| 33 | + <li> |
| 34 | + <a href="/path1">p1</a> |
| 35 | + </li> |
| 36 | + <li> |
| 37 | + <a href="/path2">p2</a> |
| 38 | + <ul> |
| 39 | + <li> |
| 40 | + <a href="/path2/path2.1">p2.1</a> |
| 41 | + </li> |
| 42 | + <li> |
| 43 | + <a href="/path2/path2.2">p2.2</a> |
| 44 | + <ul> |
| 45 | + <li> |
| 46 | + <a href="/path2/path2.2/path2.2.1">p2.2.1</a> |
| 47 | + </li> |
| 48 | + <li> |
| 49 | + <a href="/path2/path2.2/path2.2.2">p2.2.2</a> |
| 50 | + </li> |
| 51 | + </ul> |
| 52 | + </li> |
| 53 | + <li> |
| 54 | + <a href="../../path3">p1</a> |
| 55 | + </li> |
| 56 | + <li> |
| 57 | + <a href="https://patternslib.com/path4">p1</a> |
| 58 | + </li> |
| 59 | + </ul> |
| 60 | + </li> |
| 61 | + </ul> |
| 62 | + </nav> |
| 63 | + `; |
| 64 | + |
| 65 | + set_url("https://patternslib.com/"); |
| 66 | + |
| 67 | + const instance = new Pattern(document.querySelector(".pat-navigationmarker")); |
| 68 | + |
| 69 | + const it0 = document.querySelector("a[href='/']"); |
| 70 | + const it1 = document.querySelector("a[href='/path1']"); |
| 71 | + const it2 = document.querySelector("a[href='/path2']"); |
| 72 | + const it21 = document.querySelector("a[href='/path2/path2.1']"); |
| 73 | + const it22 = document.querySelector("a[href='/path2/path2.2']"); |
| 74 | + const it221 = document.querySelector("a[href='/path2/path2.2/path2.2.1']"); |
| 75 | + const it222 = document.querySelector("a[href='/path2/path2.2/path2.2.2']"); |
| 76 | + const it3 = document.querySelector("a[href='../../path3']"); |
| 77 | + const it4 = document.querySelector("a[href='https://patternslib.com/path4']"); |
| 78 | + |
| 79 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 80 | + expect(document.querySelectorAll(".inPath").length).toBe(0); |
| 81 | + expect(document.querySelector(".current a")).toBe(it0); |
| 82 | + |
| 83 | + instance.clear_items(); |
| 84 | + instance.mark_items("https://patternslib.com/path1"); |
| 85 | + |
| 86 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 87 | + expect(document.querySelectorAll(".inPath").length).toBe(0); |
| 88 | + expect(document.querySelector(".current a")).toBe(it1); |
| 89 | + |
| 90 | + instance.clear_items(); |
| 91 | + instance.mark_items("https://patternslib.com/path2"); |
| 92 | + |
| 93 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 94 | + expect(document.querySelectorAll(".inPath").length).toBe(0); |
| 95 | + expect(document.querySelector(".current a")).toBe(it2); |
| 96 | + |
| 97 | + instance.clear_items(); |
| 98 | + instance.mark_items("https://patternslib.com/path2/path2.1"); |
| 99 | + |
| 100 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 101 | + expect(document.querySelectorAll(".inPath").length).toBe(1); |
| 102 | + expect(document.querySelector(".current a")).toBe(it21); |
| 103 | + |
| 104 | + instance.clear_items(); |
| 105 | + instance.mark_items("https://patternslib.com/path2/path2.2"); |
| 106 | + |
| 107 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 108 | + expect(document.querySelectorAll(".inPath").length).toBe(1); |
| 109 | + expect(document.querySelector(".current a")).toBe(it22); |
| 110 | + |
| 111 | + instance.clear_items(); |
| 112 | + instance.mark_items("https://patternslib.com/path2/path2.2/path2.2.1"); |
| 113 | + |
| 114 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 115 | + expect(document.querySelectorAll(".inPath").length).toBe(2); |
| 116 | + expect(document.querySelector(".current a")).toBe(it221); |
| 117 | + |
| 118 | + instance.clear_items(); |
| 119 | + instance.mark_items("https://patternslib.com/path2/path2.2/path2.2.2"); |
| 120 | + |
| 121 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 122 | + expect(document.querySelectorAll(".inPath").length).toBe(2); |
| 123 | + expect(document.querySelector(".current a")).toBe(it222); |
| 124 | + |
| 125 | + instance.clear_items(); |
| 126 | + instance.mark_items("https://patternslib.com/path3"); |
| 127 | + |
| 128 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 129 | + expect(document.querySelectorAll(".inPath").length).toBe(0); |
| 130 | + expect(document.querySelector(".current a")).toBe(it3); |
| 131 | + |
| 132 | + instance.clear_items(); |
| 133 | + instance.mark_items("https://patternslib.com/path4"); |
| 134 | + |
| 135 | + expect(document.querySelectorAll(".current").length).toBe(1); |
| 136 | + expect(document.querySelectorAll(".inPath").length).toBe(0); |
| 137 | + expect(document.querySelector(".current a")).toBe(it4); |
| 138 | + }); |
| 139 | +}); |
0 commit comments