Skip to content

Commit

Permalink
test: improve and enhance ItemDropdownService and ItemDropdownOverlay…
Browse files Browse the repository at this point in the history
…Builder unit tests
  • Loading branch information
tFaster committed Mar 24, 2020
1 parent b859243 commit 25b6a0b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ItemDropdownOverlayBuilder } from './item-dropdown-overlay-builder';
import { getComposedEventPath, ItemDropdownOverlayBuilder } from './item-dropdown-overlay-builder';
import { OverlayConfig } from '@angular/cdk/overlay';

const overlayServiceStub: any = {
Expand All @@ -12,3 +12,21 @@ describe('ItemOverlayBuilder', () => {
expect(new ItemDropdownOverlayBuilder(overlayServiceStub)).toBeTruthy();
});
});

describe('getComposedEventPath', () => {
it('should get same composed event path as native composedPath() function', () => {
const testBtnElement: HTMLButtonElement = document.createElement('button');
document.body.append(testBtnElement);
const testEvent: MouseEvent = new MouseEvent('click');

testBtnElement.addEventListener('click', (mouseEvent: MouseEvent) => {
const composedPath: EventTarget[] = getComposedEventPath(mouseEvent);
const nativeComposedPath: EventTarget[] = mouseEvent.composedPath();
expect(composedPath.length).toBe(nativeComposedPath.length);
nativeComposedPath.forEach((el: EventTarget, index) => {
expect(composedPath[index]).toBe(el);
});
});
testBtnElement.dispatchEvent(testEvent);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ export class ItemDropdownOverlayBuilder<T, C> {
private _config: ItemOverlayBuilderConfig = DEFAULT_CONFIG;

private _documentClickPath$: Observable<EventTarget[]> = fromEvent<MouseEvent>(document, 'click', {capture: true}).pipe(
map((event: MouseEvent) => {
if (event.composedPath) {
return event.composedPath();
}
return getComposedEventPath(event); // Fallback for IE11
})
map((event: MouseEvent) => event.composedPath ? event.composedPath() : getComposedEventPath(event))
);

constructor(private _overlayService: Overlay) {
Expand Down Expand Up @@ -201,9 +196,9 @@ export interface ItemOverlayBuilderConfig {
dropdownBypassElement?: HTMLElement;
}

export function getComposedEventPath(event) {
let el = event.target;
const path = [];
export function getComposedEventPath(event: Event): EventTarget[] {
let el: Element = event.target as Element;
const path: EventTarget[] = [];
while (el) {
path.push(el);
if (el.tagName === 'HTML') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('ItemOverlayBuilderService', () => {
expect(document.querySelector('.test-overlay-panel')).toBeNull();
});

it('should build overlay and close on document click', () => {
it('should build overlay and close on body click', () => {
const overlayController: ItemDropdownController<any, any> = builder.withConfig({
panelClass: 'test-overlay-panel'
}).buildAndConnect(
Expand All @@ -114,6 +114,21 @@ describe('ItemOverlayBuilderService', () => {
expect(document.querySelector('.test-overlay-panel')).toBeNull();
});

it('should build overlay and not close on body click when body is bypass element', () => {
const overlayController: ItemDropdownController<any, any> = builder.withConfig({
panelClass: 'test-overlay-panel',
dropdownBypassElement: document.body
}).buildAndConnect(
templateTestComponentRef.instance.originElement,
templateTestComponentRef.instance.itemDropdownTemplate
);
overlayController.open();
fixture.detectChanges();
document.querySelector('body').dispatchEvent(new KeyboardEvent('click', {}));
fixture.detectChanges();
expect(document.querySelector('.test-overlay-panel')).not.toBeNull();
});

});
})
;
Expand Down

0 comments on commit 25b6a0b

Please sign in to comment.