Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement missing router functions #2393

Merged
merged 9 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment relevant to every test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's relevant to every test. I can deduplicate if it's an issue.

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"
}
}
}