getresizeobserver.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, setTimeout, Event */
  6. import getResizeObserver from '../../src/dom/getresizeobserver';
  7. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  11. describe.only( 'getResizeObserver()', () => {
  12. testUtils.createSinonSandbox();
  13. it( 'returns the native implementation if available', () => {
  14. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( function( callback ) {
  15. this.callback = callback;
  16. } );
  17. expect( getResizeObserver( 'foo' ).callback ).to.equal( 'foo' );
  18. } );
  19. it( 'returns the polyfill when no native implementation available', () => {
  20. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  21. expect( getResizeObserver().constructor.name ).to.equal( 'ResizeObserverPolyfill' );
  22. } );
  23. describe( 'ResizeObserverPolyfill', () => {
  24. let elementA, elementB, observer, callback, elementRectA, elementRectB;
  25. beforeEach( () => {
  26. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  27. callback = sinon.spy();
  28. observer = getResizeObserver( callback );
  29. elementA = document.createElement( 'div' );
  30. elementB = document.createElement( 'div' );
  31. elementRectA = {
  32. top: 10,
  33. right: 20,
  34. bottom: 20,
  35. left: 0,
  36. height: 10,
  37. width: 20
  38. };
  39. elementRectB = {
  40. top: 0,
  41. right: 10,
  42. bottom: 10,
  43. left: 0,
  44. height: 10,
  45. width: 10
  46. };
  47. elementA.getBoundingClientRect = () => elementRectA;
  48. elementB.getBoundingClientRect = () => elementRectB;
  49. document.body.appendChild( elementA );
  50. document.body.appendChild( elementB );
  51. } );
  52. afterEach( () => {
  53. observer.disconnect();
  54. elementA.remove();
  55. elementB.remove();
  56. } );
  57. it( 'mixes DomEmitterMixin', () => {
  58. expect( testUtils.isMixed( getResizeObserver().constructor, DomEmitterMixin ) ).to.be.true;
  59. } );
  60. describe( 'observe()', () => {
  61. it( 'calls the callback immediatelly', () => {
  62. observer.observe( elementA );
  63. sinon.assert.calledOnce( callback );
  64. const { target, contentRect } = callback.firstCall.args[ 0 ][ 0 ];
  65. expect( target ).to.equal( elementA );
  66. expect( contentRect ).to.be.instanceOf( Rect );
  67. expect( contentRect ).to.deep.equal( elementRectA );
  68. } );
  69. it( 'starts periodic check and asynchronously does not execute the callback if the element rect is the same', done => {
  70. observer.observe( elementA );
  71. setTimeout( () => {
  72. sinon.assert.calledOnce( callback );
  73. done();
  74. }, 200 );
  75. } );
  76. it( 'starts periodic check and asynchronously executes the callback if the element rect changed', done => {
  77. observer.observe( elementA );
  78. sinon.assert.calledOnce( callback );
  79. const newRect = {
  80. top: 30,
  81. right: 10,
  82. bottom: 40,
  83. left: 0,
  84. height: 10,
  85. width: 10
  86. };
  87. elementA.getBoundingClientRect = () => newRect;
  88. setTimeout( () => {
  89. sinon.assert.calledTwice( callback );
  90. const { target, contentRect } = callback.secondCall.args[ 0 ][ 0 ];
  91. expect( target ).to.equal( elementA );
  92. expect( contentRect ).to.deep.equal( newRect );
  93. done();
  94. }, 200 );
  95. } );
  96. it( 'starts periodic check and asynchronously executes the callback if multiple element rects changed', done => {
  97. observer.observe( elementA );
  98. observer.observe( elementB );
  99. sinon.assert.calledOnce( callback );
  100. const newRectA = {
  101. top: 30,
  102. right: 10,
  103. bottom: 40,
  104. left: 0,
  105. height: 10,
  106. width: 10
  107. };
  108. const newRectB = {
  109. top: 30,
  110. right: 100,
  111. bottom: 40,
  112. left: 0,
  113. height: 10,
  114. width: 100
  115. };
  116. elementA.getBoundingClientRect = () => newRectA;
  117. elementB.getBoundingClientRect = () => newRectB;
  118. setTimeout( () => {
  119. sinon.assert.calledTwice( callback );
  120. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ][ 0 ];
  121. const { target: targetB, contentRect: contentRectB } = callback.secondCall.args[ 0 ][ 1 ];
  122. expect( targetA ).to.equal( elementA );
  123. expect( contentRectA ).to.deep.equal( newRectA );
  124. expect( targetB ).to.equal( elementB );
  125. expect( contentRectB ).to.deep.equal( newRectB );
  126. done();
  127. }, 200 );
  128. } );
  129. it( 'starts periodic check and synchronously responds to window resize', () => {
  130. observer.observe( elementA );
  131. sinon.assert.calledOnce( callback );
  132. const newRectA = {
  133. top: 30,
  134. right: 10,
  135. bottom: 40,
  136. left: 0,
  137. height: 10,
  138. width: 10
  139. };
  140. elementA.getBoundingClientRect = () => newRectA;
  141. global.window.dispatchEvent( new Event( 'resize' ) );
  142. sinon.assert.calledTwice( callback );
  143. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ][ 0 ];
  144. expect( targetA ).to.equal( elementA );
  145. expect( contentRectA ).to.deep.equal( newRectA );
  146. } );
  147. } );
  148. describe( 'unobserve()', () => {
  149. it( 'removes the element from the observer so no future changes to the element execute the callback', done => {
  150. observer.observe( elementA );
  151. sinon.assert.calledOnce( callback );
  152. const newRect = {
  153. top: 30,
  154. right: 10,
  155. bottom: 40,
  156. left: 0,
  157. height: 10,
  158. width: 10
  159. };
  160. observer.unobserve( elementA );
  161. elementA.getBoundingClientRect = () => newRect;
  162. setTimeout( () => {
  163. sinon.assert.calledOnce( callback );
  164. done();
  165. }, 200 );
  166. } );
  167. it( 'does not affect the callback being executed for other elements in the observer', done => {
  168. observer.observe( elementA );
  169. observer.observe( elementB );
  170. sinon.assert.calledOnce( callback );
  171. const newRectA = {
  172. top: 30,
  173. right: 10,
  174. bottom: 40,
  175. left: 0,
  176. height: 10,
  177. width: 10
  178. };
  179. const newRectB = {
  180. top: 30,
  181. right: 100,
  182. bottom: 40,
  183. left: 0,
  184. height: 10,
  185. width: 100
  186. };
  187. elementA.getBoundingClientRect = () => newRectA;
  188. elementB.getBoundingClientRect = () => newRectB;
  189. observer.unobserve( elementA );
  190. setTimeout( () => {
  191. sinon.assert.calledTwice( callback );
  192. const { target: targetB, contentRect: contentRectB } = callback.secondCall.args[ 0 ][ 0 ];
  193. expect( callback.secondCall.args[ 0 ] ).to.have.length( 1 );
  194. expect( targetB ).to.equal( elementB );
  195. expect( contentRectB ).to.deep.equal( newRectB );
  196. done();
  197. }, 200 );
  198. } );
  199. it( 'disables the Emitter when no elements left in the observer', () => {
  200. const stopCheckSpy = testUtils.sinon.spy( observer, '_stopPeriodicCheck' );
  201. observer.observe( elementA );
  202. sinon.assert.calledOnce( callback );
  203. observer.unobserve( elementA );
  204. sinon.assert.calledOnce( stopCheckSpy );
  205. } );
  206. } );
  207. describe( 'disconnect()', () => {
  208. it( 'calls unobserve() for all observed elements', () => {
  209. observer.observe( elementA );
  210. observer.observe( elementB );
  211. const unobserveSpy = testUtils.sinon.spy( observer, 'unobserve' );
  212. observer.disconnect();
  213. sinon.assert.calledTwice( unobserveSpy );
  214. sinon.assert.calledWith( unobserveSpy.firstCall, elementA );
  215. sinon.assert.calledWith( unobserveSpy.secondCall, elementB );
  216. } );
  217. } );
  218. } );
  219. } );