Skip to content

Commit

Permalink
Fix CustomOverlayContainer test failures
Browse files Browse the repository at this point in the history
These tests were utilizing the Document object in ways that were not
perfectly isolated. While they had some cleanup logic, other tests may
manipulate the Document in such a way that make these tests fail. Fix
this by allowing tests to inject a node to search for instead of
defaulting to `document.body`. The `document.body` node is used in the
actual `_createContainer()` method.

PER-9864: CustomOverlayContainer test failure
  • Loading branch information
meisekimiu committed Oct 29, 2024
1 parent 905b866 commit 0a3a12a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
35 changes: 15 additions & 20 deletions src/app/dialog-cdk/custom-overlay-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,26 @@ describe('CustomOverlayContainer', () => {
expect(customOverlayContainer).toBeTruthy();
});

describe('_createContainer', () => {
it('should append the container to pr-app-root if it exists', () => {
const mockAppRoot = document.createElement('pr-app-root');
document.body.appendChild(mockAppRoot);
it('should append the container to pr-app-root if it exists', () => {
const mockBodyNode = document.createElement('body');
const mockAppRoot = document.createElement('pr-app-root');
mockBodyNode.appendChild(mockAppRoot);

customOverlayContainer['_createContainer']();
customOverlayContainer.appendContainer(mockBodyNode);

const container = document.querySelector('.cdk-overlay-container');
const container = mockBodyNode.querySelector('.cdk-overlay-container');

expect(container).toBeTruthy();
expect(mockAppRoot.contains(container!)).toBeTrue();

mockAppRoot.remove();
container?.remove();
});

it('should append the container to document.body if pr-app-root does not exist', () => {
customOverlayContainer['_createContainer']();
expect(container).toBeTruthy();
expect(mockAppRoot.contains(container!)).toBeTrue();
});

const container = document.querySelector('.cdk-overlay-container');
it('should append the container to document.body if pr-app-root does not exist', () => {
const mockBodyNode = document.createElement('body');
customOverlayContainer.appendContainer(mockBodyNode);

expect(container).toBeTruthy();
expect(document.body.contains(container!)).toBeTrue();
const container = mockBodyNode.querySelector('.cdk-overlay-container');

container?.remove();
});
expect(container).toBeTruthy();
expect(mockBodyNode.contains(container!)).toBeTrue();
});
});
14 changes: 7 additions & 7 deletions src/app/dialog-cdk/custom-overlay-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { Injectable } from '@angular/core';

@Injectable()
export class CustomOverlayContainer extends OverlayContainer {
protected _createContainer(): void {
public appendContainer(node: Element): void {
const container = document.createElement('div');
container.classList.add('cdk-overlay-container');

const parent = document.querySelector('pr-app-root') || document.body;
if (parent) {
parent.appendChild(container);
} else {
document.body.appendChild(container);
}
const parent = node.querySelector('pr-app-root') || node;
parent.appendChild(container);
this._containerElement = container;
}

protected _createContainer(): void {
this.appendContainer(document.body);

Check warning on line 17 in src/app/dialog-cdk/custom-overlay-container.ts

View check run for this annotation

Codecov / codecov/patch

src/app/dialog-cdk/custom-overlay-container.ts#L17

Added line #L17 was not covered by tests
}
}

0 comments on commit 0a3a12a

Please sign in to comment.