Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the dispose method is called for the first time, overwrite the method with one that logs a warning. This is to prevent the code inside the dispose method from executing twice, even if the method is overloaded in a parent class
- Loading branch information
Floris Bernard
committed
Aug 10, 2018
1 parent
6559124
commit 7ae950c
Showing
2 changed files
with
37 additions
and
16 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 |
---|---|---|
@@ -1,20 +1,35 @@ | ||
import Disposable from '../src/lib/Disposable'; | ||
import { expect } from 'chai'; | ||
import {} from 'mocha'; | ||
import { expect, use as chaiUse } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
chaiUse(sinonChai); | ||
|
||
describe('DisposableSpec', () => { | ||
let disposable:Disposable; | ||
it('isDisposed should return false when instance has not been disposed', () => { | ||
const disposable = new Disposable(); | ||
expect(disposable.isDisposed()).equals(false); | ||
}); | ||
|
||
it('isDisposed should return true when instance has been disposed', () => { | ||
const disposable = new Disposable(); | ||
disposable.dispose(); | ||
expect(disposable.isDisposed()).equals(true); | ||
}); | ||
|
||
beforeEach(() => { | ||
disposable = new Disposable(); | ||
}); | ||
it('dispose() should not be executed multiple times', () => { | ||
const disposeSpy = spy(); | ||
|
||
it('isDisposed should return false when instance has not been disposed', () => { | ||
expect(disposable.isDisposed()).equals(false); | ||
}); | ||
class TestDispose extends Disposable { | ||
public dispose():void { | ||
disposeSpy(); | ||
super.dispose(); | ||
} | ||
} | ||
|
||
it('isDisposed should return true when instance has been disposed', () => { | ||
disposable.dispose(); | ||
expect(disposable.isDisposed()).equals(true); | ||
}); | ||
const disposable = new TestDispose(); | ||
disposable.dispose(); | ||
disposable.dispose(); | ||
expect(disposeSpy).to.have.been.calledOnce; | ||
}); | ||
}); |