resizeobserver.js 12 KB

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