Repository navigation
fix(upgrade): need to take base uri into account in setUpLocationSync - #20244
observstream wants to merge 5 commits into
Conversation
|
I had one question. This function isn't being tested anywhere in Angular that I can find. I am not sure if I should make an entire test file and write new tests for all of the functionality. |
|
The logic on this is pretty sound but there is a problem with the commit as is. If pulled in as is you will still get the infinite redirect. Until the url.href is set (like it is on |
|
I have submitted a fix for the issues listed. Please recheck to see if this fixes this bug. |
gkalpak
left a comment
There was a problem hiding this comment.
Thx for looking into this 👍
I left a couple of comments. And yes, we should definitely have tests for this kind of stuff 😕
Ideally, we should have both unit and integration tests. But either is better than nothing 😁
Feel free to add a new spec file with tests ❤️
There was a problem hiding this comment.
The indentation change causes linting to fail.
There was a problem hiding this comment.
Thanks for looking at this! I just got back from a trip and will push a fix for this over the weekend.
There was a problem hiding this comment.
I think the general direction (stripping baseHref from the URL) is correct, but I think it would be better to use something like Location#normalize() for that. This ensures that there is no unnecessary duplication (of code and runtime overhead) and provides a more robust implementation (for examples it takes APP_BASE_HREF into account if provided).
WDYT?
There was a problem hiding this comment.
That sounds like a much better solution. I will make those changes and write unit tests for them. They should be up either this weekend or early next week.
|
@jahumes, any update on this? Will you find time to work on this in the near future or should someone else take it on? |
|
I have done what you have requested with the change and that helped greatly make the code much smaller and normalized. I have been having a hard time with the writing of tests (which is why this has taken so long to push). I seem to be unable to get my tests to run at all. When I create a new file (packages/router/test/upgrade.spec.ts), none of the tests I create in there are run during |
|
I think the tests inside If you need If you find any difficulties (or if you don't have time to work on this atm and want someone else to take it over), just let me know 😉 |
|
Thanks @gkalpak! I will look at this and if I can't get to it by next week, I will let you know. |
|
It would be awesome to get this merged, I've just spent two days hunting down this bug :-/. Unfortunately I only found this issue after I traced the bug and solved the issue. Anything I can do to help so that others can benefit? |
|
@mafredi, currently this PR is missing tests. If @jahumes doesn't have time to work on this atm, perhaps you could give it a shot (adding tests)? |
|
@gkalpak Do these tests work? Is there anything else you would like me to do? |
gkalpak
left a comment
There was a problem hiding this comment.
A few minor questions/suggestions. Looks pretty good overall 👍
There was a problem hiding this comment.
Is this change really necessary? AFAICT, @angular/upgrade/static is not used in @angular/router/testing, is it?
There was a problem hiding this comment.
This is a mistake from all the testing I had to do to figure out how to get the tests to run successfully
There was a problem hiding this comment.
Is this change really necessary? AFAICT, @angular/upgrade/static is not used in @angular/router/testing, is it?
There was a problem hiding this comment.
Same as the above issue.
There was a problem hiding this comment.
Why is this needed? What is requesting stuff from upgrade/static/src/...?
There was a problem hiding this comment.
The new test file upgrade.spec.ts requests this. I will review with the below request and see if I can get it to work.
There was a problem hiding this comment.
After reviewing this, this is required because the spec file needs the UpgradeModule for the test bed.
See line 31 of upgrade.spec.ts
There was a problem hiding this comment.
UpgradeModule is imported from @angular/upgrade/static. Why isn't dist/all/@angular/upgrade/static/*.js enough?
There was a problem hiding this comment.
So, without both of these lines, Karma can't find all of necessary files. In karma-test-shim.js, we are linking the module @angular/upgrade/static to the index.js files which includes file from the src directory. (See here)
If I remove either one of them, I get the following error:
ERROR: Error{__zone_symbol__currentTask: ZoneTask{_zone: Zone{_properties: ..., _parent: ..., _name: ..., _zoneDelegate: ...}, runCount: 0, _zoneDelegates: null, _state: 'notScheduled', type: 'microTask', source: 'Promise.then', data: undefined, scheduleFn: undefined, cancelFn: null, callback: function () { ... }, invoke: function () { ... }}}
There was a problem hiding this comment.
Do the new tests run under bazel test //... ?
There was a problem hiding this comment.
ping! we ought to make sure any added tests are being run by circleci
There was a problem hiding this comment.
👍 I'll make sure tests are run and then mark for merge.
There was a problem hiding this comment.
I think this file should be in packages/router/upgrade/test/.... From what I see in other similar packages, (e.g. packages/animations/browser/), test/ should be next to the corresponding src/.
There was a problem hiding this comment.
Good idea. I will make this change.
There was a problem hiding this comment.
This test is redundant (since this is implicitly verified by the test below).
There was a problem hiding this comment.
I will remove it.
There was a problem hiding this comment.
Nit: Since these three vars ($injector, injector, callback) are not necessary for most tests, I think it would make the tests cleaner if you removed them from here (and in beforeEach()) and get them only in the tests that actually use them.
There was a problem hiding this comment.
Easy change to make.
There was a problem hiding this comment.
Nit: Since callback is only needed it one test, this could be simplified as:
get: jasmine.createSpy('$injector.get').and.returnValue({'$on': () => undefined})There was a problem hiding this comment.
Easy change to make.
There was a problem hiding this comment.
Are you talking about spying on the injector.get? I am doing this primarily to isolate the test from any side effects. I will try removing it to see if I can get away with it.
There was a problem hiding this comment.
Nothing should be calling injector.get (and even if it did, we get a new injector for each test anyway).
There was a problem hiding this comment.
These two assertions are redundant imo. For example, you can implicitly test this, by changing the $injector.get spy to:
$injector.get.and.callFake((name: string) => (name === '$rootScope') && $rootScope)(Keeping the tests focused on what matters most, makes them easier to follow.)
There was a problem hiding this comment.
I have made this change.
|
@gkalpak Thanks for the feedback! I will revisit and send you responses and fixes shortly. |
|
@gkalpak I have made the changes you have requested. Please review and let me know if there is anything I have missed or still need to change. |
There was a problem hiding this comment.
This change is wrong. We do want files from .../@angular/upgrade/test.
It should be .../@angular/router/upgrade/test/**. Or maybe you could change the above line to .../@angular/router/**/test/**.
There was a problem hiding this comment.
So sorry! I will make this change.
There was a problem hiding this comment.
UpgradeModule is imported from @angular/upgrade/static. Why isn't dist/all/@angular/upgrade/static/*.js enough?
There was a problem hiding this comment.
No need for this. $injector.get is already a spy 😃
There was a problem hiding this comment.
Thanks. I had to modify line 31 above to not use a fat arrow function and that allowed me to remove it.
There was a problem hiding this comment.
Nothing should be calling injector.get (and even if it did, we get a new injector for each test anyway).
gkalpak
left a comment
There was a problem hiding this comment.
Thx for the quick turnaround, @jahumes 👍
Almost there 😃
There was a problem hiding this comment.
Nit: I would simplify this test like this (but it is totally optional 😁):
const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']);
upgradeModule.$injector.get.and.returnValue($rootScope);
LocationMock.normalize.and.returnValue(pathname);
setUpLocationSync(upgradeModule);
const url = 'https://google.com';
const pathname = '/custom/route';
const query = '?query=1&query2=3';
const hash = '#new/hash';
const callback = $rootScope.$on.calls.argsFor(0)[1];
callback({}, url + pathname + query + hash, '');
expect(LocationMock.normalize).toHaveBeenCalledTimes(1);
expect(LocationMock.normalize).toHaveBeenCalledWith(pathname);
expect(RouterMock.navigateByUrl).toHaveBeenCalledTimes(1);
expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(pathname + query + hash);There was a problem hiding this comment.
Thanks! I am always happy to see new ways to do things. I wasn't aware about the argsFor functionality. That makes it much easier.
|
@gkalpak I have pushed another set of small changes based on your advice. |
|
Thanks for your efforts @jahumes and @gkalpak, excited to see this moving along. My co-worker just noticed that this does not work in IE 11. The problem lies in using The
In IE, I'm thinking something like: let path = url.pathname;
if (!path.startsWith('/')) {
path = `/${path}`;
}
path = location.normalize(path);Would that cause any other problems, can we assume there always should be a leading slash? This fix works for us though. |
|
@gregmagolan could you help them green this up? |
Normalize the full URL (including the base uri) before passing it to `router.navigateByUrl()`. Fixes angular#20061
a8986c1 to
2f07677
Compare
2f07677 to
d2af25f
Compare
|
It should be green now. We just need another |
|
A Googler has manually verified that the CLAs look good. (Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.) |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
…ular#20244) Normalize the full URL (including the base uri) before passing it to `router.navigateByUrl()`. Fixes angular#20061 PR Close angular#20244
The url.pathname passes the base uri as part of the url instead of removing it
Fixes #20061
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
I am modifying the setUpLocationSync in packages/router/upgrade/src/upgrade.ts to take the base tag into account when navigating by url
Issue Number: 20061
What is the new behavior?
The base tag is removed from the pathname before the router navigates to the specified url. This stops an infinite loop from occurring when navigating between Angular and AngularJS.
Does this PR introduce a breaking change?
Other information