Skip to content

Commit

Permalink
Test to new Disposables test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortell68 committed Feb 2, 2024
1 parent 020df9c commit 64c2f60
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions packages/core/src/common/disposable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,28 @@ describe('Disposables', () => {
const collection = new DisposableCollection();
collection.onDispose(onDispose);

// DisposableCollection doesn't provide direct access to its array,
// so we have to defeat TypeScript to properly test pruning.
function collectionSize(): number {
/* eslint-disable @typescript-eslint/no-explicit-any */
return (<any>collection).disposables.length;
}
const disposable1 = Disposable.create(elementDispose);
collection.push(disposable1);
expect(collectionSize()).equal(1);
expect(collection['disposables']).to.have.lengthOf(1);

const disposable2 = Disposable.create(elementDispose);
collection.push(disposable2);
expect(collectionSize()).equal(2);
expect(collection['disposables']).to.have.lengthOf(2);

disposable1.dispose();
expect(collectionSize()).equal(1);
expect(collection['disposables']).to.have.lengthOf(1);
expect(onDispose).to.have.not.been.called();
expect(collection.disposed).is.false;

// Test that calling dispose on an already disposed element doesn't
// alter the collection state
disposable1.dispose();
expect(collectionSize()).equal(1);
expect(collection['disposables']).to.have.lengthOf(1);
expect(onDispose).to.have.not.been.called();
expect(collection.disposed).is.false;

disposable2.dispose();
expect(collectionSize()).equal(0);
expect(collection['disposables']).to.be.empty;
expect(collection.disposed).is.true;
expect(onDispose).to.have.been.called.once;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Object.defineProperty(Disposable, 'NULL', {
* - the collection is auto-pruned when an element it contains is disposed by
* any code that has a reference to it
* - you can register to be notified when all elements in the collection have
* been disposed[1]
* been disposed [1]
* - you can conveniently dispose all elements by calling dispose()
* on the collection
*
Expand Down

0 comments on commit 64c2f60

Please sign in to comment.