getresizeobserver.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, console */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Rect from '../../src/dom/rect';
  8. import global from '../../src/dom/global';
  9. import DomEmitterMixin from '../../src/dom/emittermixin';
  10. import getResizeObserver from '../../src/dom/getresizeobserver';
  11. describe( '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( 'does not execute the callback if element has no parent in DOM', () => {
  70. const warnSpy = testUtils.sinon.spy( console, 'warn' );
  71. elementA.remove();
  72. observer.observe( elementA );
  73. sinon.assert.notCalled( callback );
  74. sinon.assert.notCalled( warnSpy );
  75. } );
  76. it( 'starts periodic check and asynchronously does not execute the callback if the element rect is the same', done => {
  77. observer.observe( elementA );
  78. setTimeout( () => {
  79. sinon.assert.calledOnce( callback );
  80. done();
  81. }, 200 );
  82. } );
  83. it( 'starts periodic check and asynchronously executes the callback if the element rect changed', done => {
  84. observer.observe( elementA );
  85. sinon.assert.calledOnce( callback );
  86. const newRect = {
  87. top: 30,
  88. right: 10,
  89. bottom: 40,
  90. left: 0,
  91. height: 10,
  92. width: 10
  93. };
  94. elementA.getBoundingClientRect = () => newRect;
  95. setTimeout( () => {
  96. sinon.assert.calledTwice( callback );
  97. const { target, contentRect } = callback.secondCall.args[ 0 ][ 0 ];
  98. expect( target ).to.equal( elementA );
  99. expect( contentRect ).to.deep.equal( newRect );
  100. done();
  101. }, 200 );
  102. } );
  103. it( 'starts periodic check and asynchronously executes the callback if multiple element rects changed', done => {
  104. observer.observe( elementA );
  105. observer.observe( elementB );
  106. sinon.assert.calledOnce( callback );
  107. const newRectA = {
  108. top: 30,
  109. right: 10,
  110. bottom: 40,
  111. left: 0,
  112. height: 10,
  113. width: 10
  114. };
  115. const newRectB = {
  116. top: 30,
  117. right: 100,
  118. bottom: 40,
  119. left: 0,
  120. height: 10,
  121. width: 100
  122. };
  123. elementA.getBoundingClientRect = () => newRectA;
  124. elementB.getBoundingClientRect = () => newRectB;
  125. setTimeout( () => {
  126. sinon.assert.calledTwice( callback );
  127. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ][ 0 ];
  128. const { target: targetB, contentRect: contentRectB } = callback.secondCall.args[ 0 ][ 1 ];
  129. expect( targetA ).to.equal( elementA );
  130. expect( contentRectA ).to.deep.equal( newRectA );
  131. expect( targetB ).to.equal( elementB );
  132. expect( contentRectB ).to.deep.equal( newRectB );
  133. done();
  134. }, 200 );
  135. } );
  136. it( 'starts periodic check and synchronously responds to window resize', () => {
  137. observer.observe( elementA );
  138. sinon.assert.calledOnce( callback );
  139. const newRectA = {
  140. top: 30,
  141. right: 10,
  142. bottom: 40,
  143. left: 0,
  144. height: 10,
  145. width: 10
  146. };
  147. elementA.getBoundingClientRect = () => newRectA;
  148. global.window.dispatchEvent( new Event( 'resize' ) );
  149. sinon.assert.calledTwice( callback );
  150. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ][ 0 ];
  151. expect( targetA ).to.equal( elementA );
  152. expect( contentRectA ).to.deep.equal( newRectA );
  153. } );
  154. } );
  155. describe( 'unobserve()', () => {
  156. it( 'removes the element from the observer so no future changes to the element execute the callback', done => {
  157. observer.observe( elementA );
  158. sinon.assert.calledOnce( callback );
  159. const newRect = {
  160. top: 30,
  161. right: 10,
  162. bottom: 40,
  163. left: 0,
  164. height: 10,
  165. width: 10
  166. };
  167. observer.unobserve( elementA );
  168. elementA.getBoundingClientRect = () => newRect;
  169. setTimeout( () => {
  170. sinon.assert.calledOnce( callback );
  171. done();
  172. }, 200 );
  173. } );
  174. it( 'does not affect the callback being executed for other elements in the observer', done => {
  175. observer.observe( elementA );
  176. observer.observe( elementB );
  177. sinon.assert.calledOnce( callback );
  178. const newRectA = {
  179. top: 30,
  180. right: 10,
  181. bottom: 40,
  182. left: 0,
  183. height: 10,
  184. width: 10
  185. };
  186. const newRectB = {
  187. top: 30,
  188. right: 100,
  189. bottom: 40,
  190. left: 0,
  191. height: 10,
  192. width: 100
  193. };
  194. elementA.getBoundingClientRect = () => newRectA;
  195. elementB.getBoundingClientRect = () => newRectB;
  196. observer.unobserve( elementA );
  197. setTimeout( () => {
  198. sinon.assert.calledTwice( callback );
  199. const { target: targetB, contentRect: contentRectB } = callback.secondCall.args[ 0 ][ 0 ];
  200. expect( callback.secondCall.args[ 0 ] ).to.have.length( 1 );
  201. expect( targetB ).to.equal( elementB );
  202. expect( contentRectB ).to.deep.equal( newRectB );
  203. done();
  204. }, 200 );
  205. } );
  206. it( 'disables the Emitter when no elements left in the observer', () => {
  207. const stopCheckSpy = testUtils.sinon.spy( observer, '_stopPeriodicCheck' );
  208. observer.observe( elementA );
  209. sinon.assert.calledOnce( callback );
  210. observer.unobserve( elementA );
  211. sinon.assert.calledOnce( stopCheckSpy );
  212. } );
  213. } );
  214. describe( 'disconnect()', () => {
  215. it( 'calls unobserve() for all observed elements', () => {
  216. observer.observe( elementA );
  217. observer.observe( elementB );
  218. const unobserveSpy = testUtils.sinon.spy( observer, 'unobserve' );
  219. observer.disconnect();
  220. sinon.assert.calledTwice( unobserveSpy );
  221. sinon.assert.calledWith( unobserveSpy.firstCall, elementA );
  222. sinon.assert.calledWith( unobserveSpy.secondCall, elementB );
  223. } );
  224. } );
  225. } );
  226. } );