-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And add unit tests + update docs
- Loading branch information
Alex Malkevich
committed
Jan 9, 2019
1 parent
1749f15
commit 423a896
Showing
4 changed files
with
92 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { EMPTY, Subject } from 'rxjs'; | ||
|
||
import * as untilDestroyedObj from '../src/take-until-destroy'; | ||
|
||
import { WithUntilDestroyed } from '../src/decorator'; | ||
|
||
describe('@WithUntilDestroyed decorator', () => { | ||
it('should throw when applied on non Observable prop', () => { | ||
class Test { | ||
@WithUntilDestroyed() | ||
notObservable = true; | ||
} | ||
|
||
expect(() => new Test()).toThrowError(); | ||
}); | ||
|
||
it('should call `untilDestroyed()` with target prototype and `destroyMethodName` on setter', () => { | ||
const untilDestroyedSpy = spyOn( | ||
untilDestroyedObj, | ||
'untilDestroyed', | ||
).and.callThrough(); | ||
|
||
class Test { | ||
@WithUntilDestroyed('destroy') | ||
notObservable = EMPTY; | ||
|
||
destroy() {} | ||
} | ||
|
||
expect(untilDestroyedSpy).not.toHaveBeenCalled(); | ||
|
||
new Test(); | ||
|
||
expect(untilDestroyedSpy).toHaveBeenCalledWith(Test.prototype, 'destroy'); | ||
}); | ||
|
||
it('should unsubscribe when `destroyMethodName` was called', () => { | ||
const callback = jest.fn(); | ||
|
||
class Test { | ||
subject = new Subject<any>(); | ||
|
||
@WithUntilDestroyed('destroy') | ||
stream$ = this.subject.asObservable(); | ||
|
||
destroy() {} | ||
} | ||
|
||
const test = new Test(); | ||
test.stream$.subscribe(callback); | ||
|
||
test.subject.next('event'); | ||
|
||
expect(callback).toHaveBeenCalledWith('event'); | ||
|
||
callback.mockReset(); | ||
test.destroy(); | ||
test.subject.next('event'); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { untilDestroyed } from './take-until-destroy'; | ||
export { AutoUnsubscribe } from './decorator'; | ||
export { WithUntilDestroyed as AutoUnsubscribe } from './decorator'; |