8
0

resizeobserver.js 11 KB

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