resizeobserver.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 ResizeObserver from '../../src/dom/resizeobserver';
  11. describe( 'ResizeObserver()', () => {
  12. let elementA, elementB;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. elementA = document.createElement( 'div' );
  16. elementA.id = 'A';
  17. elementB = document.createElement( 'div' );
  18. elementB.id = 'B';
  19. } );
  20. afterEach( () => {
  21. // Make it look like the module was loaded from scratch.
  22. ResizeObserver._observerInstance = null;
  23. ResizeObserver._elementCallbacks = null;
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should use the native implementation if available', () => {
  27. const spy = sinon.spy();
  28. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( () => {
  29. return {
  30. observe: spy,
  31. unobserve: sinon.spy()
  32. };
  33. } );
  34. const observer = new ResizeObserver( elementA, () => {} );
  35. sinon.assert.calledOnce( spy );
  36. observer.destroy();
  37. } );
  38. it( 'should use the polyfill when no native implementation available', () => {
  39. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  40. const observer = new ResizeObserver( elementA, () => {} );
  41. expect( ResizeObserver._observerInstance.constructor.name ).to.equal( 'ResizeObserverPolyfill' );
  42. observer.destroy();
  43. } );
  44. it( 'should re-use the same native observer instance over and over again', () => {
  45. const elementA = document.createElement( 'div' );
  46. const elementB = document.createElement( 'div' );
  47. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( () => {
  48. return {
  49. observe() {},
  50. unobserve() {}
  51. };
  52. } );
  53. const observerA = new ResizeObserver( elementA, () => {} );
  54. const observerB = new ResizeObserver( elementB, () => {} );
  55. sinon.assert.calledOnce( global.window.ResizeObserver );
  56. observerA.destroy();
  57. observerB.destroy();
  58. } );
  59. it( 'should react to resizing of an element', () => {
  60. const callbackA = sinon.spy();
  61. let resizeCallback;
  62. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  63. resizeCallback = callback;
  64. return {
  65. observe() {},
  66. unobserve() {}
  67. };
  68. } );
  69. const observerA = new ResizeObserver( elementA, callbackA );
  70. resizeCallback( [
  71. { target: elementA }
  72. ] );
  73. sinon.assert.calledOnce( callbackA );
  74. sinon.assert.calledWithExactly( callbackA.firstCall, { target: elementA } );
  75. observerA.destroy();
  76. } );
  77. it( 'should be able to observe the same element along with other observers', () => {
  78. const callbackA = sinon.spy();
  79. const callbackB = sinon.spy();
  80. let resizeCallback;
  81. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  82. resizeCallback = callback;
  83. return {
  84. observe() {},
  85. unobserve() {}
  86. };
  87. } );
  88. const observerA = new ResizeObserver( elementA, callbackA );
  89. const observerB = new ResizeObserver( elementA, callbackB );
  90. resizeCallback( [
  91. { target: elementA }
  92. ] );
  93. sinon.assert.calledOnce( callbackA );
  94. sinon.assert.calledWithExactly( callbackA, { target: elementA } );
  95. sinon.assert.calledOnce( callbackB );
  96. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  97. observerA.destroy();
  98. observerB.destroy();
  99. } );
  100. it( 'should not be affected by other observers being destroyed', () => {
  101. const callbackA = sinon.spy();
  102. const callbackB = sinon.spy();
  103. let resizeCallback;
  104. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  105. resizeCallback = callback;
  106. return {
  107. observe() {},
  108. unobserve() {}
  109. };
  110. } );
  111. const observerA = new ResizeObserver( elementA, callbackA );
  112. const observerB = new ResizeObserver( elementA, callbackB );
  113. resizeCallback( [
  114. { target: elementA }
  115. ] );
  116. sinon.assert.calledOnce( callbackA );
  117. sinon.assert.calledWithExactly( callbackA, { target: elementA } );
  118. sinon.assert.calledOnce( callbackB );
  119. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  120. observerB.destroy();
  121. resizeCallback( [
  122. { target: elementA }
  123. ] );
  124. sinon.assert.calledTwice( callbackA );
  125. sinon.assert.calledWithExactly( callbackA.secondCall, { target: elementA } );
  126. sinon.assert.calledOnce( callbackB );
  127. sinon.assert.calledWithExactly( callbackB, { target: elementA } );
  128. observerA.destroy();
  129. } );
  130. } );
  131. describe( 'destroy()', () => {
  132. it( 'should make the observer stop responding to resize of an element', () => {
  133. const callbackA = sinon.spy();
  134. let resizeCallback;
  135. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  136. resizeCallback = callback;
  137. return {
  138. observe() {},
  139. unobserve() {}
  140. };
  141. } );
  142. const observerA = new ResizeObserver( elementA, callbackA );
  143. resizeCallback( [
  144. { target: elementA }
  145. ] );
  146. sinon.assert.calledOnce( callbackA );
  147. observerA.destroy();
  148. resizeCallback( [
  149. { target: elementA }
  150. ] );
  151. sinon.assert.calledOnce( callbackA );
  152. } );
  153. it( 'should not throw if called multiple times', () => {
  154. const callbackA = sinon.spy();
  155. const observerA = new ResizeObserver( elementA, callbackA );
  156. expect( () => {
  157. observerA.destroy();
  158. observerA.destroy();
  159. } ).should.not.throw;
  160. } );
  161. } );
  162. describe( 'ResizeObserverPolyfill', () => {
  163. let callback, elementRectA, elementRectB;
  164. beforeEach( () => {
  165. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( null );
  166. callback = sinon.spy();
  167. elementRectA = {
  168. top: 10,
  169. right: 20,
  170. bottom: 20,
  171. left: 0,
  172. height: 10,
  173. width: 20
  174. };
  175. elementRectB = {
  176. top: 0,
  177. right: 10,
  178. bottom: 10,
  179. left: 0,
  180. height: 10,
  181. width: 10
  182. };
  183. elementA.getBoundingClientRect = () => elementRectA;
  184. elementB.getBoundingClientRect = () => elementRectB;
  185. document.body.appendChild( elementA );
  186. document.body.appendChild( elementB );
  187. } );
  188. afterEach( () => {
  189. elementA.remove();
  190. elementB.remove();
  191. } );
  192. it( 'mixes DomEmitterMixin', () => {
  193. const observer = new ResizeObserver( elementA, () => {} );
  194. expect( testUtils.isMixed( ResizeObserver._observerInstance.constructor, DomEmitterMixin ) ).to.be.true;
  195. observer.destroy();
  196. } );
  197. describe( 'observe()', () => {
  198. it( 'calls the callback immediatelly', () => {
  199. const observer = new ResizeObserver( elementA, callback );
  200. sinon.assert.calledOnce( callback );
  201. const { target, contentRect } = callback.firstCall.args[ 0 ];
  202. expect( target ).to.equal( elementA );
  203. expect( contentRect ).to.be.instanceOf( Rect );
  204. expect( contentRect ).to.deep.equal( elementRectA );
  205. observer.destroy();
  206. } );
  207. it( 'does not execute the callback if element has no parent in DOM', () => {
  208. const warnSpy = testUtils.sinon.spy( console, 'warn' );
  209. elementA.remove();
  210. const observer = new ResizeObserver( elementA, callback );
  211. sinon.assert.notCalled( callback );
  212. sinon.assert.notCalled( warnSpy );
  213. observer.destroy();
  214. } );
  215. it( 'starts periodic check and asynchronously does not execute the callback if the element rect is the same', done => {
  216. const observer = new ResizeObserver( elementA, callback );
  217. setTimeout( () => {
  218. sinon.assert.calledOnce( callback );
  219. observer.destroy();
  220. done();
  221. }, 200 );
  222. } );
  223. it( 'starts periodic check and asynchronously executes the callback if the element rect changed', done => {
  224. const observer = new ResizeObserver( elementA, callback );
  225. sinon.assert.calledOnce( callback );
  226. const newRect = {
  227. top: 30,
  228. right: 10,
  229. bottom: 40,
  230. left: 0,
  231. height: 10,
  232. width: 10
  233. };
  234. elementA.getBoundingClientRect = () => newRect;
  235. setTimeout( () => {
  236. sinon.assert.calledTwice( callback );
  237. const { target, contentRect } = callback.secondCall.args[ 0 ];
  238. expect( target ).to.equal( elementA );
  239. expect( contentRect ).to.deep.equal( newRect );
  240. observer.destroy();
  241. done();
  242. }, 200 );
  243. } );
  244. it( 'starts periodic check and asynchronously executes the callback if multiple element rects changed', done => {
  245. const callbackA = sinon.spy();
  246. const callbackB = sinon.spy();
  247. const observerA = new ResizeObserver( elementA, callbackA );
  248. const observerB = new ResizeObserver( elementB, callbackB );
  249. sinon.assert.calledOnce( callbackA );
  250. sinon.assert.calledOnce( callbackB );
  251. const newRectA = {
  252. top: 30,
  253. right: 10,
  254. bottom: 40,
  255. left: 0,
  256. height: 10,
  257. width: 10
  258. };
  259. const newRectB = {
  260. top: 30,
  261. right: 100,
  262. bottom: 40,
  263. left: 0,
  264. height: 10,
  265. width: 100
  266. };
  267. elementA.getBoundingClientRect = () => newRectA;
  268. elementB.getBoundingClientRect = () => newRectB;
  269. setTimeout( () => {
  270. sinon.assert.calledTwice( callbackA );
  271. sinon.assert.calledTwice( callbackB );
  272. const { target: targetA, contentRect: contentRectA } = callbackA.secondCall.args[ 0 ];
  273. const { target: targetB, contentRect: contentRectB } = callbackB.secondCall.args[ 0 ];
  274. expect( targetA ).to.equal( elementA );
  275. expect( contentRectA ).to.deep.equal( newRectA );
  276. expect( targetB ).to.equal( elementB );
  277. expect( contentRectB ).to.deep.equal( newRectB );
  278. observerA.destroy();
  279. observerB.destroy();
  280. done();
  281. }, 200 );
  282. } );
  283. it( 'starts periodic check and synchronously responds to window resize', () => {
  284. const observer = new ResizeObserver( elementA, callback );
  285. sinon.assert.calledOnce( callback );
  286. const newRectA = {
  287. top: 30,
  288. right: 10,
  289. bottom: 40,
  290. left: 0,
  291. height: 10,
  292. width: 10
  293. };
  294. elementA.getBoundingClientRect = () => newRectA;
  295. global.window.dispatchEvent( new Event( 'resize' ) );
  296. sinon.assert.calledTwice( callback );
  297. const { target: targetA, contentRect: contentRectA } = callback.secondCall.args[ 0 ];
  298. expect( targetA ).to.equal( elementA );
  299. expect( contentRectA ).to.deep.equal( newRectA );
  300. observer.destroy();
  301. } );
  302. } );
  303. describe( 'unobserve()', () => {
  304. it( 'removes the element from the observer so no future changes to the element execute the callback', done => {
  305. const observer = new ResizeObserver( elementA, callback );
  306. sinon.assert.calledOnce( callback );
  307. const newRect = {
  308. top: 30,
  309. right: 10,
  310. bottom: 40,
  311. left: 0,
  312. height: 10,
  313. width: 10
  314. };
  315. observer.destroy();
  316. elementA.getBoundingClientRect = () => newRect;
  317. setTimeout( () => {
  318. sinon.assert.calledOnce( callback );
  319. done();
  320. }, 200 );
  321. } );
  322. it( 'disables the Emitter when no elements left in the observer', () => {
  323. const observer = new ResizeObserver( elementA, callback );
  324. const stopCheckSpy = testUtils.sinon.spy( ResizeObserver._observerInstance, '_stopPeriodicCheck' );
  325. sinon.assert.calledOnce( callback );
  326. observer.destroy();
  327. sinon.assert.calledOnce( stopCheckSpy );
  328. } );
  329. } );
  330. } );
  331. } );