test(click-block): fix click block test

This commit is contained in:
Adam Bradley
2016-12-07 11:31:25 -06:00
parent e210448e63
commit df52a410d3
2 changed files with 16 additions and 20 deletions

View File

@ -1,7 +1,8 @@
import { App } from '../app';
import { AppPortal } from '../app-root';
import { ClickBlock } from '../../../util/click-block';
import { Config } from '../../../config/config';
import { mockApp, mockConfig, mockNavController, mockPlatform, mockTab, mockTabs, mockView, mockViews } from '../../../util/mock-providers';
import { mockApp, mockConfig, mockElementRef, mockNavController, mockPlatform, mockRenderer, mockTab, mockTabs, mockView, mockViews } from '../../../util/mock-providers';
import { OverlayPortal } from '../../nav/overlay-portal';
import { Platform } from '../../../platform/platform';
@ -354,29 +355,22 @@ describe('App', () => {
});
describe('setEnabled', () => {
it('should disable click block when app is enabled', (done) => {
// arrange
let mockClickBlock: any = {
activate: () => {}
};
app._clickBlock = new ClickBlock(app, mockConfig(), mockElementRef(), mockRenderer());
spyOn(mockClickBlock, 'activate');
spyOn(app._clickBlock, '_activate');
app._clickBlock = mockClickBlock;
// act
app.setEnabled(true);
// assert
expect(mockClickBlock.activate).not.toHaveBeenCalled();
expect(app._clickBlock._activate).not.toHaveBeenCalledWith();
setTimeout(() => {
expect(mockClickBlock.activate).toHaveBeenCalledWith(false, 0);
expect(app._clickBlock._activate).toHaveBeenCalledWith(false);
done();
}, 120);
}, 1000);
it('should enable click block when false is passed with duration', () => {
// arrange
let mockClickBlock: any = {

View File

@ -12,7 +12,7 @@ import { Config } from '../config/config';
selector: '.click-block'
})
export class ClickBlock {
private _tmrId: number;
private _tmr: number;
private _showing: boolean = false;
isEnabled: boolean;
@ -23,30 +23,32 @@ export class ClickBlock {
private renderer: Renderer
) {
app._clickBlock = this;
let enabled = this.isEnabled = config.getBoolean('clickBlock', true);
const enabled = this.isEnabled = config.getBoolean('clickBlock', true);
if (enabled) {
this.setElementClass('click-block-enabled', true);
this._setElementClass('click-block-enabled', true);
}
}
activate(shouldShow: boolean, expire: number = 100) {
if (this.isEnabled) {
clearNativeTimeout(this._tmrId);
clearNativeTimeout(this._tmr);
if (shouldShow) {
this._activate(true);
}
this._tmrId = nativeTimeout(this._activate.bind(this, false), expire);
this._tmr = nativeTimeout(this._activate.bind(this, false), expire);
}
}
/** @internal */
_activate(shouldShow: boolean) {
if (this._showing !== shouldShow) {
this.setElementClass('click-block-active', shouldShow);
this._setElementClass('click-block-active', shouldShow);
this._showing = shouldShow;
}
}
setElementClass(className: string, add: boolean) {
private _setElementClass(className: string, add: boolean) {
this.renderer.setElementClass(this.elementRef.nativeElement, className, add);
}