Testing setup for components in modals in weird behaviour of change detection in these setups #8713
Replies: 1 comment
-
@antonborisoff Greetings, For Your First Question:You're correct that creating For Your Second Question:I believe the first usage you're referring to involves creating the modal using the For the second scenario, where you're creating the modal using the service, change detection won't occur until the modal is opened for the first time. Also, the modal component must be visible at least once for change detection to be active, which might have already occurred in previous tests. To better illustrate this, I've created a StackBlitz example: (link) I hope I could be of help. |
Beta Was this translation helpful? Give feedback.
-
ngZorro uses a particular setup to test that components for modals are correctly opened. Here is an example: https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/components/modal/modal-footer.directive.spec.ts
If you check 'should work with service' test and setup for it, you'll notice that ngZorro devs call createComponent() with completely unrelated component in beforeEach() (TestDirectiveFooterComponent, but it could be any dummy component with any template) to get a fixture, but then open the component under testing with modalService.create() in tests.
Suprisingly enough, if you call fixture.detectChanges() in such setup, you will trigger change detection for the component in the modal as well (it could be checked if you add ngDoCheck() logging to the component under testing).
Now consider a different (IMHO, more natural) setup. Have a dummy component that provides a button and a click on this button opens the modal:
If you now use HostComponent to get fixture and click its button to open the modal, the strange thing happens: fixture.detectChanges() no longer triggers change detection on component in the modal.
So I have the following questions:
I would greatly appreciate any help with these questions either from the community or ngZorr dev team.
Beta Was this translation helpful? Give feedback.
All reactions