Skip to content

Commit

Permalink
fix: πŸ› router.route is now overridable via application (#39)
Browse files Browse the repository at this point in the history
* fix: πŸ› router.route is now overridable via application

This should resolve bug, where overriden router.route method would not
propagate in integration test run, since it is overriden in this plugin
also.

* test: πŸ’ fix tests for bind extension

* test: πŸ’ no need to define async when not using await
  • Loading branch information
Filipoliko authored Jul 27, 2020
1 parent 1a02913 commit 8f73cc0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
.idea/
.history/
.vscode/
node_modules/
dist/
coverage/
package-lock.json
**/node_modules/
**/dist/
**/doc/
**/coverage/
**/package-lock.json
lerna-debug.log
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"release:publish": "lerna publish from-package --yes"
},
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/core": "^7.10.5",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@babel/preset-react": "^7.10.4",
"@commitlint/cli": "^9.0.1",
"@commitlint/config-conventional": "^9.0.1",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@ima/core": "^17.6.0",
"@ima/gulp-task-loader": "^17.7.0",
"@rollup/plugin-babel": "^5.0.4",
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-babel": "^5.1.0",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^8.1.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-replace": "^2.3.3",
"babel-eslint": "^10.1.0",
"babel-preset-jest": "^26.1.0",
Expand All @@ -33,12 +33,12 @@
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.5.0",
"eslint": "^7.4.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "6.11.0",
"eslint-plugin-jasmine": "^4.1.1",
"eslint-plugin-jest": "^23.18.0",
"eslint-plugin-jest": "^23.18.2",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "7.20.3",
"eslint-plugin-react": "7.20.4",
"git-cz": "^4.7.0",
"gulp": "^4.0.2",
"gulp-babel": "8.0.0",
Expand All @@ -54,7 +54,7 @@
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"rollup": "^2.20.0",
"rollup": "^2.23.0",
"rollup-plugin-copy": "^3.3.0",
"rollup-plugin-peer-deps-external": "^2.2.3",
"to-mock": "^1.6.2"
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-testing-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"dependencies": {
"@ima/helpers": "^17.4.0",
"globby": "^11.0.0",
"jsdom": "^16.2.1"
"jsdom": "^16.2.1",
"to-aop": "^0.3.5"
},
"peerDependencies": {
"@ima/core": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import initBindApp from '../bind';
describe('Bind', () => {
it('can override router.route method to propagate route navigation to jsdom', () => {
const baseUrl = 'https://www.example.com';
const routeSpy = jest.fn();
class Router {
getBaseUrl() {
return baseUrl;
}

route(...args) {
return routeSpy(...args);
}
}
const path = '/my/test/path';
const route = jest.fn();
const $Router = {
route,
getBaseUrl: jest.fn().mockReturnValue(baseUrl)
};
const $Router = new Router();
const $PageManager = { _managedPage: {} };
const objects = { $Router, $PageManager };
const oc = {
Expand All @@ -24,8 +30,7 @@ describe('Bind', () => {

/* eslint-disable-next-line no-undef */
expect(jsdom.reconfigure).toHaveBeenCalledWith({ url: baseUrl + path });
expect($Router.route).not.toEqual(route);
expect(route).toHaveBeenCalled();
expect(routeSpy).toHaveBeenCalledWith(path);

delete global.jsdom;
});
Expand Down
37 changes: 21 additions & 16 deletions packages/plugin-testing-integration/src/extensions/bind.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
export default (ns, oc) => {
const pageManager = oc.get('$PageManager');
const router = oc.get('$Router');
const nativeRoute = router.route.bind(router);
import { aop, hookName, createHook } from 'to-aop';

router.route = (path, ...args) => {
const isFirstNavigation = !pageManager._managedPage.controller;
export default (ns, oc) => {
const Router = oc.get('$Router').constructor;
const routeHook = createHook(
hookName.beforeMethod,
'route',
({ args, context }) => {
const pageManager = oc.get('$PageManager');
const isFirstNavigation = !pageManager._managedPage.controller;
const path = args[0];

// We have to set correct url in jsdom for first application
// navigation to simulate browser behavior, where you
// already have correct url set in address bar.
if (isFirstNavigation) {
/* eslint-disable-next-line no-undef */
jsdom.reconfigure({
url: router.getBaseUrl() + path
});
// We have to set correct url in jsdom for first application
// navigation to simulate browser behavior, where you
// already have correct url set in address bar.
if (isFirstNavigation) {
/* eslint-disable-next-line no-undef */
jsdom.reconfigure({
url: context.getBaseUrl() + path
});
}
}
);

return nativeRoute(path, ...args);
};
aop(Router, routeHook);
};

0 comments on commit 8f73cc0

Please sign in to comment.