Skip to content

Commit

Permalink
Update peer dependencies (#5)
Browse files Browse the repository at this point in the history
* update peerDeps

* add more tests

* more tests

* up version
  • Loading branch information
un33k committed Jan 27, 2019
1 parent 4d5110b commit 8291570
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## 1.0.1

Enhancements:

- Update peer dependencies
- Add more tests

## 1.0.0

Release:
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/app/lazy/lazy.component.ts
Expand Up @@ -26,7 +26,11 @@ export class LazyComponent implements OnDestroy {
.pipe(takeUntil(this.unsub.destroy$))
.subscribe(num => console.log(`LazyComponent - takeUntil - ${num}`));

this.unsub.autoUnsubscribe([this.customSub$]);
this.customSub2$ = interval(2000).subscribe(num =>
console.log(`LazyComponent - customSub2$ - ${num}`)
);

this.unsub.autoUnsubscribe([this.customSub$, this.customSub2$]);
}

ngOnDestroy() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@nwx/unsub",
"version": "1.0.0",
"version": "1.0.1",
"repository": {
"type": "git",
"url": "git+https://github.com/neekware/nwx-unsub.git"
Expand Down
6 changes: 3 additions & 3 deletions pkgs/unsub/package.json
@@ -1,8 +1,8 @@
{
"name": "@nwx/unsub",
"description": "A simple subscription clean up module for Angular applications",
"description": "A simple subscription clean up library for Angular applications",
"peerDependencies": {
"@angular/core": "^6.0.0",
"rxjs": "^6.0.0"
"@angular/core": ">=5.0.0 <8.0.0",
"rxjs": ">=6.0.0 || ^5.6.0-forward-compat.4"
}
}
8 changes: 4 additions & 4 deletions pkgs/unsub/src/unsub.service.ts
Expand Up @@ -22,11 +22,11 @@ export class UnsubService implements OnDestroy {
/**
* Registers all subscriptions that need auto cancellation on destroy
*/
autoUnsubscribe(subscriptions: Subscription | Subscription[]) {
if (Array.isArray(subscriptions)) {
this.subscriptions = [...this.subscriptions, ...subscriptions];
autoUnsubscribe(subscription: Subscription | Subscription[]) {
if (Array.isArray(subscription)) {
this.subscriptions = [...this.subscriptions, ...subscription];
} else {
this.subscriptions.push(subscriptions);
this.subscriptions.push(subscription);
}
}

Expand Down
18 changes: 16 additions & 2 deletions pkgs/unsub/tst/unsub.decorator.spec.ts
Expand Up @@ -6,9 +6,9 @@
* found in the LICENSE file at http://neekware.com/license/MIT.html
*/

import { Unsubscribable } from '../src/unsub.decorators';
import { Subject } from 'rxjs';
import { OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { Unsubscribable } from '../src/unsub.decorators';

const mockSub1 = {
unsubscribe: () => {
Expand Down Expand Up @@ -43,6 +43,20 @@ describe('@Unsubscribable', () => {
expect(destroySpy).toHaveBeenCalled();
});

it('should call ngOnDestroy of super if exists', () => {
@Unsubscribable()
class UnsubComponent implements OnDestroy {
ngOnDestroy() {
console.log('ngOnDestroy of super called');
}
}

const comp = new UnsubComponent();
const logSpy = spyOn(console, 'log');
comp['ngOnDestroy']();
expect(logSpy).toHaveBeenCalled();
});

it('should throw on missing destroy$', () => {
@Unsubscribable({
takeUntilInputName: 'destroy$'
Expand Down
43 changes: 35 additions & 8 deletions pkgs/unsub/tst/unsub.service.spec.ts
Expand Up @@ -18,6 +18,12 @@ const mockSub1 = {
}
};

const mockSub2 = {
unsubscribe: () => {
console.log('Sub2 cancelled');
}
};

describe('UnsubService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -38,22 +44,31 @@ describe('UnsubService', () => {
expect(service.destroy$).toBeDefined();
expect(service.subscriptions).toBeDefined();
expect(service.autoUnsubscribe).toBeDefined();
expect(service.autoUnsubscribe).toBeDefined();
expect(service.ngOnDestroy).toBeDefined();
})
);

it(
'should have functional autoUnsubscribe',
'autoUnsubscribe() should accept subscriptions objects',
inject([UnsubService], (service: UnsubService) => {
const sub$ = mockSub1;
service.autoUnsubscribe(<Subscription>sub$);
const sub1$ = mockSub1 as Subscription;
service.autoUnsubscribe(sub1$);
expect(service.subscriptions.length).toBe(1);
})
);

it(
'should complete destroy$ onDestroy',
'autoUnsubscribe() should accept a list of subscriptions',
inject([UnsubService], (service: UnsubService) => {
const sub1$ = mockSub1 as Subscription;
const sub2$ = mockSub2 as Subscription;
service.autoUnsubscribe([sub1$, sub2$]);
expect(service.subscriptions.length).toBe(2);
})
);

it(
'ngOnDestroy() should complete destroy$',
inject([UnsubService], (service: UnsubService) => {
const completeSpy = spyOn(service.destroy$, 'complete');
service.ngOnDestroy();
Expand All @@ -62,14 +77,26 @@ describe('UnsubService', () => {
);

it(
'should have cancelled sub$ onDestroy',
'ngOnDestroy() should have cancelled subscriptions',
inject([UnsubService], (service: UnsubService) => {
const sub$ = mockSub1;
service.autoUnsubscribe(<Subscription>sub$);
const sub1$ = mockSub1 as Subscription;
service.autoUnsubscribe(sub1$);
const sub2$ = mockSub1 as Subscription;
service.autoUnsubscribe(sub2$);
const logSpy = spyOn(console, 'log');
service.ngOnDestroy();
expect(logSpy).toHaveBeenCalled();
})
);

it(
'ngOnDestroy() should handle invalid subscriptions',
inject([UnsubService], (service: UnsubService) => {
const sub1$ = {} as Subscription;
service.autoUnsubscribe([sub1$]);
const logSpy = spyOn(console, 'log');
service.ngOnDestroy();
expect(logSpy).not.toHaveBeenCalled();
})
);
});

0 comments on commit 8291570

Please sign in to comment.