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

feat(cdk/testing): add method to wait for async tasks outside angular to complete #17408

Merged

Conversation

devversion
Copy link
Member

@devversion devversion commented Oct 15, 2019

Adds a new method to the ComponentHarness base class that can be
used to wait for async tasks to complete. This is useful for harness
authors as sometimes the component schedules tasks outside of the
Angular zone which therefore won't be captured by whenStable.

These tasks can be relevant for the harness logic though, so there
should be an option for harness authors to await all async tasks
(not only the ones captured in the ng zone).

e.g. the slider re-adjusts when the directionality changes. This
re-rendering happens in the next tick. To make sure that setValue
works when the directionality changed, we need to wait for the async
tasks to complete, so that the slider completed re-rendering and the
value can be changed through a mouse click.

@devversion devversion added the in progress This issue is currently in progress label Oct 15, 2019
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 15, 2019
@devversion devversion force-pushed the fix/stabilize-cdk-testing-testbed-harness branch from 4d959b0 to 22f5294 Compare October 16, 2019 12:49
@devversion devversion marked this pull request as ready for review October 16, 2019 12:54
@devversion devversion added target: major This PR is targeted for the next major release and removed in progress This issue is currently in progress labels Oct 16, 2019
@devversion devversion added this to the 9.0.0 milestone Oct 16, 2019
@mmalerba mmalerba added the P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful label Oct 16, 2019
@devversion devversion force-pushed the fix/stabilize-cdk-testing-testbed-harness branch 2 times, most recently from b53d8d3 to ee7c989 Compare October 18, 2019 12:04
@devversion devversion requested a review from a team as a code owner October 18, 2019 12:04
@devversion devversion changed the title fix(cdk/testing): testbed harness stabilizing not handling tasks outside of NgZone feat(cdk/testing): add method to wait for async tasks to complete Oct 18, 2019
Copy link
Contributor

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

This looks good to me, just a naming suggestion. We should see what others think for the name too

@devversion devversion assigned jelbourn and unassigned devversion Oct 18, 2019
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM aside from the naming discussion

@devversion devversion force-pushed the fix/stabilize-cdk-testing-testbed-harness branch from df5c2dc to d5409f3 Compare October 18, 2019 17:58
@mmalerba mmalerba added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Oct 18, 2019
@mmalerba
Copy link
Contributor

@devversion Inside google3 it appears that the actual typings from the angular/angular repo (packages/zone.js/lib/zone.d.ts) are present. That's causing the compiler to complain that you're re-declaring them in this PR. Is there a way that we can depend on the real typings?

@jelbourn If not, how do you think we should handle this? just copybara out the import of the .d.ts file created in this PR?

@devversion
Copy link
Member Author

devversion commented Oct 18, 2019

@mmalerba I had logic to access the zone.js types, but it actually caused the whole cdk compilation unit (in legacy gulp) to break because zone types brought in the node types. I tried excluding them somehow, but wasn't able to get it work.

@jelbourn
Copy link
Member

Omitting those type from the sync seems pretty trivial

@devversion for the naming, I think that waitForTasksOutsideAngular does explain what the function does from the perspective of the people using it. The majority of people don't know much about zones, with some knowing enough to do runOutsideAngular, so I think this will align with people's idea of what's happening

@devversion
Copy link
Member Author

devversion commented Oct 18, 2019

@jelbourn We said in the meeting that the case that someone uses runOutsideAngular is uncommon, hence explicitly running tasks outside Angular is probably a bit more advanced and involves a basic understanding of zones/how CD works.

I'm not in particular saying we should mention zone in the name, nor do I say that my initial proposal is the best. I totally see why you want it to be named waitForTasksOutsideAngular. Though I personally disagree that the name is semantically correct. It's describing only a subset of the action that is performed by calling this method (hence doing more than expected.. which can result in unexpected behavior in cases we did not think of). This is just my thinking, and its good that we discuss it. I think both names have trade-offs, so I'm fine with what you/Miles prefer 😄

@mmalerba
Copy link
Contributor

@devversion I think one thing that kind of mitigates your concern is the fact that waiting for the NgZone always happens, even if you don't call this method. So yes, while you're technically right, the only difference a user will notice is the fact that it waits for stuff outside the NgZone too, if they do call this method

@devversion
Copy link
Member Author

devversion commented Oct 18, 2019

@mmalerba But not necessarily. In more advanced harnesses where the provided default platform-agnostic TestElement is not sufficient and the platform is accessed directly, the NgZone stabilizing does not run automatically. In those cases, the method will do more than expected and rather confuse people on why other tasks are awaited too.

As said, I see your reasoning. It will be helpful for most cases, but I personally prefer method names to not be based on their effect when called in specific scenarios, but rather based on what is done regardless of the callee context. Not trying to argue on the name, just trying to explain my standpoint.

@mmalerba
Copy link
Contributor

I don't think people should ever be accessing the platform directly in harness code. The harnesses are meant to be platform-independent. I'm sure you're right that someone will try to do that, but its not really a use case I think we need to be concerned about supporting

@devversion
Copy link
Member Author

devversion commented Oct 18, 2019

Fair point. My thinking was that harnesses can be also built just for one platform though (i.e. with the goal of just making breaking changes less painful in tests / or just making test authoring easier).
Above was just one example (who know what other scenarios there are). I guess it's more of a best-practice question for me. My thinking is probably just different on how method names should be called. Anyway, I'm good with that name as it will work in most of the cases just fine.

export type ZoneDelegate = Object;
export type HasTaskState = {microTask: boolean, macroTask: boolean};

export interface ProxyZoneStatic {
Copy link
Contributor

Choose a reason for hiding this comment

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

@devversion It looks like ProxyZoneStatic and ProxyZone are not part of the real zone.js d.ts file. Can you move these to a separate file (either task-state-zone-interceptor.ts or some new file like proxy-zone-types.d.ts). I need to be able to easily strip away just the ones that are part of the real zone.js d.ts file for google3.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. I've split the ProxyZone types into a separate file.

… to complete

Adds a new method to the `ComponentHarness` base class that can be
used to wait for async tasks to complete. This is useful for harness
authors as sometimes the component schedules tasks outside of the
Angular zone which therefore won't be captured by `whenStable`.

These tasks can be relevant for the harness logic though, so there
should be an option for harness authors to await _all_ async tasks
(not only the ones captured in the ng zone).

e.g. the slider re-adjusts when the directionality changes. This
re-rendering happens in the next tick. To make sure that `setValue`
works when the directionality changed, we need to wait for the async
tasks to complete, so that the slider completed re-rendering and the
value can be changed through a mouse click.
@devversion devversion changed the title feat(cdk/testing): add method to wait for async tasks to complete feat(cdk/testing): add method to wait for async tasks outside angular to complete Oct 19, 2019
@devversion devversion force-pushed the fix/stabilize-cdk-testing-testbed-harness branch from d5409f3 to 48daa8f Compare October 19, 2019 08:49
@mmalerba mmalerba merged commit c50aa21 into angular:master Oct 21, 2019
@devversion devversion deleted the fix/stabilize-cdk-testing-testbed-harness branch October 22, 2019 09:44
@angular-automatic-lock-bot
Copy link

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 Nov 22, 2019
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 cla: yes PR author has agreed to Google's Contributor License Agreement P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants