-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk):
mustBePresent
add new operator (#53)
- Loading branch information
Showing
5 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters