primeng icon indicating copy to clipboard operation
primeng copied to clipboard

AccordionTab throwing ViewDestroyedError during unit testing

Open brian428 opened this issue 7 years ago • 7 comments

I'm submitting a ... (check one with "x")

[X] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Plunkr Case (Bug Reports) Unfortunately, I'm not sure how to create a demo of the problem because the problem only manifests during unit testing. However, the source of the problem is very straightforward:

  1. If a spec completes (e.g. calls done()) before the Accordion finishes animating, a ViewDestroyedError: Attempt to use a destroyed view: detectChanges is thrown.
  2. When the animation finishes, it triggers onToggleDone(). This in turn calls the animation setter, which tries to run change detection (https://github.com/primefaces/primeng/blame/master/src/app/components/accordion/accordion.ts#L77).
  3. If the component has already been destroyed, the ViewDestroyedError is thrown.

It doesn't cause the spec to fail, but it does create a ton of these errors in the log. Perhaps just add an internal property for isDestroyed that is set to true in onDestroy(), and then check to make sure it is still false before calling detectChanges()?

brian428 avatar Aug 09 '19 21:08 brian428

Same problem here (same error coming from onToggleDone()), this only shows up when in debug mode.

sticl avatar Aug 12 '19 08:08 sticl

Hello! I am having the same troubles here, tests are passing but errors are thrown. Is there any fix available to this bug? all help will be appreciated,

Details: Angular cli v 8.1.2 Angular core 8.1.3 Karma 4.2.0 Jasmine core 3.4.0 Prime NG v 8.0.2

Thanks

cicciogeppo88 avatar Aug 16 '19 13:08 cicciogeppo88

Hi, Can you provide stackblitz sample using with https://stackblitz.com/github/primefaces/primeng-issue-template .

yigitfindikli avatar Aug 20 '19 13:08 yigitfindikli

Like I said in the initial report, I'm not sure how to provide an example because I only see this during unit testing, where a test completes and the component is destroyed before the Accordion finishes animating.

I think my initial description tells the whole story though. If you just look at the code I linked to above you can clearly see the problem. There's no check in place to make sure the component hasn't been destroyed before it runs detectChanges().

I think onDestroy() just needs to set a flag like this.isDestroyed = true;, and then the animating setter would just become:

set animating(val: boolean) {
    this._animating = val;
    if(!this.isDestroyed) this.changeDetector.detectChanges();
}

brian428 avatar Aug 20 '19 16:08 brian428

@yigitfindikli same comment as @brian428 can't provide an example as well; what I could understand is that a singular test ran with Fit (even if is obsolete) is not throwing errors, but if I run more tests together, some of these are throwing errors.

cicciogeppo88 avatar Aug 21 '19 10:08 cicciogeppo88

I've done a pull request using a solition also proposed by @brian428 (https://github.com/primefaces/primeng/pull/8083).

gferreira-keeps avatar Aug 23 '19 15:08 gferreira-keeps

Hello there, Thank you @gferreira-keeps , do you know when this will be approved/released? I would like to try it to see if is working and fixing this topic's issue,

Thanks

cicciogeppo88 avatar Sep 04 '19 09:09 cicciogeppo88