Skip to content

Commit

Permalink
Fixed circular dependency warning
Browse files Browse the repository at this point in the history
  • Loading branch information
escheiermann committed Sep 15, 2022
1 parent 0f60bdf commit d852b05
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 56 deletions.
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { setRoute } from "./router/routes";
import { updateRoute } from "./router/routes";

window.onpopstate = setRoute;
window.onpopstate = updateRoute;

export { defineRoutes } from "./router/routes";
export { navigate } from "./router/navigate";
export { defineRoutes, navigate } from "./router/routes";
export { default } from "./router/Router.svelte";
5 changes: 1 addition & 4 deletions src/router/guard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { navigate } from "./navigate";

/**
* Checks if route can be accessed.
* @returns true if route can be accessed.
Expand All @@ -9,8 +7,7 @@ export function guardRoute(route) {
for (let i = 0; i < route.guards.length; i++) {
const path = route.guards[i](route.path);
if (path !== true) {
navigate(path);
return false;
return path;
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/router/navigate.js

This file was deleted.

24 changes: 20 additions & 4 deletions src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var allRoutes = [];
*/
export function defineRoutes(routes) {
allRoutes = routes;
setRoute();
updateRoute();
}

/**
Expand All @@ -21,12 +21,28 @@ export function getRoute(path) {
return allRoutes.filter(route => route.path === path)[0];
}

/**
* Navigates to the specified path.
* @param {string} path to the page (e.g "home").
*/
export function navigate(path) {
const newRoute = getRoute(path);
if (newRoute) {
location.hash = newRoute.path;
}
}

/**
* Sets the route for the current page.
*/
export function setRoute() {
export function updateRoute() {
const newRoute = getRoute(location.hash.substring(1));
if (newRoute && guardRoute(newRoute)) {
currentPage.set(newRoute.component);
if (newRoute) {
const guard = guardRoute(newRoute);
if (guard === true) {
currentPage.set(newRoute.component);
} else if (guard !== false) {
navigate(guard);
}
}
}
6 changes: 3 additions & 3 deletions test/guard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ describe("guard", () => {
expect(guard).toBe(false);
});

test("guardRoute should navigate if guards return path", () => {
test("guardRoute should return route path if guards return path", () => {
const expectedPath = "test1";
const route = {path: expectedPath, guards: [function(path) {return expectedPath}]};
defineRoutes([route]);

guardRoute(route);
const reroute = guardRoute(route);

expect(location.hash).toBe("#" + expectedPath);
expect(reroute).toBe(expectedPath);
});
});
22 changes: 0 additions & 22 deletions test/navigate.spec.js

This file was deleted.

46 changes: 39 additions & 7 deletions test/routes.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRoute, defineRoutes, setRoute } from "../src/router/routes";
import { getRoute, defineRoutes, updateRoute, navigate } from "../src/router/routes";
import { currentPage } from "../src/router/page";
import { get } from "svelte/store";

Expand All @@ -19,37 +19,69 @@ describe("routes", () => {
expect(actualRoute).toBe(expectedRoute);
});

test("setRoute should update currentPage", () => {
test("updateRoute should update currentPage", () => {
currentPage.set({});
const route = {path: "test", component: "TestComponent"};
location.hash = route.path;
defineRoutes([route]);

setRoute();
updateRoute();

expect(get(currentPage)).toBe(route.component);
});

test("setRoute should not update currentPage if route is missing", () => {
test("updateRoute should not update currentPage if route is missing", () => {
const component = "TestComp";
currentPage.set(component);
location.hash = "test2";
defineRoutes([]);

setRoute();
updateRoute();

expect(get(currentPage)).toBe(component);
});

test("setRoute should not update currentPage if route guard returns false", () => {
test("updateRoute should not update currentPage if route guard returns false", () => {
const component = "TestComp";
currentPage.set(component);
const route = {path: "test", guards: [function(path) {return false}]};
location.hash = route.path;
defineRoutes([route]);

setRoute();
updateRoute();

expect(get(currentPage)).toBe(component);
});

test("updateRoute should reroute if route guard returns path", () => {
const component = "TestComp";
const rerouteComponent = "RerouteComp";
currentPage.set(component);
const reroute = {path: "reroute", component: rerouteComponent}
const route = {path: "test", guards: [function(path) {return reroute.path}]};
location.hash = route.path;
defineRoutes([route, reroute]);

updateRoute();

expect(get(currentPage)).toBe(rerouteComponent);
});

test("navigate should update location hash for existing route", () => {
const path = "test";
defineRoutes([{path}]);

navigate(path);

expect(location.hash).toBe("#" + path);
});

test("navigate should not update location hash for missing path", () => {
const path = "test";
location.hash = path

navigate("hello");

expect(location.hash).toBe("#" + path);
});
});

0 comments on commit d852b05

Please sign in to comment.