Skip to content

fix/refactor(common): prevent duplicate URL change notifications from SpyLocation and use TestBed.inject in the Location test suite#37459

Closed
LayZeeDK wants to merge 2 commits into
angular:masterfrom
LayZeeDK:LayZeeDK/refactor/common-refactor-private-members-and-use-testbed
Closed

fix/refactor(common): prevent duplicate URL change notifications from SpyLocation and use TestBed.inject in the Location test suite#37459
LayZeeDK wants to merge 2 commits into
angular:masterfrom
LayZeeDK:LayZeeDK/refactor/common-refactor-private-members-and-use-testbed

Conversation

@LayZeeDK

@LayZeeDK LayZeeDK commented Jun 5, 2020

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

  1. SpyLocation emits duplicate notifications to URL change listeners when multiple are registered.
  2. Location uses the private acces modifier for the private _urlChangeSubscription member.
  3. location_spec.ts uses the weakly typed inject to resolve dependencies.

Issue Number: As discussed in #37404

What is the new behavior?

  1. SpyLocation only emits one URL change notification to all listeners.
  2. Location use @internal JSDoc annotations for all private members.
  3. location_spec.ts uses the strongly typed TestBed.inject to resolve dependencies.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Cc @atscott

@LayZeeDK LayZeeDK force-pushed the LayZeeDK/refactor/common-refactor-private-members-and-use-testbed branch 3 times, most recently from 68e51d2 to c8f6895 Compare June 5, 2020 12:43
@LayZeeDK

LayZeeDK commented Jun 5, 2020

Copy link
Copy Markdown
Contributor Author

Hey @atscott,

I've tried different things to get around this, but several other tests are broken by switching from @internal annotations to private access modifiers because of TypeScript weirdness when using MyClass implements MyOtherClass.

packages/router/test/integration.spec.ts:1225:27 - error TS2352: Conversion of type 'Location' to type 'SpyLocation' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
 

 
1225          const location = TestBed.inject(Location) as SpyLocation;
 
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
packages/router/test/integration.spec.ts:1252:27 - error TS2352: Conversion of type 'Location' to type 'SpyLocation' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
 

 
1252          const location = TestBed.inject(Location) as SpyLocation;
 
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
packages/router/test/integration.spec.ts:1318:25 - error TS2352: Conversion of type 'Location' to type 'SpyLocation' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
 
  Type 'Location' is missing the following properties from type 'SpyLocation': urlChanges, _history, _historyIndex, setInitialPath, and 3 more.
 

 
1318        const location = TestBed.inject(Location) as SpyLocation;
 
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

This will break a lot of userland tests as well as SpyLocation is the most important stub for working with the RouterTestingModule.

What do we do? TypeScript suggests to use SpyLocation extends Location instead. We could do that, but it seems odd and might introduce additional work.

@atscott

atscott commented Jun 5, 2020

Copy link
Copy Markdown
Contributor

Hi @LayZeeDK Hmmm, I see. Thanks for investigating this. After taking a closer look, I think maybe @internal really is the correct access modifier. I don't think extends is quite right because we really don't want to use any of the real Location implementations in the SpyLocation and in addition, we still can't have separate implementations of the private members with extends and I don't think exposing them to things outside the package with protected is desired either.

Sorry for all this back and forth! I think @internal is correct and that you actually will need to change _urlChangeSubscription to @internal as well. In addition to learning a bit more about these interactions, another good thing that came out of this is that I noticed you need the same subscription fix using _urlChangeSubscription for SpyLocation that you added for Location in the previous PR. Could you do that here?

LayZeeDK added 2 commits June 5, 2020 23:24
Use the strongly typed TestBed.inject rather than the weakly typed inject test utility function. Reuse injected dependency variables between sibling test cases.
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange.

Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of  `SpyLocation implements Location`.
@LayZeeDK LayZeeDK force-pushed the LayZeeDK/refactor/common-refactor-private-members-and-use-testbed branch from c8f6895 to 10af4db Compare June 5, 2020 21:25
@LayZeeDK LayZeeDK changed the title refactor(common): refactor private members in Location and use TestBed.inject in its test suite fix/refactor(common): refactor private members in Location + SpyLocation and use TestBed.inject in its test suite Jun 5, 2020
@LayZeeDK LayZeeDK changed the title fix/refactor(common): refactor private members in Location + SpyLocation and use TestBed.inject in its test suite fix/refactor(common): prevent duplicate URL change notifications from SpyLocation and use TestBed.inject in the Location test suite Jun 5, 2020
@LayZeeDK LayZeeDK marked this pull request as ready for review June 5, 2020 21:47
@pullapprove pullapprove Bot requested a review from kara June 5, 2020 21:47
@LayZeeDK

LayZeeDK commented Jun 5, 2020

Copy link
Copy Markdown
Contributor Author

🏎️ Here we go, @atscott. Let's do this!

@pullapprove pullapprove Bot requested a review from AndrewKushnir June 6, 2020 02:25

@atscott atscott left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great, thanks so much for working on this!

@atscott atscott added area: common Issues related to APIs in the @angular/common package action: presubmit The PR is in need of a google3 presubmit target: patch This PR is targeted for the next patch release labels Jun 8, 2020
@ngbot ngbot Bot modified the milestone: needsTriage Jun 8, 2020

@AndrewKushnir AndrewKushnir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 👍

LayZeeDK referenced this pull request Jun 8, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Close #37404
@atscott atscott added action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels Jun 8, 2020
@atscott atscott removed the request for review from kara June 8, 2020 22:04
@atscott atscott closed this in 0b78459 Jun 9, 2020
atscott pushed a commit that referenced this pull request Jun 9, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange.

Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of  `SpyLocation implements Location`.

PR Close #37459
atscott pushed a commit that referenced this pull request Jun 9, 2020
Use the strongly typed TestBed.inject rather than the weakly typed inject test utility function. Reuse injected dependency variables between sibling test cases.

PR Close #37459
atscott pushed a commit that referenced this pull request Jun 9, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange.

Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of  `SpyLocation implements Location`.

PR Close #37459
@LayZeeDK LayZeeDK deleted the LayZeeDK/refactor/common-refactor-private-members-and-use-testbed branch June 9, 2020 19:37
ngwattcos pushed a commit to ngwattcos/angular that referenced this pull request Jun 25, 2020
Use the strongly typed TestBed.inject rather than the weakly typed inject test utility function. Reuse injected dependency variables between sibling test cases.

PR Close angular#37459
ngwattcos pushed a commit to ngwattcos/angular that referenced this pull request Jun 25, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange.

Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of  `SpyLocation implements Location`.

PR Close angular#37459
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jul 10, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
Use the strongly typed TestBed.inject rather than the weakly typed inject test utility function. Reuse injected dependency variables between sibling test cases.

PR Close angular#37459
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange.

Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of  `SpyLocation implements Location`.

PR Close angular#37459
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package cla: yes target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants