clickoutsidehandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event */
  6. import clickOutsideHandler from '../../src/bindings/clickoutsidehandler';
  7. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. testUtils.createSinonSandbox();
  10. describe( 'clickOutsideHandler', () => {
  11. let activator, actionSpy, contextElement;
  12. beforeEach( () => {
  13. activator = testUtils.sinon.stub().returns( false );
  14. contextElement = document.createElement( 'div' );
  15. actionSpy = testUtils.sinon.spy();
  16. document.body.appendChild( contextElement );
  17. clickOutsideHandler( {
  18. emitter: Object.create( DomEmitterMixin ),
  19. activator: activator,
  20. contextElement: contextElement,
  21. callback: actionSpy
  22. } );
  23. } );
  24. afterEach( () => {
  25. document.body.removeChild( contextElement );
  26. } );
  27. it( 'should fired callback after clicking out of context element when listener is active', () => {
  28. activator.returns( true );
  29. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  30. sinon.assert.calledOnce( actionSpy );
  31. } );
  32. it( 'should not fired callback after clicking out of context element when listener is not active', () => {
  33. activator.returns( false );
  34. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  35. sinon.assert.notCalled( actionSpy );
  36. } );
  37. it( 'should not fired callback after clicking on context element when listener is active', () => {
  38. activator.returns( true );
  39. contextElement.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  40. sinon.assert.notCalled( actionSpy );
  41. } );
  42. it( 'should not fired callback after clicking on context element when listener is not active', () => {
  43. activator.returns( false );
  44. contextElement.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  45. sinon.assert.notCalled( actionSpy );
  46. } );
  47. it( 'should listen when model initial `ifActive` value was `true`', () => {
  48. const spy = testUtils.sinon.spy();
  49. activator.returns( true );
  50. clickOutsideHandler( {
  51. emitter: Object.create( DomEmitterMixin ),
  52. activator: activator,
  53. contextElement: contextElement,
  54. callback: spy
  55. } );
  56. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. it( 'should not listen when model initial `ifActive` value was `false`', () => {
  60. const spy = testUtils.sinon.spy();
  61. activator.returns( false );
  62. clickOutsideHandler( {
  63. emitter: Object.create( DomEmitterMixin ),
  64. activator: activator,
  65. contextElement: contextElement,
  66. callback: spy
  67. } );
  68. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  69. sinon.assert.notCalled( spy );
  70. } );
  71. it( 'should react on model `ifActive` property change', () => {
  72. activator.returns( true );
  73. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  74. sinon.assert.calledOnce( actionSpy );
  75. activator.returns( false );
  76. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  77. // Still called once, was not called second time.
  78. sinon.assert.calledOnce( actionSpy );
  79. activator.returns( true );
  80. document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  81. // Called one more time.
  82. sinon.assert.calledTwice( actionSpy );
  83. } );
  84. } );