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

fix: 🐛 component re-render when updating fields in lifecycle hook #646

Merged
merged 2 commits into from Mar 20, 2024

Conversation

kfrancois
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[X] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

See #643

When Spectator is used to set inputs on a component, the following will happen:

  1. ChangeDetectorRef.detectChanges() is called
  2. fixture.detectChanges is called

Issue Number: #643

What is the new behavior?

I simply swapped these 2 calls:

  1. fixture.detectChanges will run a change detection cycle on the test fixture. This is when ngOnChanges will run
  2. ChangeDetectorRef.detectChanges() will check the component view. This is where the change detection check will detect the updated field from ngOnChanges and re-render the component.

I wish I could give a better explanation to why this happens, but this is my interpretation of the bug/fix

Does this PR introduce a breaking change?

[ ] Yes
[X] No

Other information

Copy link

stackblitz bot commented Mar 14, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@kfrancois
Copy link
Contributor Author

Seeing 1 test fail which did succeed locally - will investigate when I have time after work.

@kfrancois
Copy link
Contributor Author

@NetanelBasal could you check this failing CI run? The failing test succeeds locally, I wonder if the flakiness is due to CI.

One weird thing I'm seeing is that the CI job runs 290 tests, while locally I only have 280 tests 🤔

@NetanelBasal
Copy link
Member

Maybe there is a skip some where?

@kfrancois
Copy link
Contributor Author

Got it, I simply didn't rebase on latest master - I messed up in this commit 3f63c68 by focusing a test in Jasmine (which means all other tests were skipped), and in the meantime a test got broken. Should be g2g now!

`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NgOnChangesInputComponent {

Choose a reason for hiding this comment

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

Suggested change
export class NgOnChangesInputComponent {
export class OnPushComponent {

I think thats more to the point.

Copy link
Member

Choose a reason for hiding this comment

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

Correct, but let's not wait for this fix

@NetanelBasal NetanelBasal merged commit e522c99 into ngneat:master Mar 20, 2024
3 checks passed
@kfrancois kfrancois deleted the patch-ngonchanges-render branch March 20, 2024 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants