Skip to content

Commit

Permalink
feat(cdk): mustBePresent add new operator (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea authored Dec 8, 2020
1 parent dacf719 commit 0f12ac5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion projects/cdk/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const base = require('../../karma.conf');
const base = require('../../karma.config');

module.exports = function (config) {
base(config);
Expand Down
1 change: 1 addition & 0 deletions projects/cdk/observables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './focus-visible-observable';
export * from './is-alive';
export * from './items-query-list-observable';
export * from './mouse-drag-finish-from';
export * from './must-be-present';
export * from './pressed-observable';
export * from './prevent-default';
export * from './replay-control-value-changes';
Expand Down
13 changes: 13 additions & 0 deletions projects/cdk/observables/must-be-present.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {isPresent} from '@taiga-ui/cdk/utils/miscellaneous';
import {OperatorFunction} from 'rxjs';
import {map} from 'rxjs/operators';

export function mustBePresent<T>(): OperatorFunction<T | undefined | null, T> {
return map(value => {
if (!isPresent(value)) {
throw new Error('Value must present');
}

return value;
});
}
72 changes: 72 additions & 0 deletions projects/cdk/observables/test/must-be-present.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {fakeAsync, tick} from '@angular/core/testing';
import {mustBePresent} from '@taiga-ui/cdk/observables/must-be-present';
import {Subject} from 'rxjs';
import {first} from 'rxjs/operators';

describe('mustBePresent operator function', () => {
it('not throws on NaN', fakeAsync(() => {
const stream = new Subject<number | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next(NaN);
tick();
}).not.toThrow();
}));

it('not throws on 0', fakeAsync(() => {
const stream = new Subject<number | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next(0);
tick();
}).not.toThrow();
}));

it('not throws on false', fakeAsync(() => {
const stream = new Subject<boolean | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next(false);
tick();
}).not.toThrow();
}));

it('not throws on empty string', fakeAsync(() => {
const stream = new Subject<string | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next('');
tick();
}).not.toThrow();
}));

it('throws on undefined', fakeAsync(() => {
const stream = new Subject<undefined | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next(undefined);
tick();
}).toThrow();
}));

it('throws on null', fakeAsync(() => {
const stream = new Subject<undefined | null>();

stream.pipe(first(), mustBePresent()).subscribe();

expect(() => {
stream.next(null);
tick();
}).toThrow();
}));
});
2 changes: 1 addition & 1 deletion projects/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@angular/forms": ">=6.0.0",
"@angular/platform-browser": ">=6.0.0",
"@angular/router": ">=6.0.0",
"@tinkoff/ng-event-filters": ">=1.0.0",
"@tinkoff/ng-event-plugins": ">=1.0.1",
"@tinkoff/ng-polymorpheus": ">=2.0.0",
"rxjs": ">=6.0.0"
}
Expand Down

0 comments on commit 0f12ac5

Please sign in to comment.