Skip to content

Commit

Permalink
feat(observables): stopPropagation add operator
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Oct 13, 2020
1 parent b72d3df commit d930e9d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions projects/cdk/observables/stop-propagation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {MonoTypeOperatorFunction} from 'rxjs';
import {tap} from 'rxjs/operators';

export function stopPropagation<T extends Event>(): MonoTypeOperatorFunction<T> {
return tap(e => {
e.stopPropagation();
});
}
16 changes: 16 additions & 0 deletions projects/cdk/observables/test/prevent-default.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Event>();

subject.pipe(preventDefault(), first()).subscribe();
subject.next(event);

expect(event.defaultPrevented).toBe(true);
}));
});
16 changes: 16 additions & 0 deletions projects/cdk/observables/test/stop-propagation.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Event>();

subject.pipe(stopPropagation(), first()).subscribe();
subject.next(event);

expect(event.cancelBubble).toBe(true);
}));
});

0 comments on commit d930e9d

Please sign in to comment.