__tests__/router.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
const mod = require("../h3.js"); const h3 = mod.h3; const h = mod.h; let preStartCalled = false; let postStartCalled = false; let count = 0; let result = 0; const setCount = () => { count = count + 2; h3.dispatch("count/set", count); }; let hash = "#/c2"; const mockLocation = { get hash() { return hash; }, set hash(value) { const event = new CustomEvent("hashchange"); event.oldURL = hash; event.newURL = value; hash = value; window.dispatchEvent(event); }, }; const C1 = () => { const parts = h3.route.parts; const content = Object.keys(parts).map((key) => h("li", `${key}: ${parts[key]}`) ); return h("ul.c1", content); }; const C2 = () => { const params = h3.route.params; const content = Object.keys(params).map((key) => h("li", `${key}: ${params[key]}`) ); return h("ul.c2", content); }; describe("h3 (Router)", () => { beforeEach(async () => { const preStart = () => (preStartCalled = true); const postStart = () => (postStartCalled = true); await h3.init({ routes: { "/c1/:a/:b/:c": C1, "/c2": C2, }, location: mockLocation, preStart: preStart, postStart: postStart, }); }); it("should support routing configuration at startup", () => { expect(h3.route.def).toEqual("/c2"); }); it("should support pre/post start hooks", () => { expect(preStartCalled).toEqual(true); expect(postStartCalled).toEqual(true); }); it("should support the capturing of parts within the current route", (done) => { const sub = h3.on("$redraw", () => { expect( document.body.childNodes[0].childNodes[1].textContent ).toEqual("b: 2"); sub(); done(); }); mockLocation.hash = "#/c1/1/2/3"; }); it("should expose a navigateTo method to navigate to another path", (done) => { const sub = h3.on("$redraw", () => { expect( document.body.childNodes[0].childNodes[1].textContent ).toEqual("test2: 2"); sub(); done(); }); h3.navigateTo("/c2", { test1: 1, test2: 2 }); }); it("should throw an error if no route matches", async () => { try { await h3.init({ element: document.body, routes: { "/c1/:a/:b/:c": () => h("div"), "/c2": () => h("div"), }, }); } catch (e) { expect(e.message).toMatch(/No route matches/); } }); it("should execute setup and teardown methods", (done) => { let redraws = 0; C1.setup = (cstate) => { cstate.result = cstate.result || 0; cstate.sub = h3.on("count/set", (state, count) => { cstate.result = count * count; }); }; C1.teardown = (cstate) => { cstate.sub(); result = cstate.result; return { result: cstate.result }; }; const sub = h3.on("$redraw", () => { redraws++; setCount(); setCount(); if (redraws === 1) { expect(count).toEqual(4); expect(result).toEqual(0); h3.navigateTo("/c2"); } if (redraws === 2) { expect(count).toEqual(8); expect(result).toEqual(16); delete C1.setup; delete C1.teardown; sub(); done(); } }); h3.navigateTo("/c1/a/b/c"); }); }); |