Skip to content

Commit

Permalink
Implement missing router functions (#2393)
Browse files Browse the repository at this point in the history
* Implement missing router functions

- Implement `Router.setQueryParam()`
- Fix `Router.getRouteName()`

* Fix function name for `setQueryParams`

* Fixes and tests

- Added jest config to package.json
- Added some jest unit tests for Router
- Added mocks for some dependencies
- Fixed route matching in Router.isActiveClassName method

* fix package.json to run tests (#2396)

* Removed comment

* Remove duplicated setQueryParams function

Fixed variable
  • Loading branch information
mikemurray authored and brent-hoover committed Jun 8, 2017
1 parent 6d88f3c commit 133e764
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 12 deletions.
4 changes: 4 additions & 0 deletions imports/plugins/core/router/__mocks__/meteor/blaze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const Blaze = {
remove() {},
renderWithData() {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class BlazeComponent {}
4 changes: 4 additions & 0 deletions imports/plugins/core/router/__mocks__/meteor/meteor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const Meteor = {
isClient: false,
isServer: true
};
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions imports/plugins/core/router/__mocks__/meteor/tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

class Dependency {
depend() {}
changed() {}
}

export const Tracker = {
Dependency
};
100 changes: 100 additions & 0 deletions imports/plugins/core/router/lib/__tests__/router.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
jest.mock("/lib/collections", () => {
return {
Packages: {},
Shops: {}
};
});

import Router from "../router.js";

beforeEach(() => {
// Set some default routes
Router.setCurrentRoute({});
Router.routes = [
{
fullPath: "/test?filter=red",
query: {
filter: "red"
},
route: {
name: "test",
path: "/test",
route: "/test"
}
},
{
params: {
id: "a12345"
},
query: {
filter: "green"
},
route: {
fullPath: "/reaction/test/1234?filter=green",
group: {
prefix: "/reaction"
},
name: "singleTestItem",
path: "/reaction/test/12345",
route: "/reaction/test/:id"
}
}
];
});

afterEach(() => {
jest.clearAllMocks();
});


test("Route name to equal test", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[0]);

expect(Router.getRouteName()).toBe("test");
});

test("Route query param 'filter' to equal 'red'", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[0]);

const queryValue = Router.getQueryParam("filter");

expect(queryValue).toBe("red");
});

test("Route /test/:id param 'id' to equal '12345'", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[1]);

const received = Router.getParam("id");

expect(received).toBe("a12345");
});

test("Route is active with name 'singleTestItem'", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[1]);

const received = Router.isActiveClassName("singleTestItem");

expect(received).toBe("active");
});

test("Route /test/:id is active with prefixed path '/reaction/test/12345'", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[1]);

const received = Router.isActiveClassName("/reaction/test/12345");

expect(received).toBe("active");
});

test("Route /test is active with un-prefixed path '/test'", () => {
// TODO: Update test when server router is implemented to use Router.go()
Router.setCurrentRoute(Router.routes[0]);

const received = Router.isActiveClassName("/test");

expect(received).toBe("active");
});
46 changes: 34 additions & 12 deletions imports/plugins/core/router/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Router {
static Hooks = Hooks
static routes = []
static _initialized = false;
static activeClassName = "active";

static set _routes(value) {
Router.routes = value;
Expand Down Expand Up @@ -70,7 +71,7 @@ class Router {
static getRouteName() {
const current = Router.current();

return current.options && current.options.name || "";
return current.route && current.route.name || "";
}

static getParam(name) {
Expand All @@ -87,6 +88,19 @@ class Router {
return current.query && current.query[name] || undefined;
}

static setQueryParams(newParams) {
const current = Router.current();
const queryParams = Object.assign({}, current.query, newParams);

for (const key in queryParams) {
if (queryParams[key] === null || queryParams[key] === undefined) {
delete queryParams[key];
}
}

Router.go(current.route.name, current.params, queryParams);
}

static watchPathChange() {
routerChangeDependency.depend();
}
Expand Down Expand Up @@ -199,18 +213,26 @@ Router.reload = () => {
Router.isActiveClassName = (routeName) => {
const current = Router.current();
const group = current.route.group;
const path = current.route.path;
let prefix;
let prefix = "";

if (group && group.prefix) {
prefix = current.route.group.prefix;
} else {
prefix = "";
}
if (current.route) {
const path = current.route.path;

if (typeof path === "string") {
const routeDef = path.replace(prefix + "/", "");
return routeDef === routeName ? "active" : "";
if (group && group.prefix) {
prefix = current.route.group.prefix;
}

// Match route
if (prefix.length && routeName.startsWith(prefix) && path === routeName) {
// Route name is a path and starts with the prefix. (default '/reaction')
return Router.activeClassName;
} else if (routeName.startsWith("/") && path === routeName) {
// Route name isa path and starts with slash, but was not prefixed
return Router.activeClassName;
} else if (current.route.name === routeName) {
// Route name is the actual name of the route
return Router.activeClassName;
}
}

return "";
Expand All @@ -236,7 +258,7 @@ function hasRoutePermission(route) {


/**
* getRouteName
* getRegistryRouteName
* assemble route name to be standard
* prefix/package name + registry name or route
* @param {String} packageName [package name]
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,12 @@
"del-report": "rm -rf ~/allure-report && rm -rf allure-results",
"test-local": "wdio tests/runner/local.conf.js",
"test-browserstack": "wdio tests/runner/browserstack.conf.js; kill $(ps aux | grep '[b]rowserstack' | awk '{print $2}')"
},
"jest": {
"moduleNameMapper": {
"^\/lib(.*)": "<rootDir>/lib/$1",
"^\/imports\/plugins(.*)": "<rootDir>/imports/plugins/$1",
"^meteor/(.*)": "<rootDir>/imports/plugins/core/router/__mocks__/meteor/$1"
}
}
}

0 comments on commit 133e764

Please sign in to comment.