ui_utils_spec.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _ui_utils = require("../../web/ui_utils.js");
  24. var _is_node = require("../../shared/is_node.js");
  25. describe("ui_utils", function () {
  26. describe("binary search", function () {
  27. function isTrue(boolean) {
  28. return boolean;
  29. }
  30. function isGreater3(number) {
  31. return number > 3;
  32. }
  33. it("empty array", function () {
  34. expect((0, _ui_utils.binarySearchFirstItem)([], isTrue)).toEqual(0);
  35. });
  36. it("single boolean entry", function () {
  37. expect((0, _ui_utils.binarySearchFirstItem)([false], isTrue)).toEqual(1);
  38. expect((0, _ui_utils.binarySearchFirstItem)([true], isTrue)).toEqual(0);
  39. });
  40. it("three boolean entries", function () {
  41. expect((0, _ui_utils.binarySearchFirstItem)([true, true, true], isTrue)).toEqual(0);
  42. expect((0, _ui_utils.binarySearchFirstItem)([false, true, true], isTrue)).toEqual(1);
  43. expect((0, _ui_utils.binarySearchFirstItem)([false, false, true], isTrue)).toEqual(2);
  44. expect((0, _ui_utils.binarySearchFirstItem)([false, false, false], isTrue)).toEqual(3);
  45. });
  46. it("three numeric entries", function () {
  47. expect((0, _ui_utils.binarySearchFirstItem)([0, 1, 2], isGreater3)).toEqual(3);
  48. expect((0, _ui_utils.binarySearchFirstItem)([2, 3, 4], isGreater3)).toEqual(2);
  49. expect((0, _ui_utils.binarySearchFirstItem)([4, 5, 6], isGreater3)).toEqual(0);
  50. });
  51. });
  52. describe("EventBus", function () {
  53. it("dispatch event", function () {
  54. const eventBus = new _ui_utils.EventBus();
  55. let count = 0;
  56. eventBus.on("test", function (evt) {
  57. expect(evt).toEqual(undefined);
  58. count++;
  59. });
  60. eventBus.dispatch("test");
  61. expect(count).toEqual(1);
  62. });
  63. it("dispatch event with arguments", function () {
  64. const eventBus = new _ui_utils.EventBus();
  65. let count = 0;
  66. eventBus.on("test", function (evt) {
  67. expect(evt).toEqual({
  68. abc: 123
  69. });
  70. count++;
  71. });
  72. eventBus.dispatch("test", {
  73. abc: 123
  74. });
  75. expect(count).toEqual(1);
  76. });
  77. it("dispatch different event", function () {
  78. const eventBus = new _ui_utils.EventBus();
  79. let count = 0;
  80. eventBus.on("test", function () {
  81. count++;
  82. });
  83. eventBus.dispatch("nottest");
  84. expect(count).toEqual(0);
  85. });
  86. it("dispatch event multiple times", function () {
  87. const eventBus = new _ui_utils.EventBus();
  88. let count = 0;
  89. eventBus.dispatch("test");
  90. eventBus.on("test", function () {
  91. count++;
  92. });
  93. eventBus.dispatch("test");
  94. eventBus.dispatch("test");
  95. expect(count).toEqual(2);
  96. });
  97. it("dispatch event to multiple handlers", function () {
  98. const eventBus = new _ui_utils.EventBus();
  99. let count = 0;
  100. eventBus.on("test", function () {
  101. count++;
  102. });
  103. eventBus.on("test", function () {
  104. count++;
  105. });
  106. eventBus.dispatch("test");
  107. expect(count).toEqual(2);
  108. });
  109. it("dispatch to detached", function () {
  110. const eventBus = new _ui_utils.EventBus();
  111. let count = 0;
  112. const listener = function () {
  113. count++;
  114. };
  115. eventBus.on("test", listener);
  116. eventBus.dispatch("test");
  117. eventBus.off("test", listener);
  118. eventBus.dispatch("test");
  119. expect(count).toEqual(1);
  120. });
  121. it("dispatch to wrong detached", function () {
  122. const eventBus = new _ui_utils.EventBus();
  123. let count = 0;
  124. eventBus.on("test", function () {
  125. count++;
  126. });
  127. eventBus.dispatch("test");
  128. eventBus.off("test", function () {
  129. count++;
  130. });
  131. eventBus.dispatch("test");
  132. expect(count).toEqual(2);
  133. });
  134. it("dispatch to detached during handling", function () {
  135. const eventBus = new _ui_utils.EventBus();
  136. let count = 0;
  137. const listener1 = function () {
  138. eventBus.off("test", listener2);
  139. count++;
  140. };
  141. const listener2 = function () {
  142. eventBus.off("test", listener1);
  143. count++;
  144. };
  145. eventBus.on("test", listener1);
  146. eventBus.on("test", listener2);
  147. eventBus.dispatch("test");
  148. eventBus.dispatch("test");
  149. expect(count).toEqual(2);
  150. });
  151. it("dispatch event to handlers with/without 'once' option", function () {
  152. const eventBus = new _ui_utils.EventBus();
  153. let multipleCount = 0,
  154. onceCount = 0;
  155. eventBus.on("test", function () {
  156. multipleCount++;
  157. });
  158. eventBus.on("test", function () {
  159. onceCount++;
  160. }, {
  161. once: true
  162. });
  163. eventBus.dispatch("test");
  164. eventBus.dispatch("test");
  165. eventBus.dispatch("test");
  166. expect(multipleCount).toEqual(3);
  167. expect(onceCount).toEqual(1);
  168. });
  169. it("should not re-dispatch to DOM", async function () {
  170. if (_is_node.isNodeJS) {
  171. pending("Document is not supported in Node.js.");
  172. }
  173. const eventBus = new _ui_utils.EventBus();
  174. let count = 0;
  175. eventBus.on("test", function (evt) {
  176. expect(evt).toEqual(undefined);
  177. count++;
  178. });
  179. function domEventListener() {
  180. expect(false).toEqual(true);
  181. }
  182. document.addEventListener("test", domEventListener);
  183. eventBus.dispatch("test");
  184. await Promise.resolve();
  185. expect(count).toEqual(1);
  186. document.removeEventListener("test", domEventListener);
  187. });
  188. });
  189. describe("isValidRotation", function () {
  190. it("should reject non-integer angles", function () {
  191. expect((0, _ui_utils.isValidRotation)()).toEqual(false);
  192. expect((0, _ui_utils.isValidRotation)(null)).toEqual(false);
  193. expect((0, _ui_utils.isValidRotation)(NaN)).toEqual(false);
  194. expect((0, _ui_utils.isValidRotation)([90])).toEqual(false);
  195. expect((0, _ui_utils.isValidRotation)("90")).toEqual(false);
  196. expect((0, _ui_utils.isValidRotation)(90.5)).toEqual(false);
  197. });
  198. it("should reject non-multiple of 90 degree angles", function () {
  199. expect((0, _ui_utils.isValidRotation)(45)).toEqual(false);
  200. expect((0, _ui_utils.isValidRotation)(-123)).toEqual(false);
  201. });
  202. it("should accept valid angles", function () {
  203. expect((0, _ui_utils.isValidRotation)(0)).toEqual(true);
  204. expect((0, _ui_utils.isValidRotation)(90)).toEqual(true);
  205. expect((0, _ui_utils.isValidRotation)(-270)).toEqual(true);
  206. expect((0, _ui_utils.isValidRotation)(540)).toEqual(true);
  207. });
  208. });
  209. describe("isPortraitOrientation", function () {
  210. it("should be portrait orientation", function () {
  211. expect((0, _ui_utils.isPortraitOrientation)({
  212. width: 200,
  213. height: 400
  214. })).toEqual(true);
  215. expect((0, _ui_utils.isPortraitOrientation)({
  216. width: 500,
  217. height: 500
  218. })).toEqual(true);
  219. });
  220. it("should be landscape orientation", function () {
  221. expect((0, _ui_utils.isPortraitOrientation)({
  222. width: 600,
  223. height: 300
  224. })).toEqual(false);
  225. });
  226. });
  227. describe("parseQueryString", function () {
  228. it("should parse one key/value pair", function () {
  229. const parameters = (0, _ui_utils.parseQueryString)("key1=value1");
  230. expect(parameters.size).toEqual(1);
  231. expect(parameters.get("key1")).toEqual("value1");
  232. });
  233. it("should parse multiple key/value pairs", function () {
  234. const parameters = (0, _ui_utils.parseQueryString)("key1=value1&key2=value2&key3=value3");
  235. expect(parameters.size).toEqual(3);
  236. expect(parameters.get("key1")).toEqual("value1");
  237. expect(parameters.get("key2")).toEqual("value2");
  238. expect(parameters.get("key3")).toEqual("value3");
  239. });
  240. it("should parse keys without values", function () {
  241. const parameters = (0, _ui_utils.parseQueryString)("key1");
  242. expect(parameters.size).toEqual(1);
  243. expect(parameters.get("key1")).toEqual("");
  244. });
  245. it("should decode encoded key/value pairs", function () {
  246. const parameters = (0, _ui_utils.parseQueryString)("k%C3%ABy1=valu%C3%AB1");
  247. expect(parameters.size).toEqual(1);
  248. expect(parameters.get("këy1")).toEqual("valuë1");
  249. });
  250. it("should convert keys to lowercase", function () {
  251. const parameters = (0, _ui_utils.parseQueryString)("Key1=Value1&KEY2=Value2");
  252. expect(parameters.size).toEqual(2);
  253. expect(parameters.get("key1")).toEqual("Value1");
  254. expect(parameters.get("key2")).toEqual("Value2");
  255. });
  256. });
  257. describe("waitOnEventOrTimeout", function () {
  258. let eventBus;
  259. beforeAll(function () {
  260. eventBus = new _ui_utils.EventBus();
  261. });
  262. afterAll(function () {
  263. eventBus = null;
  264. });
  265. it("should reject invalid parameters", async function () {
  266. const invalidTarget = (0, _ui_utils.waitOnEventOrTimeout)({
  267. target: "window",
  268. name: "DOMContentLoaded"
  269. }).then(function () {
  270. expect(false).toEqual(true);
  271. }, function (reason) {
  272. expect(reason instanceof Error).toEqual(true);
  273. });
  274. const invalidName = (0, _ui_utils.waitOnEventOrTimeout)({
  275. target: eventBus,
  276. name: ""
  277. }).then(function () {
  278. expect(false).toEqual(true);
  279. }, function (reason) {
  280. expect(reason instanceof Error).toEqual(true);
  281. });
  282. const invalidDelay = (0, _ui_utils.waitOnEventOrTimeout)({
  283. target: eventBus,
  284. name: "pagerendered",
  285. delay: -1000
  286. }).then(function () {
  287. expect(false).toEqual(true);
  288. }, function (reason) {
  289. expect(reason instanceof Error).toEqual(true);
  290. });
  291. await Promise.all([invalidTarget, invalidName, invalidDelay]);
  292. });
  293. it("should resolve on event, using the DOM", async function () {
  294. if (_is_node.isNodeJS) {
  295. pending("Document is not supported in Node.js.");
  296. }
  297. const button = document.createElement("button");
  298. const buttonClicked = (0, _ui_utils.waitOnEventOrTimeout)({
  299. target: button,
  300. name: "click",
  301. delay: 10000
  302. });
  303. button.click();
  304. const type = await buttonClicked;
  305. expect(type).toEqual(_ui_utils.WaitOnType.EVENT);
  306. });
  307. it("should resolve on timeout, using the DOM", async function () {
  308. if (_is_node.isNodeJS) {
  309. pending("Document is not supported in Node.js.");
  310. }
  311. const button = document.createElement("button");
  312. const buttonClicked = (0, _ui_utils.waitOnEventOrTimeout)({
  313. target: button,
  314. name: "click",
  315. delay: 10
  316. });
  317. const type = await buttonClicked;
  318. expect(type).toEqual(_ui_utils.WaitOnType.TIMEOUT);
  319. });
  320. it("should resolve on event, using the EventBus", async function () {
  321. const pageRendered = (0, _ui_utils.waitOnEventOrTimeout)({
  322. target: eventBus,
  323. name: "pagerendered",
  324. delay: 10000
  325. });
  326. eventBus.dispatch("pagerendered");
  327. const type = await pageRendered;
  328. expect(type).toEqual(_ui_utils.WaitOnType.EVENT);
  329. });
  330. it("should resolve on timeout, using the EventBus", async function () {
  331. const pageRendered = (0, _ui_utils.waitOnEventOrTimeout)({
  332. target: eventBus,
  333. name: "pagerendered",
  334. delay: 10
  335. });
  336. const type = await pageRendered;
  337. expect(type).toEqual(_ui_utils.WaitOnType.TIMEOUT);
  338. });
  339. });
  340. describe("getPageSizeInches", function () {
  341. it("gets page size (in inches)", function () {
  342. const page = {
  343. view: [0, 0, 595.28, 841.89],
  344. userUnit: 1.0,
  345. rotate: 0
  346. };
  347. const {
  348. width,
  349. height
  350. } = (0, _ui_utils.getPageSizeInches)(page);
  351. expect(+width.toPrecision(3)).toEqual(8.27);
  352. expect(+height.toPrecision(4)).toEqual(11.69);
  353. });
  354. it("gets page size (in inches), for non-default /Rotate entry", function () {
  355. const pdfPage1 = {
  356. view: [0, 0, 612, 792],
  357. userUnit: 1,
  358. rotate: 0
  359. };
  360. const {
  361. width: width1,
  362. height: height1
  363. } = (0, _ui_utils.getPageSizeInches)(pdfPage1);
  364. expect(width1).toEqual(8.5);
  365. expect(height1).toEqual(11);
  366. const pdfPage2 = {
  367. view: [0, 0, 612, 792],
  368. userUnit: 1,
  369. rotate: 90
  370. };
  371. const {
  372. width: width2,
  373. height: height2
  374. } = (0, _ui_utils.getPageSizeInches)(pdfPage2);
  375. expect(width2).toEqual(11);
  376. expect(height2).toEqual(8.5);
  377. });
  378. });
  379. describe("getVisibleElements", function () {
  380. const BORDER_WIDTH = 9;
  381. const SPACING = 2 * BORDER_WIDTH - 7;
  382. function makePages(lines) {
  383. const result = [];
  384. let lineTop = 0,
  385. id = 0;
  386. for (const line of lines) {
  387. const lineHeight = line.reduce(function (maxHeight, pair) {
  388. return Math.max(maxHeight, pair[1]);
  389. }, 0);
  390. let offsetLeft = -BORDER_WIDTH;
  391. for (const [clientWidth, clientHeight] of line) {
  392. const offsetTop = lineTop + (lineHeight - clientHeight) / 2 - BORDER_WIDTH;
  393. const div = {
  394. offsetLeft,
  395. offsetTop,
  396. clientWidth,
  397. clientHeight,
  398. clientLeft: BORDER_WIDTH,
  399. clientTop: BORDER_WIDTH
  400. };
  401. result.push({
  402. id,
  403. div
  404. });
  405. ++id;
  406. offsetLeft += clientWidth + SPACING;
  407. }
  408. lineTop += lineHeight + SPACING;
  409. }
  410. return result;
  411. }
  412. function slowGetVisibleElements(scroll, pages) {
  413. const views = [];
  414. const {
  415. scrollLeft,
  416. scrollTop
  417. } = scroll;
  418. const scrollRight = scrollLeft + scroll.clientWidth;
  419. const scrollBottom = scrollTop + scroll.clientHeight;
  420. for (const view of pages) {
  421. const {
  422. div
  423. } = view;
  424. const viewLeft = div.offsetLeft + div.clientLeft;
  425. const viewRight = viewLeft + div.clientWidth;
  426. const viewTop = div.offsetTop + div.clientTop;
  427. const viewBottom = viewTop + div.clientHeight;
  428. if (viewLeft < scrollRight && viewRight > scrollLeft && viewTop < scrollBottom && viewBottom > scrollTop) {
  429. const hiddenHeight = Math.max(0, scrollTop - viewTop) + Math.max(0, viewBottom - scrollBottom);
  430. const hiddenWidth = Math.max(0, scrollLeft - viewLeft) + Math.max(0, viewRight - scrollRight);
  431. const fractionHeight = (div.clientHeight - hiddenHeight) / div.clientHeight;
  432. const fractionWidth = (div.clientWidth - hiddenWidth) / div.clientWidth;
  433. const percent = fractionHeight * fractionWidth * 100 | 0;
  434. views.push({
  435. id: view.id,
  436. x: viewLeft,
  437. y: viewTop,
  438. view,
  439. percent,
  440. widthPercent: fractionWidth * 100 | 0
  441. });
  442. }
  443. }
  444. return {
  445. first: views[0],
  446. last: views[views.length - 1],
  447. views
  448. };
  449. }
  450. function scrollOverDocument(pages, horizontal = false, rtl = false) {
  451. const size = pages.reduce(function (max, {
  452. div
  453. }) {
  454. return Math.max(max, horizontal ? Math.abs(div.offsetLeft + div.clientLeft + div.clientWidth) : div.offsetTop + div.clientTop + div.clientHeight);
  455. }, 0);
  456. for (let i = -size; i < size; i += 7) {
  457. for (let j = i + 5; j < size; j += j - i) {
  458. const scrollEl = horizontal ? {
  459. scrollTop: 0,
  460. scrollLeft: i,
  461. clientHeight: 10000,
  462. clientWidth: j - i
  463. } : {
  464. scrollTop: i,
  465. scrollLeft: 0,
  466. clientHeight: j - i,
  467. clientWidth: 10000
  468. };
  469. expect((0, _ui_utils.getVisibleElements)({
  470. scrollEl,
  471. views: pages,
  472. sortByVisibility: false,
  473. horizontal,
  474. rtl
  475. })).toEqual(slowGetVisibleElements(scrollEl, pages));
  476. }
  477. }
  478. }
  479. it("with pages of varying height", function () {
  480. const pages = makePages([[[50, 20], [20, 50]], [[30, 12], [12, 30]], [[20, 50], [50, 20]], [[50, 20], [20, 50]]]);
  481. scrollOverDocument(pages);
  482. });
  483. it("widescreen challenge", function () {
  484. const pages = makePages([[[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]], [[10, 90], [10, 80], [10, 70], [10, 60], [10, 50]], [[10, 50], [10, 60], [10, 70], [10, 80], [10, 90]]]);
  485. scrollOverDocument(pages);
  486. });
  487. it("works with horizontal scrolling", function () {
  488. const pages = makePages([[[10, 50], [20, 20], [30, 10]]]);
  489. scrollOverDocument(pages, true);
  490. });
  491. it("works with horizontal scrolling with RTL-documents", function () {
  492. const pages = makePages([[[-10, 50], [-20, 20], [-30, 10]]]);
  493. scrollOverDocument(pages, true, true);
  494. });
  495. it("handles `sortByVisibility` correctly", function () {
  496. const scrollEl = {
  497. scrollTop: 75,
  498. scrollLeft: 0,
  499. clientHeight: 750,
  500. clientWidth: 1500
  501. };
  502. const views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  503. const visible = (0, _ui_utils.getVisibleElements)({
  504. scrollEl,
  505. views
  506. });
  507. const visibleSorted = (0, _ui_utils.getVisibleElements)({
  508. scrollEl,
  509. views,
  510. sortByVisibility: true
  511. });
  512. const viewsOrder = [],
  513. viewsSortedOrder = [];
  514. for (const view of visible.views) {
  515. viewsOrder.push(view.id);
  516. }
  517. for (const view of visibleSorted.views) {
  518. viewsSortedOrder.push(view.id);
  519. }
  520. expect(viewsOrder).toEqual([0, 1, 2]);
  521. expect(viewsSortedOrder).toEqual([1, 2, 0]);
  522. });
  523. it("handles views being empty", function () {
  524. const scrollEl = {
  525. scrollTop: 10,
  526. scrollLeft: 0,
  527. clientHeight: 750,
  528. clientWidth: 1500
  529. };
  530. const views = [];
  531. expect((0, _ui_utils.getVisibleElements)({
  532. scrollEl,
  533. views
  534. })).toEqual({
  535. first: undefined,
  536. last: undefined,
  537. views: []
  538. });
  539. });
  540. it("handles all views being hidden (without errors)", function () {
  541. const scrollEl = {
  542. scrollTop: 100000,
  543. scrollLeft: 0,
  544. clientHeight: 750,
  545. clientWidth: 1500
  546. };
  547. const views = makePages([[[100, 150]], [[100, 150]], [[100, 150]]]);
  548. expect((0, _ui_utils.getVisibleElements)({
  549. scrollEl,
  550. views
  551. })).toEqual({
  552. first: undefined,
  553. last: undefined,
  554. views: []
  555. });
  556. });
  557. describe("backtrackBeforeAllVisibleElements", function () {
  558. const tallPage = [10, 50];
  559. const shortPage = [10, 10];
  560. const top1 = 20 + SPACING + 40;
  561. const top2 = 20 + SPACING + 10;
  562. it("handles case 1", function () {
  563. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]], [[10, 20]]]);
  564. const bsResult = 4;
  565. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  566. });
  567. it("handles case 2", function () {
  568. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, tallPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  569. const bsResult = 6;
  570. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  571. });
  572. it("handles case 3", function () {
  573. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  574. const bsResult = 8;
  575. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top1)).toEqual(4);
  576. });
  577. it("handles case 4", function () {
  578. const pages = makePages([[[10, 20], [10, 20], [10, 20], [10, 20]], [tallPage, shortPage, tallPage, shortPage], [[10, 50], [10, 50], [10, 50], [10, 50]], [[10, 20], [10, 20], [10, 20], [10, 20]]]);
  579. const bsResult = 4;
  580. expect((0, _ui_utils.backtrackBeforeAllVisibleElements)(bsResult, pages, top2)).toEqual(4);
  581. });
  582. });
  583. });
  584. describe("moveToEndOfArray", function () {
  585. it("works on empty arrays", function () {
  586. const data = [];
  587. (0, _ui_utils.moveToEndOfArray)(data, function () {});
  588. expect(data).toEqual([]);
  589. });
  590. it("works when moving everything", function () {
  591. const data = [1, 2, 3, 4, 5];
  592. (0, _ui_utils.moveToEndOfArray)(data, function () {
  593. return true;
  594. });
  595. expect(data).toEqual([1, 2, 3, 4, 5]);
  596. });
  597. it("works when moving some things", function () {
  598. const data = [1, 2, 3, 4, 5];
  599. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  600. return x % 2 === 0;
  601. });
  602. expect(data).toEqual([1, 3, 5, 2, 4]);
  603. });
  604. it("works when moving one thing", function () {
  605. const data = [1, 2, 3, 4, 5];
  606. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  607. return x === 1;
  608. });
  609. expect(data).toEqual([2, 3, 4, 5, 1]);
  610. });
  611. it("works when moving nothing", function () {
  612. const data = [1, 2, 3, 4, 5];
  613. (0, _ui_utils.moveToEndOfArray)(data, function (x) {
  614. return x === 0;
  615. });
  616. expect(data).toEqual([1, 2, 3, 4, 5]);
  617. });
  618. });
  619. });