chore: add tests

This commit is contained in:
mathuo 2022-03-10 20:58:23 +00:00
parent fe8845631d
commit 943ea3b93e
No known key found for this signature in database
GPG Key ID: C6EEDEFD6CA07281
3 changed files with 27 additions and 17 deletions

View File

@ -1,4 +1,4 @@
import { MutableDisposable } from '../lifecycle';
import { CompositeDisposable, MutableDisposable } from '../lifecycle';
describe('lifecycle', () => {
test('mutable disposable', () => {
@ -23,4 +23,29 @@ describe('lifecycle', () => {
mutableDisposable.dispose();
});
test('composite disposable', () => {
const d1 = {
dispose: jest.fn(),
};
const d2 = {
dispose: jest.fn(),
};
const d3 = {
dispose: jest.fn(),
};
const d4 = {
dispose: jest.fn(),
};
const cut = new CompositeDisposable(d1, d2);
cut.addDisposables(d3, d4);
cut.dispose();
expect(d1.dispose).toHaveBeenCalledTimes(1);
expect(d2.dispose).toHaveBeenCalledTimes(1);
expect(d3.dispose).toHaveBeenCalledTimes(1);
expect(d4.dispose).toHaveBeenCalledTimes(1);
});
});

View File

@ -1,15 +0,0 @@
export function tryParseJSON(
text: string,
reviver?: (this: any, key: string, value: any) => any
): any | undefined;
export function tryParseJSON<T>(
text: string,
reviver?: (this: any, key: string, value: any) => any
): T | undefined {
try {
return JSON.parse(text, reviver) as T;
} catch (err) {
console.warn('failed to parse JSON');
return undefined;
}
}

View File

@ -27,7 +27,7 @@ export class CompositeDisposable {
}
public addDisposables(...args: IDisposable[]) {
args?.forEach((arg) => this.disposables.push(arg));
args.forEach((arg) => this.disposables.push(arg));
}
public dispose() {