diff --git a/projects/cdk/observables/stop-propagation.ts b/projects/cdk/observables/stop-propagation.ts new file mode 100644 index 000000000000..cca329f6e441 --- /dev/null +++ b/projects/cdk/observables/stop-propagation.ts @@ -0,0 +1,8 @@ +import {MonoTypeOperatorFunction} from 'rxjs'; +import {tap} from 'rxjs/operators'; + +export function stopPropagation(): MonoTypeOperatorFunction { + return tap(e => { + e.stopPropagation(); + }); +} diff --git a/projects/cdk/observables/test/prevent-default.spec.ts b/projects/cdk/observables/test/prevent-default.spec.ts new file mode 100644 index 000000000000..f80c7a82e464 --- /dev/null +++ b/projects/cdk/observables/test/prevent-default.spec.ts @@ -0,0 +1,16 @@ +import {fakeAsync} from '@angular/core/testing'; +import {Subject} from 'rxjs'; +import {first} from 'rxjs/operators'; +import {preventDefault} from '../prevent-default'; + +describe('preventDefault operator function', () => { + it('prevents event default behavior', fakeAsync(() => { + const event = new Event('click'); + const subject = new Subject(); + + subject.pipe(preventDefault(), first()).subscribe(); + subject.next(event); + + expect(event.defaultPrevented).toBe(true); + })); +}); diff --git a/projects/cdk/observables/test/stop-propagation.spec.ts b/projects/cdk/observables/test/stop-propagation.spec.ts new file mode 100644 index 000000000000..c9874de0a312 --- /dev/null +++ b/projects/cdk/observables/test/stop-propagation.spec.ts @@ -0,0 +1,16 @@ +import {fakeAsync} from '@angular/core/testing'; +import {Subject} from 'rxjs'; +import {first} from 'rxjs/operators'; +import {stopPropagation} from '../stop-propagation'; + +describe('stopPropagation operator function', () => { + it('stops event propagation', fakeAsync(() => { + const event = new Event('click'); + const subject = new Subject(); + + subject.pipe(stopPropagation(), first()).subscribe(); + subject.next(event); + + expect(event.cancelBubble).toBe(true); + })); +});