Hi,
whenever I have a test that involves an IgxDialogComponent or the IgxOverlayService it produces a persistent igx-overlay dom node in the karma test runner's html page. One for every test. It's not until I manually remove these overlays that I can read the results.
I've tried the following:
to no avail. As a matter of fact, by debug I see the detachAll method finds no overlayInfo to run on. The array is empty.
What's that I'm missing? Could you please supply any workaround to this issue?
Kind regards
Hello Cesar,
Thank you for reaching out.
Based on your description, it seems that persistent igx-overlay elements in the DOM are causing issues for you. To address this, I can suggest using a function to clear all opened overlays and reset the scroll position, which can be called before each test to ensure a clean test environment.
igx-overlay
Here is a sample utility method you can implement:
export class TestUtils { // Clears all opened overlays and resets document scrollTop and scrollLeft public static clearAllOverlays() { const overlays = document.getElementsByClassName('igx-overlay') as HTMLCollectionOf<Element>; Array.from(overlays).forEach(element => { element.remove(); }); document.documentElement.scrollTop = 0; document.documentElement.scrollLeft = 0; document.body.style.overflow = 'hidden'; } }
You can use it before each test and after all tests to ensure clean state.
beforeEach(waitForAsync(() => { TestUtils.clearAllOverlays(); })); afterAll(() => { TestUtils.clearAllOverlays(); });
Also If you have injected IgxOverlayService into your component (e.g., constructor(@Inject(IgxOverlayService) public overlay: IgxOverlayService) { }), you can access it directly in tests to ensure all overlays are detached:
IgxOverlayService
constructor(@Inject(IgxOverlayService) public overlay: IgxOverlayService) { }
const overlay = fixture.componentInstance.overlay; overlay.detachAll();
This combination of clearAllOverlays and overlay.detachAll() should effectively remove lingering overlays.
clearAllOverlays
overlay.detachAll()
If these methods do not resolve the issue, could you please provide additional details or examples of the problem, such as:
I’ll be happy to investigate further with this information.
Best Regards,
Arkan Ahmedov
Infragistics
Thank you Arkan for your fast and useful reply. Clearing on afterAll proved good enough.
Best regards
Hello César,
I am glad that you find my suggestion helpful.
Thank you for using Infragistics components.
Regards,