足力健前端,vue版本

intersection-observer-test.js 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /**
  2. * Copyright 2016 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
  5. *
  6. * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
  7. *
  8. */
  9. // Sets the timeout to three times the poll interval to ensure all updates
  10. // happen (especially in slower browsers). Native implementations get the
  11. // standard 100ms timeout defined in the spec.
  12. var ASYNC_TIMEOUT = IntersectionObserver.prototype.THROTTLE_TIMEOUT * 3 || 100;
  13. var io;
  14. var noop = function() {};
  15. // References to DOM elements, which are accessible to any test
  16. // and reset prior to each test so state isn't shared.
  17. var rootEl;
  18. var grandParentEl;
  19. var parentEl;
  20. var targetEl1;
  21. var targetEl2;
  22. var targetEl3;
  23. var targetEl4;
  24. describe('IntersectionObserver', function() {
  25. before(function() {
  26. // If the browser running the tests doesn't support MutationObserver,
  27. // fall back to polling.
  28. if (!('MutationObserver' in window)) {
  29. IntersectionObserver.prototype.POLL_INTERVAL =
  30. IntersectionObserver.prototype.THROTTLE_TIMEOUT || 100;
  31. }
  32. });
  33. beforeEach(function() {
  34. addStyles();
  35. addFixtures();
  36. });
  37. afterEach(function() {
  38. if (io && 'disconnect' in io) io.disconnect();
  39. io = null;
  40. removeStyles();
  41. removeFixtures();
  42. });
  43. describe('constructor', function() {
  44. it('throws when callback is not a function', function() {
  45. expect(function() {
  46. io = new IntersectionObserver(null);
  47. }).to.throwException();
  48. });
  49. it('instantiates root correctly', function() {
  50. io = new IntersectionObserver(noop);
  51. expect(io.root).to.be(null);
  52. io = new IntersectionObserver(noop, {root: rootEl});
  53. expect(io.root).to.be(rootEl);
  54. });
  55. it('throws when root is not an Element', function() {
  56. expect(function() {
  57. io = new IntersectionObserver(noop, {root: 'foo'});
  58. }).to.throwException();
  59. });
  60. it('instantiates rootMargin correctly', function() {
  61. io = new IntersectionObserver(noop, {rootMargin: '10px'});
  62. expect(io.rootMargin).to.be('10px 10px 10px 10px');
  63. io = new IntersectionObserver(noop, {rootMargin: '10px -5%'});
  64. expect(io.rootMargin).to.be('10px -5% 10px -5%');
  65. io = new IntersectionObserver(noop, {rootMargin: '10px 20% 0px'});
  66. expect(io.rootMargin).to.be('10px 20% 0px 20%');
  67. io = new IntersectionObserver(noop, {rootMargin: '0px 0px -5% 5px'});
  68. expect(io.rootMargin).to.be('0px 0px -5% 5px');
  69. // TODO(philipwalton): the polyfill supports fractional pixel and
  70. // percentage values, but the native Chrome implementation does not,
  71. // at least not in what it reports `rootMargin` to be.
  72. if (!supportsNativeIntersectionObserver()) {
  73. io = new IntersectionObserver(noop, {rootMargin: '-2.5% -8.5px'});
  74. expect(io.rootMargin).to.be('-2.5% -8.5px -2.5% -8.5px');
  75. }
  76. });
  77. // TODO(philipwalton): this doesn't throw in FF, consider readding once
  78. // expected behavior is clarified.
  79. // it('throws when rootMargin is not in pixels or pecernt', function() {
  80. // expect(function() {
  81. // io = new IntersectionObserver(noop, {rootMargin: '0'});
  82. // }).to.throwException();
  83. // });
  84. // Chrome's implementation in version 51 doesn't include the thresholds
  85. // property, but versions 52+ do.
  86. if ('thresholds' in IntersectionObserver.prototype) {
  87. it('instantiates thresholds correctly', function() {
  88. io = new IntersectionObserver(noop);
  89. expect(io.thresholds).to.eql([0]);
  90. io = new IntersectionObserver(noop, {threshold: 0.5});
  91. expect(io.thresholds).to.eql([0.5]);
  92. io = new IntersectionObserver(noop, {threshold: [0.25, 0.5, 0.75]});
  93. expect(io.thresholds).to.eql([0.25, 0.5, 0.75]);
  94. io = new IntersectionObserver(noop, {threshold: [1, .5, 0]});
  95. expect(io.thresholds).to.eql([0, .5, 1]);
  96. });
  97. }
  98. it('throws when a threshold is not a number', function() {
  99. expect(function() {
  100. io = new IntersectionObserver(noop, {threshold: ['foo']});
  101. }).to.throwException();
  102. });
  103. it('throws when a threshold value is not between 0 and 1', function() {
  104. expect(function() {
  105. io = new IntersectionObserver(noop, {threshold: [0, -1]});
  106. }).to.throwException();
  107. });
  108. });
  109. describe('observe', function() {
  110. it('throws when target is not an Element', function() {
  111. expect(function() {
  112. io = new IntersectionObserver(noop);
  113. io.observe(null);
  114. }).to.throwException();
  115. });
  116. it('triggers for all targets when observing begins', function(done) {
  117. io = new IntersectionObserver(function(records) {
  118. expect(records.length).to.be(2);
  119. expect(records[0].intersectionRatio).to.be(1);
  120. expect(records[1].intersectionRatio).to.be(0);
  121. done();
  122. }, {root: rootEl});
  123. targetEl2.style.top = '-40px';
  124. io.observe(targetEl1);
  125. io.observe(targetEl2);
  126. });
  127. it('triggers for existing targets when observing begins after monitoring has begun', function(done) {
  128. var spy = sinon.spy();
  129. io = new IntersectionObserver(spy, {root: rootEl});
  130. io.observe(targetEl1);
  131. setTimeout(function() {
  132. io.observe(targetEl2);
  133. setTimeout(function() {
  134. expect(spy.callCount).to.be(2);
  135. done();
  136. }, ASYNC_TIMEOUT);
  137. }, ASYNC_TIMEOUT);
  138. });
  139. it('triggers with the correct arguments', function(done) {
  140. io = new IntersectionObserver(function(records, observer) {
  141. expect(records.length).to.be(2);
  142. expect(records[0] instanceof IntersectionObserverEntry).to.be.ok();
  143. expect(records[1] instanceof IntersectionObserverEntry).to.be.ok();
  144. expect(observer).to.be(io);
  145. expect(this).to.be(io);
  146. done();
  147. }, {root: rootEl});
  148. targetEl2.style.top = '-40px';
  149. io.observe(targetEl1);
  150. io.observe(targetEl2);
  151. });
  152. it('handles container elements with non-visible overflow',
  153. function(done) {
  154. var spy = sinon.spy();
  155. io = new IntersectionObserver(spy, {root: rootEl});
  156. runSequence([
  157. function(done) {
  158. io.observe(targetEl1);
  159. setTimeout(function() {
  160. expect(spy.callCount).to.be(1);
  161. var records = sortRecords(spy.lastCall.args[0]);
  162. expect(records.length).to.be(1);
  163. expect(records[0].intersectionRatio).to.be(1);
  164. done();
  165. }, ASYNC_TIMEOUT);
  166. },
  167. function(done) {
  168. targetEl1.style.left = '-40px';
  169. setTimeout(function() {
  170. expect(spy.callCount).to.be(2);
  171. var records = sortRecords(spy.lastCall.args[0]);
  172. expect(records.length).to.be(1);
  173. expect(records[0].intersectionRatio).to.be(0);
  174. done();
  175. }, ASYNC_TIMEOUT);
  176. },
  177. function(done) {
  178. parentEl.style.overflow = 'visible';
  179. setTimeout(function() {
  180. expect(spy.callCount).to.be(3);
  181. var records = sortRecords(spy.lastCall.args[0]);
  182. expect(records.length).to.be(1);
  183. expect(records[0].intersectionRatio).to.be(1);
  184. done();
  185. }, ASYNC_TIMEOUT);
  186. }
  187. ], done);
  188. });
  189. it('observes one target at a single threshold correctly', function(done) {
  190. var spy = sinon.spy();
  191. io = new IntersectionObserver(spy, {root: rootEl, threshold: 0.5});
  192. runSequence([
  193. function(done) {
  194. targetEl1.style.left = '-5px';
  195. io.observe(targetEl1);
  196. setTimeout(function() {
  197. expect(spy.callCount).to.be(1);
  198. var records = sortRecords(spy.lastCall.args[0]);
  199. expect(records.length).to.be(1);
  200. expect(records[0].intersectionRatio).to.be.greaterThan(0.5);
  201. done();
  202. }, ASYNC_TIMEOUT);
  203. },
  204. function(done) {
  205. targetEl1.style.left = '-15px';
  206. setTimeout(function() {
  207. expect(spy.callCount).to.be(2);
  208. var records = sortRecords(spy.lastCall.args[0]);
  209. expect(records.length).to.be(1);
  210. expect(records[0].intersectionRatio).to.be.lessThan(0.5);
  211. done();
  212. }, ASYNC_TIMEOUT);
  213. },
  214. function(done) {
  215. targetEl1.style.left = '-25px';
  216. setTimeout(function() {
  217. expect(spy.callCount).to.be(2);
  218. done();
  219. }, ASYNC_TIMEOUT);
  220. },
  221. function(done) {
  222. targetEl1.style.left = '-10px';
  223. setTimeout(function() {
  224. expect(spy.callCount).to.be(3);
  225. var records = sortRecords(spy.lastCall.args[0]);
  226. expect(records.length).to.be(1);
  227. expect(records[0].intersectionRatio).to.be(0.5);
  228. done();
  229. }, ASYNC_TIMEOUT);
  230. }
  231. ], done);
  232. });
  233. it('observes multiple targets at multiple thresholds correctly',
  234. function(done) {
  235. var spy = sinon.spy();
  236. io = new IntersectionObserver(spy, {
  237. root: rootEl,
  238. threshold: [1, 0.5, 0]
  239. });
  240. runSequence([
  241. function(done) {
  242. targetEl1.style.top = '0px';
  243. targetEl1.style.left = '-15px';
  244. targetEl2.style.top = '-5px';
  245. targetEl2.style.left = '0px';
  246. targetEl3.style.top = '0px';
  247. targetEl3.style.left = '205px';
  248. io.observe(targetEl1);
  249. io.observe(targetEl2);
  250. io.observe(targetEl3);
  251. setTimeout(function() {
  252. expect(spy.callCount).to.be(1);
  253. var records = sortRecords(spy.lastCall.args[0]);
  254. expect(records.length).to.be(3);
  255. expect(records[0].target).to.be(targetEl1);
  256. expect(records[0].intersectionRatio).to.be(0.25);
  257. expect(records[1].target).to.be(targetEl2);
  258. expect(records[1].intersectionRatio).to.be(0.75);
  259. expect(records[2].target).to.be(targetEl3);
  260. expect(records[2].intersectionRatio).to.be(0);
  261. done();
  262. }, ASYNC_TIMEOUT);
  263. },
  264. function(done) {
  265. targetEl1.style.top = '0px';
  266. targetEl1.style.left = '-5px';
  267. targetEl2.style.top = '-15px';
  268. targetEl2.style.left = '0px';
  269. targetEl3.style.top = '0px';
  270. targetEl3.style.left = '195px';
  271. setTimeout(function() {
  272. expect(spy.callCount).to.be(2);
  273. var records = sortRecords(spy.lastCall.args[0]);
  274. expect(records.length).to.be(3);
  275. expect(records[0].target).to.be(targetEl1);
  276. expect(records[0].intersectionRatio).to.be(0.75);
  277. expect(records[1].target).to.be(targetEl2);
  278. expect(records[1].intersectionRatio).to.be(0.25);
  279. expect(records[2].target).to.be(targetEl3);
  280. expect(records[2].intersectionRatio).to.be(0.25);
  281. done();
  282. }, ASYNC_TIMEOUT);
  283. },
  284. function(done) {
  285. targetEl1.style.top = '0px';
  286. targetEl1.style.left = '5px';
  287. targetEl2.style.top = '-25px';
  288. targetEl2.style.left = '0px';
  289. targetEl3.style.top = '0px';
  290. targetEl3.style.left = '185px';
  291. setTimeout(function() {
  292. expect(spy.callCount).to.be(3);
  293. var records = sortRecords(spy.lastCall.args[0]);
  294. expect(records.length).to.be(3);
  295. expect(records[0].target).to.be(targetEl1);
  296. expect(records[0].intersectionRatio).to.be(1);
  297. expect(records[1].target).to.be(targetEl2);
  298. expect(records[1].intersectionRatio).to.be(0);
  299. expect(records[2].target).to.be(targetEl3);
  300. expect(records[2].intersectionRatio).to.be(0.75);
  301. done();
  302. }, ASYNC_TIMEOUT);
  303. },
  304. function(done) {
  305. targetEl1.style.top = '0px';
  306. targetEl1.style.left = '15px';
  307. targetEl2.style.top = '-35px';
  308. targetEl2.style.left = '0px';
  309. targetEl3.style.top = '0px';
  310. targetEl3.style.left = '175px';
  311. setTimeout(function() {
  312. expect(spy.callCount).to.be(4);
  313. var records = sortRecords(spy.lastCall.args[0]);
  314. expect(records.length).to.be(1);
  315. expect(records[0].target).to.be(targetEl3);
  316. expect(records[0].intersectionRatio).to.be(1);
  317. done();
  318. }, ASYNC_TIMEOUT);
  319. }
  320. ], done);
  321. });
  322. it('handles rootMargin properly', function(done) {
  323. parentEl.style.overflow = 'visible';
  324. targetEl1.style.top = '0px';
  325. targetEl1.style.left = '-20px';
  326. targetEl2.style.top = '-20px';
  327. targetEl2.style.left = '0px';
  328. targetEl3.style.top = '0px';
  329. targetEl3.style.left = '200px';
  330. targetEl4.style.top = '180px';
  331. targetEl4.style.left = '180px';
  332. runSequence([
  333. function(done) {
  334. io = new IntersectionObserver(function(records) {
  335. records = sortRecords(records);
  336. expect(records.length).to.be(4);
  337. expect(records[0].target).to.be(targetEl1);
  338. expect(records[0].intersectionRatio).to.be(1);
  339. expect(records[1].target).to.be(targetEl2);
  340. expect(records[1].intersectionRatio).to.be(.5);
  341. expect(records[2].target).to.be(targetEl3);
  342. expect(records[2].intersectionRatio).to.be(.5);
  343. expect(records[3].target).to.be(targetEl4);
  344. expect(records[3].intersectionRatio).to.be(1);
  345. io.disconnect();
  346. done();
  347. }, {root: rootEl, rootMargin: '10px'});
  348. io.observe(targetEl1);
  349. io.observe(targetEl2);
  350. io.observe(targetEl3);
  351. io.observe(targetEl4);
  352. },
  353. function(done) {
  354. io = new IntersectionObserver(function(records) {
  355. records = sortRecords(records);
  356. expect(records.length).to.be(4);
  357. expect(records[0].target).to.be(targetEl1);
  358. expect(records[0].intersectionRatio).to.be(0.5);
  359. expect(records[1].target).to.be(targetEl2);
  360. expect(records[1].intersectionRatio).to.be(0);
  361. expect(records[2].target).to.be(targetEl3);
  362. expect(records[2].intersectionRatio).to.be(0.5);
  363. expect(records[3].target).to.be(targetEl4);
  364. expect(records[3].intersectionRatio).to.be(0.5);
  365. io.disconnect();
  366. done();
  367. }, {root: rootEl, rootMargin: '-10px 10%'});
  368. io.observe(targetEl1);
  369. io.observe(targetEl2);
  370. io.observe(targetEl3);
  371. io.observe(targetEl4);
  372. },
  373. function(done) {
  374. io = new IntersectionObserver(function(records) {
  375. records = sortRecords(records);
  376. expect(records.length).to.be(4);
  377. expect(records[0].target).to.be(targetEl1);
  378. expect(records[0].intersectionRatio).to.be(0.5);
  379. expect(records[1].target).to.be(targetEl2);
  380. expect(records[1].intersectionRatio).to.be(0);
  381. expect(records[2].target).to.be(targetEl3);
  382. expect(records[2].intersectionRatio).to.be(0);
  383. expect(records[3].target).to.be(targetEl4);
  384. expect(records[3].intersectionRatio).to.be(0.5);
  385. io.disconnect();
  386. done();
  387. }, {root: rootEl, rootMargin: '-5% -2.5% 0px'});
  388. io.observe(targetEl1);
  389. io.observe(targetEl2);
  390. io.observe(targetEl3);
  391. io.observe(targetEl4);
  392. },
  393. function(done) {
  394. io = new IntersectionObserver(function(records) {
  395. records = sortRecords(records);
  396. expect(records.length).to.be(4);
  397. expect(records[0].target).to.be(targetEl1);
  398. expect(records[0].intersectionRatio).to.be(0.5);
  399. expect(records[1].target).to.be(targetEl2);
  400. expect(records[1].intersectionRatio).to.be(0.5);
  401. expect(records[2].target).to.be(targetEl3);
  402. expect(records[2].intersectionRatio).to.be(0);
  403. expect(records[3].target).to.be(targetEl4);
  404. expect(records[3].intersectionRatio).to.be(0.25);
  405. io.disconnect();
  406. done();
  407. }, {root: rootEl, rootMargin: '5% -2.5% -10px -190px'});
  408. io.observe(targetEl1);
  409. io.observe(targetEl2);
  410. io.observe(targetEl3);
  411. io.observe(targetEl4);
  412. }
  413. ], done);
  414. });
  415. it('handles targets on the boundary of root', function(done) {
  416. var spy = sinon.spy();
  417. io = new IntersectionObserver(spy, {root: rootEl});
  418. runSequence([
  419. function(done) {
  420. targetEl1.style.top = '0px';
  421. targetEl1.style.left = '-21px';
  422. targetEl2.style.top = '-20px';
  423. targetEl2.style.left = '0px';
  424. io.observe(targetEl1);
  425. io.observe(targetEl2);
  426. setTimeout(function() {
  427. expect(spy.callCount).to.be(1);
  428. var records = sortRecords(spy.lastCall.args[0]);
  429. expect(records.length).to.be(2);
  430. expect(records[0].intersectionRatio).to.be(0);
  431. expect(records[0].target).to.be(targetEl1);
  432. expect(records[0].isIntersecting).to.be(false);
  433. expect(records[1].intersectionRatio).to.be(0);
  434. expect(records[1].target).to.be(targetEl2);
  435. expect(records[1].isIntersecting).to.be(true);
  436. done();
  437. }, ASYNC_TIMEOUT);
  438. },
  439. function(done) {
  440. targetEl1.style.top = '0px';
  441. targetEl1.style.left = '-20px';
  442. targetEl2.style.top = '-21px';
  443. targetEl2.style.left = '0px';
  444. setTimeout(function() {
  445. expect(spy.callCount).to.be(2);
  446. var records = sortRecords(spy.lastCall.args[0]);
  447. expect(records.length).to.be(2);
  448. expect(records[0].intersectionRatio).to.be(0);
  449. expect(records[0].target).to.be(targetEl1);
  450. expect(records[1].intersectionRatio).to.be(0);
  451. expect(records[1].target).to.be(targetEl2);
  452. done();
  453. }, ASYNC_TIMEOUT);
  454. },
  455. function(done) {
  456. targetEl1.style.top = '-20px';
  457. targetEl1.style.left = '200px';
  458. targetEl2.style.top = '200px';
  459. targetEl2.style.left = '200px';
  460. setTimeout(function() {
  461. expect(spy.callCount).to.be(3);
  462. var records = sortRecords(spy.lastCall.args[0]);
  463. expect(records.length).to.be(1);
  464. expect(records[0].intersectionRatio).to.be(0);
  465. expect(records[0].target).to.be(targetEl2);
  466. done();
  467. }, ASYNC_TIMEOUT);
  468. }
  469. ], done);
  470. });
  471. it('handles zero-size targets within the root coordinate space',
  472. function(done) {
  473. io = new IntersectionObserver(function(records) {
  474. expect(records.length).to.be(1);
  475. expect(records[0].isIntersecting).to.be(true);
  476. expect(records[0].intersectionRatio).to.be(1);
  477. done();
  478. }, {root: rootEl});
  479. targetEl1.style.top = '0px';
  480. targetEl1.style.left = '0px';
  481. targetEl1.style.width = '0px';
  482. targetEl1.style.height = '0px';
  483. io.observe(targetEl1);
  484. });
  485. it('handles elements with display set to none', function(done) {
  486. var spy = sinon.spy();
  487. io = new IntersectionObserver(spy, {root: rootEl});
  488. runSequence([
  489. function(done) {
  490. rootEl.style.display = 'none';
  491. io.observe(targetEl1);
  492. setTimeout(function() {
  493. expect(spy.callCount).to.be(1);
  494. var records = sortRecords(spy.lastCall.args[0]);
  495. expect(records.length).to.be(1);
  496. expect(records[0].isIntersecting).to.be(false);
  497. expect(records[0].intersectionRatio).to.be(0);
  498. done();
  499. }, ASYNC_TIMEOUT);
  500. },
  501. function(done) {
  502. rootEl.style.display = 'block';
  503. setTimeout(function() {
  504. expect(spy.callCount).to.be(2);
  505. var records = sortRecords(spy.lastCall.args[0]);
  506. expect(records.length).to.be(1);
  507. expect(records[0].isIntersecting).to.be(true);
  508. expect(records[0].intersectionRatio).to.be(1);
  509. done();
  510. }, ASYNC_TIMEOUT);
  511. },
  512. function(done) {
  513. parentEl.style.display = 'none';
  514. setTimeout(function() {
  515. expect(spy.callCount).to.be(3);
  516. var records = sortRecords(spy.lastCall.args[0]);
  517. expect(records.length).to.be(1);
  518. expect(records[0].isIntersecting).to.be(false);
  519. expect(records[0].intersectionRatio).to.be(0);
  520. done();
  521. }, ASYNC_TIMEOUT);
  522. },
  523. function(done) {
  524. parentEl.style.display = 'block';
  525. setTimeout(function() {
  526. expect(spy.callCount).to.be(4);
  527. var records = sortRecords(spy.lastCall.args[0]);
  528. expect(records.length).to.be(1);
  529. expect(records[0].isIntersecting).to.be(true);
  530. expect(records[0].intersectionRatio).to.be(1);
  531. done();
  532. }, ASYNC_TIMEOUT);
  533. },
  534. function(done) {
  535. targetEl1.style.display = 'none';
  536. setTimeout(function() {
  537. expect(spy.callCount).to.be(5);
  538. var records = sortRecords(spy.lastCall.args[0]);
  539. expect(records.length).to.be(1);
  540. expect(records[0].isIntersecting).to.be(false);
  541. expect(records[0].intersectionRatio).to.be(0);
  542. done();
  543. }, ASYNC_TIMEOUT);
  544. }
  545. ], done);
  546. });
  547. it('handles target elements not yet added to the DOM', function(done) {
  548. var spy = sinon.spy();
  549. io = new IntersectionObserver(spy, {root: rootEl});
  550. // targetEl5 is initially not in the DOM. Note that this element must be
  551. // created outside of the addFixtures() function to catch the IE11 error
  552. // described here: https://github.com/w3c/IntersectionObserver/pull/205
  553. var targetEl5 = document.createElement('div');
  554. targetEl5.setAttribute('id', 'target5');
  555. runSequence([
  556. function(done) {
  557. io.observe(targetEl5);
  558. setTimeout(function() {
  559. // Initial observe should trigger with no intersections since
  560. // targetEl5 is not yet in the DOM.
  561. expect(spy.callCount).to.be(1);
  562. var records = sortRecords(spy.lastCall.args[0]);
  563. expect(records.length).to.be(1);
  564. expect(records[0].isIntersecting).to.be(false);
  565. expect(records[0].intersectionRatio).to.be(0);
  566. expect(records[0].target).to.be(targetEl5);
  567. done();
  568. }, ASYNC_TIMEOUT);
  569. },
  570. function(done) {
  571. // Adding targetEl5 inside rootEl should trigger.
  572. parentEl.insertBefore(targetEl5, targetEl2);
  573. setTimeout(function() {
  574. expect(spy.callCount).to.be(2);
  575. var records = sortRecords(spy.lastCall.args[0]);
  576. expect(records.length).to.be(1);
  577. expect(records[0].intersectionRatio).to.be(1);
  578. expect(records[0].target).to.be(targetEl5);
  579. done();
  580. }, ASYNC_TIMEOUT);
  581. },
  582. function(done) {
  583. // Removing an ancestor of targetEl5 should trigger.
  584. grandParentEl.parentNode.removeChild(grandParentEl);
  585. setTimeout(function() {
  586. expect(spy.callCount).to.be(3);
  587. var records = sortRecords(spy.lastCall.args[0]);
  588. expect(records.length).to.be(1);
  589. expect(records[0].intersectionRatio).to.be(0);
  590. expect(records[0].target).to.be(targetEl5);
  591. done();
  592. }, ASYNC_TIMEOUT);
  593. },
  594. function(done) {
  595. // Adding the previously removed targetEl5 (via grandParentEl)
  596. // back directly inside rootEl should trigger.
  597. rootEl.appendChild(targetEl5);
  598. setTimeout(function() {
  599. expect(spy.callCount).to.be(4);
  600. var records = sortRecords(spy.lastCall.args[0]);
  601. expect(records.length).to.be(1);
  602. expect(records[0].intersectionRatio).to.be(1);
  603. expect(records[0].target).to.be(targetEl5);
  604. done();
  605. }, ASYNC_TIMEOUT);
  606. },
  607. function(done) {
  608. // Removing rootEl should trigger.
  609. rootEl.parentNode.removeChild(rootEl);
  610. setTimeout(function() {
  611. expect(spy.callCount).to.be(5);
  612. var records = sortRecords(spy.lastCall.args[0]);
  613. expect(records.length).to.be(1);
  614. expect(records[0].intersectionRatio).to.be(0);
  615. expect(records[0].target).to.be(targetEl5);
  616. done();
  617. }, ASYNC_TIMEOUT);
  618. }
  619. ], done);
  620. });
  621. if ('attachShadow' in Element.prototype) {
  622. it('handles targets in shadow DOM', function(done) {
  623. grandParentEl.attachShadow({mode: 'open'});
  624. grandParentEl.shadowRoot.appendChild(parentEl);
  625. io = new IntersectionObserver(function(records) {
  626. expect(records.length).to.be(1);
  627. expect(records[0].intersectionRatio).to.be(1);
  628. done();
  629. }, {root: rootEl});
  630. io.observe(targetEl1);
  631. });
  632. it('handles roots in shadow DOM', function(done) {
  633. var shadowRoot = grandParentEl.attachShadow({mode: 'open'});
  634. shadowRoot.innerHTML =
  635. '<style>' +
  636. '#slot-parent {' +
  637. ' position: relative;' +
  638. ' width: 400px;' +
  639. ' height: 200px;' +
  640. '}' +
  641. '</style>' +
  642. '<div id="slot-parent"><slot></slot></div>';
  643. var slotParent = shadowRoot.getElementById('slot-parent');
  644. io = new IntersectionObserver(function(records) {
  645. expect(records.length).to.be(1);
  646. expect(records[0].intersectionRatio).to.be(1);
  647. done();
  648. }, {root: slotParent});
  649. io.observe(targetEl1);
  650. });
  651. }
  652. it('handles sub-root element scrolling', function(done) {
  653. io = new IntersectionObserver(function(records) {
  654. expect(records.length).to.be(1);
  655. expect(records[0].intersectionRatio).to.be(1);
  656. done();
  657. }, {root: rootEl});
  658. io.observe(targetEl3);
  659. setTimeout(function() {
  660. parentEl.scrollLeft = 40;
  661. }, 0);
  662. });
  663. // Only run this test in browsers that support CSS transitions.
  664. if ('transform' in document.documentElement.style &&
  665. 'transition' in document.documentElement.style) {
  666. it('supports CSS transitions and transforms', function(done) {
  667. targetEl1.style.top = '220px';
  668. targetEl1.style.left = '220px';
  669. io = new IntersectionObserver(function(records) {
  670. expect(records.length).to.be(1);
  671. // Chrome's native implementation sometimes incorrectly reports
  672. // the intersection ratio as a number > 1.
  673. expect(records[0].intersectionRatio >= 1);
  674. done();
  675. }, {root: rootEl, threshold: [1]});
  676. // CSS transitions that are slower than the default throttle timeout
  677. // require polling to detect, which can be set on a per-instance basis.
  678. if (!supportsNativeIntersectionObserver()) {
  679. io.POLL_INTERVAL = 100;
  680. }
  681. io.observe(targetEl1);
  682. setTimeout(function() {
  683. targetEl1.style.transform = 'translateX(-40px) translateY(-40px)';
  684. }, 0);
  685. });
  686. }
  687. it('uses the viewport when no root is specified', function(done) {
  688. io = new IntersectionObserver(function(records) {
  689. var viewportWidth =
  690. document.documentElement.clientWidth || document.body.clientWidth;
  691. var viewportHeight =
  692. document.documentElement.clientHeight || document.body.clientHeight;
  693. expect(records.length).to.be(1);
  694. expect(records[0].rootBounds.top).to.be(0);
  695. expect(records[0].rootBounds.left).to.be(0);
  696. expect(records[0].rootBounds.right).to.be(viewportWidth);
  697. expect(records[0].rootBounds.width).to.be(viewportWidth);
  698. expect(records[0].rootBounds.bottom).to.be(viewportHeight);
  699. expect(records[0].rootBounds.height).to.be(viewportHeight);
  700. done();
  701. });
  702. // Ensures targetEl1 is visible in the viewport before observing.
  703. window.scrollTo(0, 0);
  704. rootEl.style.position = 'absolute';
  705. rootEl.style.top = '0px';
  706. rootEl.style.left = '0px';
  707. io.observe(targetEl1);
  708. });
  709. });
  710. describe('takeRecords', function() {
  711. it('supports getting records before the callback is invoked',
  712. function(done) {
  713. var lastestRecords = [];
  714. io = new IntersectionObserver(function(records) {
  715. lastestRecords = lastestRecords.concat(records);
  716. }, {root: rootEl});
  717. io.observe(targetEl1);
  718. window.requestAnimationFrame && requestAnimationFrame(function() {
  719. lastestRecords = lastestRecords.concat(io.takeRecords());
  720. });
  721. setTimeout(function() {
  722. expect(lastestRecords.length).to.be(1);
  723. expect(lastestRecords[0].intersectionRatio).to.be(1);
  724. done();
  725. }, ASYNC_TIMEOUT);
  726. });
  727. });
  728. describe('unobserve', function() {
  729. it('removes targets from the internal store', function(done) {
  730. var spy = sinon.spy();
  731. io = new IntersectionObserver(spy, {root: rootEl});
  732. runSequence([
  733. function(done) {
  734. targetEl1.style.top = targetEl2.style.top = '0px';
  735. targetEl1.style.left = targetEl2.style.left = '0px';
  736. io.observe(targetEl1);
  737. io.observe(targetEl2);
  738. setTimeout(function() {
  739. expect(spy.callCount).to.be(1);
  740. var records = sortRecords(spy.lastCall.args[0]);
  741. expect(records.length).to.be(2);
  742. expect(records[0].target).to.be(targetEl1);
  743. expect(records[0].intersectionRatio).to.be(1);
  744. expect(records[1].target).to.be(targetEl2);
  745. expect(records[1].intersectionRatio).to.be(1);
  746. done();
  747. }, ASYNC_TIMEOUT);
  748. },
  749. function(done) {
  750. io.unobserve(targetEl1);
  751. targetEl1.style.top = targetEl2.style.top = '0px';
  752. targetEl1.style.left = targetEl2.style.left = '-40px';
  753. setTimeout(function() {
  754. expect(spy.callCount).to.be(2);
  755. var records = sortRecords(spy.lastCall.args[0]);
  756. expect(records.length).to.be(1);
  757. expect(records[0].target).to.be(targetEl2);
  758. expect(records[0].intersectionRatio).to.be(0);
  759. done();
  760. }, ASYNC_TIMEOUT);
  761. },
  762. function(done) {
  763. io.unobserve(targetEl2);
  764. targetEl1.style.top = targetEl2.style.top = '0px';
  765. targetEl1.style.left = targetEl2.style.left = '0px';
  766. setTimeout(function() {
  767. expect(spy.callCount).to.be(2);
  768. done();
  769. }, ASYNC_TIMEOUT);
  770. }
  771. ], done);
  772. });
  773. });
  774. describe('disconnect', function() {
  775. it('removes all targets and stops listening for changes', function(done) {
  776. var spy = sinon.spy();
  777. io = new IntersectionObserver(spy, {root: rootEl});
  778. runSequence([
  779. function(done) {
  780. targetEl1.style.top = targetEl2.style.top = '0px';
  781. targetEl1.style.left = targetEl2.style.left = '0px';
  782. io.observe(targetEl1);
  783. io.observe(targetEl2);
  784. setTimeout(function() {
  785. expect(spy.callCount).to.be(1);
  786. var records = sortRecords(spy.lastCall.args[0]);
  787. expect(records.length).to.be(2);
  788. expect(records[0].target).to.be(targetEl1);
  789. expect(records[0].intersectionRatio).to.be(1);
  790. expect(records[1].target).to.be(targetEl2);
  791. expect(records[1].intersectionRatio).to.be(1);
  792. done();
  793. }, ASYNC_TIMEOUT);
  794. },
  795. function(done) {
  796. io.disconnect();
  797. targetEl1.style.top = targetEl2.style.top = '0px';
  798. targetEl1.style.left = targetEl2.style.left = '-40px';
  799. setTimeout(function() {
  800. expect(spy.callCount).to.be(1);
  801. done();
  802. }, ASYNC_TIMEOUT);
  803. }
  804. ], done);
  805. });
  806. });
  807. });
  808. /**
  809. * Runs a sequence of function and when finished invokes the done callback.
  810. * Each function in the sequence is invoked with its own done function and
  811. * it should call that function once it's complete.
  812. * @param {Array<Function>} functions An array of async functions.
  813. * @param {Function} done A final callback to be invoked once all function
  814. * have run.
  815. */
  816. function runSequence(functions, done) {
  817. var next = functions.shift();
  818. if (next) {
  819. next(function() {
  820. runSequence(functions, done);
  821. });
  822. } else {
  823. done && done();
  824. }
  825. }
  826. /**
  827. * Returns whether or not the current browser has native support for
  828. * IntersectionObserver.
  829. * @return {boolean} True if native support is detected.
  830. */
  831. function supportsNativeIntersectionObserver() {
  832. return 'IntersectionObserver' in window &&
  833. window.IntersectionObserver.toString().indexOf('[native code]') > -1;
  834. }
  835. /**
  836. * Sorts an array of records alphebetically by ascending ID. Since the current
  837. * native implementation doesn't sort change entries by `observe` order, we do
  838. * that ourselves for the non-polyfill case. Since all tests call observe
  839. * on targets in sequential order, this should always match.
  840. * https://crbug.com/613679
  841. * @param {Array<IntersectionObserverEntry>} entries The entries to sort.
  842. * @return {Array<IntersectionObserverEntry>} The sorted array.
  843. */
  844. function sortRecords(entries) {
  845. if (supportsNativeIntersectionObserver()) {
  846. entries = entries.sort(function(a, b) {
  847. return a.target.id < b.target.id ? -1 : 1;
  848. });
  849. }
  850. return entries;
  851. }
  852. /**
  853. * Adds the common styles used by all tests to the page.
  854. */
  855. function addStyles() {
  856. var styles = document.createElement('style');
  857. styles.id = 'styles';
  858. document.documentElement.appendChild(styles);
  859. var cssText =
  860. '#root {' +
  861. ' position: relative;' +
  862. ' width: 400px;' +
  863. ' height: 200px;' +
  864. ' background: #eee' +
  865. '}' +
  866. '#grand-parent {' +
  867. ' position: relative;' +
  868. ' width: 200px;' +
  869. ' height: 200px;' +
  870. '}' +
  871. '#parent {' +
  872. ' position: absolute;' +
  873. ' top: 0px;' +
  874. ' left: 200px;' +
  875. ' overflow: hidden;' +
  876. ' width: 200px;' +
  877. ' height: 200px;' +
  878. ' background: #ddd;' +
  879. '}' +
  880. '#target1, #target2, #target3, #target4, #target5 {' +
  881. ' position: absolute;' +
  882. ' top: 0px;' +
  883. ' left: 0px;' +
  884. ' width: 20px;' +
  885. ' height: 20px;' +
  886. ' transform: translateX(0px) translateY(0px);' +
  887. ' transition: transform .5s;' +
  888. ' background: #f00;' +
  889. '}';
  890. // IE8 doesn't allow setting innerHTML on a <style> element.
  891. if (styles.styleSheet) {
  892. styles.styleSheet.cssText = cssText;
  893. }
  894. else {
  895. styles.innerHTML = cssText;
  896. }
  897. }
  898. /**
  899. * Adds the DOM fixtures used by all tests to the page and assigns them to
  900. * global variables so they can be referenced within the tests.
  901. */
  902. function addFixtures() {
  903. var fixtures = document.createElement('div');
  904. fixtures.id = 'fixtures';
  905. fixtures.innerHTML =
  906. '<div id="root">' +
  907. ' <div id="grand-parent">' +
  908. ' <div id="parent">' +
  909. ' <div id="target1"></div>' +
  910. ' <div id="target2"></div>' +
  911. ' <div id="target3"></div>' +
  912. ' <div id="target4"></div>' +
  913. ' </div>' +
  914. ' </div>' +
  915. '</div>';
  916. document.body.appendChild(fixtures);
  917. rootEl = document.getElementById('root');
  918. grandParentEl = document.getElementById('grand-parent');
  919. parentEl = document.getElementById('parent');
  920. targetEl1 = document.getElementById('target1');
  921. targetEl2 = document.getElementById('target2');
  922. targetEl3 = document.getElementById('target3');
  923. targetEl4 = document.getElementById('target4');
  924. }
  925. /**
  926. * Removes the common styles from the page.
  927. */
  928. function removeStyles() {
  929. var styles = document.getElementById('styles');
  930. styles.parentNode.removeChild(styles);
  931. }
  932. /**
  933. * Removes the DOM fixtures from the page and resets the global references.
  934. */
  935. function removeFixtures() {
  936. var fixtures = document.getElementById('fixtures');
  937. fixtures.parentNode.removeChild(fixtures);
  938. rootEl = null;
  939. grandParentEl = null;
  940. parentEl = null;
  941. targetEl1 = null;
  942. targetEl2 = null;
  943. targetEl3 = null;
  944. targetEl4 = null;
  945. }