Best JavaScript code snippet using best
position_hook_tests.js
Source:position_hook_tests.js
1/** @odoo-module */2import { browser } from "@web/core/browser/browser";3import { computePositioning, DEFAULTS, usePosition } from "@web/core/position/position_hook";4import { registerCleanup } from "../helpers/cleanup";5import {6 getFixture,7 mockAnimationFrame,8 nextTick,9 patchWithCleanup,10 triggerEvent,11} from "../helpers/utils";12const { Component, mount, tags } = owl;13const { css, xml } = tags;14let container;15let reference;16/**17 * @param {HTMLElement} popper18 * @param {import("@web/core/position/position_hook").Position} [position="bottom"]19 * @returns {boolean}20 */21function isWellPositioned(popper, position = "bottom") {22 /** @type {import("@web/core/position/position_hook").Options} */23 const options = {24 ...DEFAULTS,25 container,26 position,27 };28 const [direction, variant = "middle"] = position.split("-");29 const d = /** @type {import("@web/core/position/position_hook").DirectionsDataKey} */ (direction[0]);30 const v = /** @type {import("@web/core/position/position_hook").VariantsDataKey} */ (variant[0]);31 const posSolution = computePositioning(reference, popper, options).get(d, v);32 const hasCorrectClass = popper.classList.contains(posSolution.className);33 const correctLeft = parseFloat(popper.style.left) === posSolution.left;34 const correctTop = parseFloat(popper.style.top) === posSolution.top;35 return hasCorrectClass && correctLeft && correctTop;36}37class TestComp extends Component {38 setup() {39 usePosition(reference, this.constructor.popperOptions);40 }41}42TestComp.style = css`43 #popper {44 background-color: cyan;45 height: 100px;46 width: 100px;47 }48`;49TestComp.template = xml`<div id="popper"/>`;50/** @type {import("@web/core/position/position_hook").Options} */51TestComp.popperOptions = {};52QUnit.module("usePosition Hook", {53 async beforeEach() {54 reference = document.createElement("div");55 reference.id = "reference";56 reference.style.backgroundColor = "yellow";57 reference.style.height = "50px";58 reference.style.width = "50px";59 // Force container style, to make these tests independent of screen size60 container = document.createElement("div");61 container.id = "container";62 container.style.backgroundColor = "pink";63 container.style.height = "450px";64 container.style.width = "450px";65 container.style.display = "flex";66 container.style.alignItems = "center";67 container.style.justifyContent = "center";68 container.appendChild(reference);69 getFixture().prepend(container);70 registerCleanup(() => {71 getFixture().removeChild(container);72 });73 TestComp.popperOptions = { container };74 patchWithCleanup(browser, { setTimeout: (func) => func() });75 },76});77QUnit.test("default position is bottom", async (assert) => {78 const popper = await mount(TestComp, { target: container });79 assert.ok(isWellPositioned(popper.el, "bottom"));80});81QUnit.test("can add margin", async (assert) => {82 let margin = 0;83 Object.assign(TestComp.popperOptions, { margin });84 let popper = await mount(TestComp, { target: container });85 const popBox1 = popper.el.getBoundingClientRect();86 popper.destroy();87 margin = 20;88 Object.assign(TestComp.popperOptions, { margin });89 popper = await mount(TestComp, { target: container });90 const popBox2 = popper.el.getBoundingClientRect();91 popper.destroy();92 assert.strictEqual(popBox1.top + margin, popBox2.top);93});94QUnit.test("can use a t-ref as popper", async (assert) => {95 patchWithCleanup(TestComp, {96 popperOptions: { ...TestComp.popperOptions, popper: "popperRef" },97 template: xml`98 <div id="not-popper">99 <div id="popper" t-ref="popperRef"/>100 </div>101 `,102 });103 const comp = await mount(TestComp, { target: container });104 assert.notOk(isWellPositioned(comp.el));105 assert.ok(isWellPositioned(comp.el.querySelector("#popper")));106});107QUnit.test("has no effect when component is destroyed", async (assert) => {108 const execRegisteredCallbacks = mockAnimationFrame();109 const originalReference = reference;110 reference = () => {111 assert.step("reference called");112 return originalReference;113 };114 const popper = await mount(TestComp, { target: container });115 assert.verifySteps(["reference called"], "reference called when component mounted");116 triggerEvent(document, null, "scroll");117 await nextTick();118 assert.verifySteps([]);119 execRegisteredCallbacks();120 assert.verifySteps(["reference called"], "reference called when document scrolled");121 triggerEvent(document, null, "scroll");122 await nextTick();123 popper.destroy();124 execRegisteredCallbacks();125 assert.verifySteps(126 [],127 "reference not called even if scroll happened right before the component destroys"128 );129});130const getPositionTest = (position, positionToCheck) => {131 return async (assert) => {132 Object.assign(TestComp.popperOptions, { position });133 const popper = await mount(TestComp, { target: container });134 positionToCheck = positionToCheck || position;135 assert.ok(isWellPositioned(popper.el, positionToCheck));136 };137};138QUnit.test("position top", getPositionTest("top"));139QUnit.test("position left", getPositionTest("left"));140QUnit.test("position bottom", getPositionTest("bottom"));141QUnit.test("position right", getPositionTest("right"));142QUnit.test("position top-start", getPositionTest("top-start"));143QUnit.test("position top-middle", getPositionTest("top-middle"));144QUnit.test("position top-end", getPositionTest("top-end"));145QUnit.test("position left-start", getPositionTest("left-start"));146QUnit.test("position left-middle", getPositionTest("left-middle"));147QUnit.test("position left-end", getPositionTest("left-end"));148QUnit.test("position bottom-start", getPositionTest("bottom-start"));149QUnit.test("position bottom-middle", getPositionTest("bottom-middle"));150QUnit.test("position bottom-end", getPositionTest("bottom-end"));151QUnit.test("position right-start", getPositionTest("right-start"));152QUnit.test("position right-middle", getPositionTest("right-middle"));153QUnit.test("position right-end", getPositionTest("right-end"));154QUnit.test("position top === top-middle", getPositionTest("top", "top-middle"));155QUnit.test("position left === left-middle", getPositionTest("left", "left-middle"));156QUnit.test("position bottom === bottom-middle", getPositionTest("bottom", "bottom-middle"));157QUnit.test("position right === right-middle", getPositionTest("right", "right-middle"));158const CONTAINER_STYLE_MAP = {159 top: { alignItems: "flex-start" },160 bottom: { alignItems: "flex-end" },161 left: { justifyContent: "flex-start" },162 right: { justifyContent: "flex-end" },163 slimfit: { height: "100px", width: "100px" }, // height and width of popper164 h125: { height: "125px" }, // height of popper + 1/2 reference165 w125: { width: "125px" }, // width of popper + 1/2 reference166};167const getRepositionTest = (from, to, containerStyleChanges) => {168 return async (assert) => {169 Object.assign(TestComp.popperOptions, { position: from });170 const popper = await mount(TestComp, { target: container });171 assert.ok(isWellPositioned(popper.el, from), `points to ${from} direction`);172 // Change container style and force update173 for (const styleToApply of containerStyleChanges.split(" ")) {174 Object.assign(container.style, CONTAINER_STYLE_MAP[styleToApply]);175 }176 triggerEvent(document, null, "scroll");177 await nextTick();178 assert.ok(isWellPositioned(popper.el, to), `points to ${to} direction`);179 };180};181// -----------------------------------------------------------------------------182QUnit.test(183 "reposition from top-start to bottom-start",184 getRepositionTest("top-start", "bottom-start", "top")185);186QUnit.test(187 "reposition from top-start to bottom-middle",188 getRepositionTest("top-start", "bottom-middle", "top w125")189);190QUnit.test(191 "reposition from top-start to bottom-end",192 getRepositionTest("top-start", "bottom-end", "top right")193);194QUnit.test(195 "reposition from top-start to right-start",196 getRepositionTest("top-start", "right-start", "h125 top")197);198QUnit.test(199 "reposition from top-start to right-middle",200 getRepositionTest("top-start", "right-middle", "h125")201);202QUnit.test(203 "reposition from top-start to right-end",204 getRepositionTest("top-start", "right-end", "h125 bottom")205);206QUnit.test(207 "reposition from top-start to left-start",208 getRepositionTest("top-start", "left-start", "h125 right top")209);210QUnit.test(211 "reposition from top-start to left-middle",212 getRepositionTest("top-start", "left-middle", "h125 right")213);214QUnit.test(215 "reposition from top-start to left-end",216 getRepositionTest("top-start", "left-end", "h125 right bottom")217);218QUnit.test(219 "reposition from top-start to top-start",220 getRepositionTest("top-start", "top-start", "slimfit")221);222QUnit.test(223 "reposition from top-start to top-middle",224 getRepositionTest("top-start", "top-middle", "w125")225);226QUnit.test(227 "reposition from top-start to top-end",228 getRepositionTest("top-start", "top-end", "right")229);230// -----------------------------------------------------------------------------231QUnit.test(232 "reposition from top-middle to bottom-start",233 getRepositionTest("top-middle", "bottom-start", "top left")234);235QUnit.test(236 "reposition from top-middle to bottom-middle",237 getRepositionTest("top-middle", "bottom-middle", "top")238);239QUnit.test(240 "reposition from top-middle to bottom-end",241 getRepositionTest("top-middle", "bottom-end", "top right")242);243QUnit.test(244 "reposition from top-middle to right-start",245 getRepositionTest("top-middle", "right-start", "h125 top")246);247QUnit.test(248 "reposition from top-middle to right-middle",249 getRepositionTest("top-middle", "right-middle", "h125")250);251QUnit.test(252 "reposition from top-middle to right-end",253 getRepositionTest("top-middle", "right-end", "h125 bottom")254);255QUnit.test(256 "reposition from top-middle to left-start",257 getRepositionTest("top-middle", "left-start", "h125 right top")258);259QUnit.test(260 "reposition from top-middle to left-middle",261 getRepositionTest("top-middle", "left-middle", "h125 right")262);263QUnit.test(264 "reposition from top-middle to left-end",265 getRepositionTest("top-middle", "left-end", "h125 right bottom")266);267QUnit.test(268 "reposition from top-middle to top-start",269 getRepositionTest("top-middle", "top-start", "left")270);271QUnit.test(272 "reposition from top-middle to top-middle",273 getRepositionTest("top-middle", "top-middle", "slimfit")274);275QUnit.test(276 "reposition from top-middle to top-end",277 getRepositionTest("top-middle", "top-end", "right")278);279// -----------------------------------------------------------------------------280QUnit.test(281 "reposition from top-end to bottom-start",282 getRepositionTest("top-end", "bottom-start", "top left")283);284QUnit.test(285 "reposition from top-end to bottom-middle",286 getRepositionTest("top-end", "bottom-middle", "top w125")287);288QUnit.test(289 "reposition from top-end to bottom-end",290 getRepositionTest("top-end", "bottom-end", "top")291);292QUnit.test(293 "reposition from top-end to right-start",294 getRepositionTest("top-end", "right-start", "h125 top")295);296QUnit.test(297 "reposition from top-end to right-middle",298 getRepositionTest("top-end", "right-middle", "h125")299);300QUnit.test(301 "reposition from top-end to right-end",302 getRepositionTest("top-end", "right-end", "h125 bottom")303);304QUnit.test(305 "reposition from top-end to left-start",306 getRepositionTest("top-end", "left-start", "h125 right top")307);308QUnit.test(309 "reposition from top-end to left-middle",310 getRepositionTest("top-end", "left-middle", "h125 right")311);312QUnit.test(313 "reposition from top-end to left-end",314 getRepositionTest("top-end", "left-end", "h125 right bottom")315);316QUnit.test(317 "reposition from top-end to top-start",318 getRepositionTest("top-end", "top-start", "left")319);320QUnit.test(321 "reposition from top-end to top-middle",322 getRepositionTest("top-end", "top-middle", "w125")323);324QUnit.test(325 "reposition from top-end to top-end",326 getRepositionTest("top-end", "top-end", "slimfit")327);328// -----------------------------------------------------------------------------329QUnit.test(330 "reposition from left-start to bottom-start",331 getRepositionTest("left-start", "bottom-start", "w125 left")332);333QUnit.test(334 "reposition from left-start to bottom-middle",335 getRepositionTest("left-start", "bottom-middle", "w125")336);337QUnit.test(338 "reposition from left-start to bottom-end",339 getRepositionTest("left-start", "bottom-end", "w125 right")340);341QUnit.test(342 "reposition from left-start to right-start",343 getRepositionTest("left-start", "right-start", "left")344);345QUnit.test(346 "reposition from left-start to right-middle",347 getRepositionTest("left-start", "right-middle", "left h125")348);349QUnit.test(350 "reposition from left-start to right-end",351 getRepositionTest("left-start", "right-end", "left bottom")352);353QUnit.test(354 "reposition from left-start to left-start",355 getRepositionTest("left-start", "left-start", "slimfit")356);357QUnit.test(358 "reposition from left-start to left-middle",359 getRepositionTest("left-start", "left-middle", "h125")360);361QUnit.test(362 "reposition from left-start to left-end",363 getRepositionTest("left-start", "left-end", "bottom")364);365QUnit.test(366 "reposition from left-start to top-start",367 getRepositionTest("left-start", "top-start", "w125 bottom left")368);369QUnit.test(370 "reposition from left-start to top-middle",371 getRepositionTest("left-start", "top-middle", "w125 bottom")372);373QUnit.test(374 "reposition from left-start to top-end",375 getRepositionTest("left-start", "top-end", "w125 bottom right")376);377// -----------------------------------------------------------------------------378QUnit.test(379 "reposition from left-middle to bottom-start",380 getRepositionTest("left-middle", "bottom-start", "w125 left")381);382QUnit.test(383 "reposition from left-middle to bottom-middle",384 getRepositionTest("left-middle", "bottom-middle", "w125")385);386QUnit.test(387 "reposition from left-middle to bottom-end",388 getRepositionTest("left-middle", "bottom-end", "w125 right")389);390QUnit.test(391 "reposition from left-middle to right-start",392 getRepositionTest("left-middle", "right-start", "left top")393);394QUnit.test(395 "reposition from left-middle to right-middle",396 getRepositionTest("left-middle", "right-middle", "left")397);398QUnit.test(399 "reposition from left-middle to right-end",400 getRepositionTest("left-middle", "right-end", "left bottom")401);402QUnit.test(403 "reposition from left-middle to top-start",404 getRepositionTest("left-middle", "top-start", "w125 bottom left")405);406QUnit.test(407 "reposition from left-middle to top-middle",408 getRepositionTest("left-middle", "top-middle", "w125 bottom")409);410QUnit.test(411 "reposition from left-middle to top-end",412 getRepositionTest("left-middle", "top-end", "w125 bottom right")413);414QUnit.test(415 "reposition from left-middle to left-start",416 getRepositionTest("left-middle", "left-start", "top")417);418QUnit.test(419 "reposition from left-middle to left-middle",420 getRepositionTest("left-middle", "left-middle", "slimfit")421);422QUnit.test(423 "reposition from left-middle to left-end",424 getRepositionTest("left-middle", "left-end", "bottom")425);426// -----------------------------------------------------------------------------427QUnit.test(428 "reposition from left-end to bottom-start",429 getRepositionTest("left-end", "bottom-start", "w125 left")430);431QUnit.test(432 "reposition from left-end to bottom-middle",433 getRepositionTest("left-end", "bottom-middle", "w125")434);435QUnit.test(436 "reposition from left-end to bottom-end",437 getRepositionTest("left-end", "bottom-end", "w125 right")438);439QUnit.test(440 "reposition from left-end to right-start",441 getRepositionTest("left-end", "right-start", "left top")442);443QUnit.test(444 "reposition from left-end to right-middle",445 getRepositionTest("left-end", "right-middle", "left h125")446);447QUnit.test(448 "reposition from left-end to right-end",449 getRepositionTest("left-end", "right-end", "left")450);451QUnit.test(452 "reposition from left-end to top-start",453 getRepositionTest("left-end", "top-start", "w125 bottom left")454);455QUnit.test(456 "reposition from left-end to top-middle",457 getRepositionTest("left-end", "top-middle", "w125 bottom")458);459QUnit.test(460 "reposition from left-end to top-end",461 getRepositionTest("left-end", "top-end", "w125 bottom right")462);463QUnit.test(464 "reposition from left-end to left-start",465 getRepositionTest("left-end", "left-start", "top")466);467QUnit.test(468 "reposition from left-end to left-middle",469 getRepositionTest("left-end", "left-middle", "h125")470);471QUnit.test(472 "reposition from left-end to left-end",473 getRepositionTest("left-end", "left-end", "slimfit")474);475// -----------------------------------------------------------------------------476QUnit.test(477 "reposition from bottom-start to bottom-start",478 getRepositionTest("bottom-start", "bottom-start", "slimfit")479);480QUnit.test(481 "reposition from bottom-start to bottom-middle",482 getRepositionTest("bottom-start", "bottom-middle", "w125")483);484QUnit.test(485 "reposition from bottom-start to bottom-end",486 getRepositionTest("bottom-start", "bottom-end", "right")487);488QUnit.test(489 "reposition from bottom-start to right-start",490 getRepositionTest("bottom-start", "right-start", "h125 top")491);492QUnit.test(493 "reposition from bottom-start to right-middle",494 getRepositionTest("bottom-start", "right-middle", "h125")495);496QUnit.test(497 "reposition from bottom-start to right-end",498 getRepositionTest("bottom-start", "right-end", "h125 bottom")499);500QUnit.test(501 "reposition from bottom-start to top-start",502 getRepositionTest("bottom-start", "top-start", "bottom")503);504QUnit.test(505 "reposition from bottom-start to top-middle",506 getRepositionTest("bottom-start", "top-middle", "bottom w125")507);508QUnit.test(509 "reposition from bottom-start to top-end",510 getRepositionTest("bottom-start", "top-end", "bottom right")511);512QUnit.test(513 "reposition from bottom-start to left-start",514 getRepositionTest("bottom-start", "left-start", "h125 right top")515);516QUnit.test(517 "reposition from bottom-start to left-middle",518 getRepositionTest("bottom-start", "left-middle", "h125 right")519);520QUnit.test(521 "reposition from bottom-start to left-end",522 getRepositionTest("bottom-start", "left-end", "h125 right bottom")523);524// -----------------------------------------------------------------------------525QUnit.test(526 "reposition from bottom-middle to bottom-start",527 getRepositionTest("bottom-middle", "bottom-start", "left")528);529QUnit.test(530 "reposition from bottom-middle to bottom-middle",531 getRepositionTest("bottom-middle", "bottom-middle", "slimfit")532);533QUnit.test(534 "reposition from bottom-middle to bottom-end",535 getRepositionTest("bottom-middle", "bottom-end", "right")536);537QUnit.test(538 "reposition from bottom-middle to right-start",539 getRepositionTest("bottom-middle", "right-start", "h125 top")540);541QUnit.test(542 "reposition from bottom-middle to right-middle",543 getRepositionTest("bottom-middle", "right-middle", "h125")544);545QUnit.test(546 "reposition from bottom-middle to right-end",547 getRepositionTest("bottom-middle", "right-end", "h125 bottom")548);549QUnit.test(550 "reposition from bottom-middle to top-start",551 getRepositionTest("bottom-middle", "top-start", "bottom left")552);553QUnit.test(554 "reposition from bottom-middle to top-middle",555 getRepositionTest("bottom-middle", "top-middle", "bottom")556);557QUnit.test(558 "reposition from bottom-middle to top-end",559 getRepositionTest("bottom-middle", "top-end", "bottom right")560);561QUnit.test(562 "reposition from bottom-middle to left-start",563 getRepositionTest("bottom-middle", "left-start", "h125 right top")564);565QUnit.test(566 "reposition from bottom-middle to left-middle",567 getRepositionTest("bottom-middle", "left-middle", "h125 right")568);569QUnit.test(570 "reposition from bottom-middle to left-end",571 getRepositionTest("bottom-middle", "left-end", "h125 right bottom")572);573// -----------------------------------------------------------------------------574QUnit.test(575 "reposition from bottom-end to bottom-start",576 getRepositionTest("bottom-end", "bottom-start", "left")577);578QUnit.test(579 "reposition from bottom-end to bottom-middle",580 getRepositionTest("bottom-end", "bottom-middle", "w125")581);582QUnit.test(583 "reposition from bottom-end to bottom-end",584 getRepositionTest("bottom-end", "bottom-end", "slimfit")585);586QUnit.test(587 "reposition from bottom-end to right-start",588 getRepositionTest("bottom-end", "right-start", "h125 top")589);590QUnit.test(591 "reposition from bottom-end to right-middle",592 getRepositionTest("bottom-end", "right-middle", "h125")593);594QUnit.test(595 "reposition from bottom-end to right-end",596 getRepositionTest("bottom-end", "right-end", "h125 bottom")597);598QUnit.test(599 "reposition from bottom-end to top-start",600 getRepositionTest("bottom-end", "top-start", "bottom left")601);602QUnit.test(603 "reposition from bottom-end to top-middle",604 getRepositionTest("bottom-end", "top-middle", "bottom w125")605);606QUnit.test(607 "reposition from bottom-end to top-end",608 getRepositionTest("bottom-end", "top-end", "bottom")609);610QUnit.test(611 "reposition from bottom-end to left-start",612 getRepositionTest("bottom-end", "left-start", "h125 right top")613);614QUnit.test(615 "reposition from bottom-end to left-middle",616 getRepositionTest("bottom-end", "left-middle", "h125 right")617);618QUnit.test(619 "reposition from bottom-end to left-end",620 getRepositionTest("bottom-end", "left-end", "h125 right bottom")621);622// -----------------------------------------------------------------------------623QUnit.test(624 "reposition from right-start to bottom-start",625 getRepositionTest("right-start", "bottom-start", "w125 top left")626);627QUnit.test(628 "reposition from right-start to bottom-middle",629 getRepositionTest("right-start", "bottom-middle", "w125 top")630);631QUnit.test(632 "reposition from right-start to bottom-end",633 getRepositionTest("right-start", "bottom-end", "w125 top right")634);635QUnit.test(636 "reposition from right-start to right-start",637 getRepositionTest("right-start", "right-start", "slimfit")638);639QUnit.test(640 "reposition from right-start to right-middle",641 getRepositionTest("right-start", "right-middle", "h125")642);643QUnit.test(644 "reposition from right-start to right-end",645 getRepositionTest("right-start", "right-end", "bottom")646);647QUnit.test(648 "reposition from right-start to top-start",649 getRepositionTest("right-start", "top-start", "w125 left")650);651QUnit.test(652 "reposition from right-start to top-middle",653 getRepositionTest("right-start", "top-middle", "w125")654);655QUnit.test(656 "reposition from right-start to top-end",657 getRepositionTest("right-start", "top-end", "w125 right")658);659QUnit.test(660 "reposition from right-start to left-start",661 getRepositionTest("right-start", "left-start", "right")662);663QUnit.test(664 "reposition from right-start to left-middle",665 getRepositionTest("right-start", "left-middle", "right h125")666);667QUnit.test(668 "reposition from right-start to left-end",669 getRepositionTest("right-start", "left-end", "right bottom")670);671// -----------------------------------------------------------------------------672QUnit.test(673 "reposition from right-middle to bottom-start",674 getRepositionTest("right-middle", "bottom-start", "w125 top left")675);676QUnit.test(677 "reposition from right-middle to bottom-middle",678 getRepositionTest("right-middle", "bottom-middle", "w125 top")679);680QUnit.test(681 "reposition from right-middle to bottom-end",682 getRepositionTest("right-middle", "bottom-end", "w125 top right")683);684QUnit.test(685 "reposition from right-middle to right-start",686 getRepositionTest("right-middle", "right-start", "top")687);688QUnit.test(689 "reposition from right-middle to right-middle",690 getRepositionTest("right-middle", "right-middle", "slimfit")691);692QUnit.test(693 "reposition from right-middle to right-end",694 getRepositionTest("right-middle", "right-end", "bottom")695);696QUnit.test(697 "reposition from right-middle to left-start",698 getRepositionTest("right-middle", "left-start", "right top")699);700QUnit.test(701 "reposition from right-middle to left-middle",702 getRepositionTest("right-middle", "left-middle", "right")703);704QUnit.test(705 "reposition from right-middle to left-end",706 getRepositionTest("right-middle", "left-end", "right bottom")707);708QUnit.test(709 "reposition from right-middle to top-start",710 getRepositionTest("right-middle", "top-start", "w125 left")711);712QUnit.test(713 "reposition from right-middle to top-middle",714 getRepositionTest("right-middle", "top-middle", "w125")715);716QUnit.test(717 "reposition from right-middle to top-end",718 getRepositionTest("right-middle", "top-end", "w125 right")719);720// -----------------------------------------------------------------------------721QUnit.test(722 "reposition from right-end to bottom-start",723 getRepositionTest("right-end", "bottom-start", "w125 top left")724);725QUnit.test(726 "reposition from right-end to bottom-middle",727 getRepositionTest("right-end", "bottom-middle", "w125 top")728);729QUnit.test(730 "reposition from right-end to bottom-end",731 getRepositionTest("right-end", "bottom-end", "w125 top right")732);733QUnit.test(734 "reposition from right-end to right-start",735 getRepositionTest("right-end", "right-start", "top")736);737QUnit.test(738 "reposition from right-end to right-middle",739 getRepositionTest("right-end", "right-middle", "h125")740);741QUnit.test(742 "reposition from right-end to right-end",743 getRepositionTest("right-end", "right-end", "slimfit")744);745QUnit.test(746 "reposition from right-end to left-start",747 getRepositionTest("right-end", "left-start", "right top")748);749QUnit.test(750 "reposition from right-end to left-middle",751 getRepositionTest("right-end", "left-middle", "right h125")752);753QUnit.test(754 "reposition from right-end to left-end",755 getRepositionTest("right-end", "left-end", "right")756);757QUnit.test(758 "reposition from right-end to top-start",759 getRepositionTest("right-end", "top-start", "w125 left")760);761QUnit.test(762 "reposition from right-end to top-middle",763 getRepositionTest("right-end", "top-middle", "w125")764);765QUnit.test(766 "reposition from right-end to top-end",767 getRepositionTest("right-end", "top-end", "w125 right")...
applySourceMap.js
Source:applySourceMap.js
1/*2 MIT License http://www.opensource.org/licenses/mit-license.php3 Author Tobias Koppers @sokra4*/5"use strict";6var SourceNode = require("source-map").SourceNode;7var SourceMapConsumer = require("source-map").SourceMapConsumer;8var applySourceMap = function(9 sourceNode,10 sourceMapConsumer,11 sourceFile,12 removeGeneratedCodeForSourceFile13) {14 // The following notations are used to name stuff:15 // Left <------------> Middle <-------------------> Right16 // Input arguments:17 // sourceNode - Code mapping from Left to Middle18 // sourceFile - Name of a Middle file19 // sourceMapConsumer - Code mapping from Middle to Right20 // Variables:21 // l2m m2r22 // Left <-----------------------------------------> Right23 // Variables:24 // l2r25 var l2rResult = new SourceNode();26 var l2rOutput = [];27 var middleSourceContents = {};28 var m2rMappingsByLine = {};29 var rightSourceContentsSet = {};30 var rightSourceContentsLines = {};31 // Store all mappings by generated line32 sourceMapConsumer.eachMapping(33 function(mapping) {34 (m2rMappingsByLine[mapping.generatedLine] =35 m2rMappingsByLine[mapping.generatedLine] || []).push(mapping);36 },37 null,38 SourceMapConsumer.GENERATED_ORDER39 );40 // Store all source contents41 sourceNode.walkSourceContents(function(source, content) {42 middleSourceContents["$" + source] = content;43 });44 var middleSource = middleSourceContents["$" + sourceFile];45 var middleSourceLines = middleSource ? middleSource.split("\n") : undefined;46 // Walk all left to middle mappings47 sourceNode.walk(function(chunk, middleMapping) {48 var source;49 // Find a mapping from middle to right50 if(51 middleMapping.source === sourceFile &&52 middleMapping.line &&53 m2rMappingsByLine[middleMapping.line]54 ) {55 var m2rBestFit;56 var m2rMappings = m2rMappingsByLine[middleMapping.line];57 // Note: if this becomes a performance problem, use binary search58 for(var i = 0; i < m2rMappings.length; i++) {59 if(m2rMappings[i].generatedColumn <= middleMapping.column) {60 m2rBestFit = m2rMappings[i];61 }62 }63 if(m2rBestFit) {64 var allowMiddleName = false;65 var middleLine;66 var rightSourceContent;67 var rightSourceContentLines;68 var rightSource = m2rBestFit.source;69 // Check if we have middle and right source for this mapping70 // Then we could have an "identify" mapping71 if(72 middleSourceLines &&73 rightSource &&74 (middleLine = middleSourceLines[m2rBestFit.generatedLine - 1]) &&75 ((rightSourceContentLines = rightSourceContentsLines[rightSource]) ||76 (rightSourceContent = sourceMapConsumer.sourceContentFor(77 rightSource,78 true79 )))80 ) {81 if(!rightSourceContentLines) {82 rightSourceContentLines = rightSourceContentsLines[83 rightSource84 ] = rightSourceContent.split("\n");85 }86 var rightLine = rightSourceContentLines[m2rBestFit.originalLine - 1];87 if(rightLine) {88 var offset = middleMapping.column - m2rBestFit.generatedColumn;89 if(offset > 0) {90 var middlePart = middleLine.slice(91 m2rBestFit.generatedColumn,92 middleMapping.column93 );94 var rightPart = rightLine.slice(95 m2rBestFit.originalColumn,96 m2rBestFit.originalColumn + offset97 );98 if(middlePart === rightPart) {99 // When original and generated code is equal we assume we have an "identity" mapping100 // In this case we can offset the original position101 m2rBestFit = Object.assign({}, m2rBestFit, {102 originalColumn: m2rBestFit.originalColumn + offset,103 generatedColumn: middleMapping.column104 });105 }106 }107 if(!m2rBestFit.name && middleMapping.name) {108 allowMiddleName =109 rightLine.slice(110 m2rBestFit.originalColumn,111 m2rBestFit.originalColumn + middleMapping.name.length112 ) === middleMapping.name;113 }114 }115 }116 // Construct a left to right node from the found middle to right mapping117 source = m2rBestFit.source;118 l2rOutput.push(119 new SourceNode(120 m2rBestFit.originalLine,121 m2rBestFit.originalColumn,122 source,123 chunk,124 allowMiddleName ? middleMapping.name : m2rBestFit.name125 )126 );127 // Set the source contents once128 if(!("$" + source in rightSourceContentsSet)) {129 rightSourceContentsSet["$" + source] = true;130 var sourceContent = sourceMapConsumer.sourceContentFor(source, true);131 if(sourceContent) {132 l2rResult.setSourceContent(source, sourceContent);133 }134 }135 return;136 }137 }138 if((removeGeneratedCodeForSourceFile && middleMapping.source === sourceFile) || !middleMapping.source) {139 // Construct a left to middle node with only generated code140 // Because user do not want mappings to middle sources141 // Or this chunk has no mapping142 l2rOutput.push(chunk);143 return;144 }145 // Construct a left to middle node146 source = middleMapping.source;147 l2rOutput.push(148 new SourceNode(149 middleMapping.line,150 middleMapping.column,151 source,152 chunk,153 middleMapping.name154 )155 );156 if("$" + source in middleSourceContents) {157 if(!("$" + source in rightSourceContentsSet)) {158 l2rResult.setSourceContent(source, middleSourceContents["$" + source]);159 delete middleSourceContents["$" + source];160 }161 }162 });163 // Put output into the resulting SourceNode164 l2rResult.add(l2rOutput);165 return l2rResult;166};...
Using AI Code Generation
1var middle = require('bestlibrary').middle;2console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));3var middle = require('bestlibrary').middle;4console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));5var middle = require('bestlibrary').middle;6console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));7var middle = require('bestlibrary').middle;8console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));9var middle = require('bestlibrary').middle;10console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));11var middle = require('bestlibrary').middle;12console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));13var middle = require('bestlibrary').middle;14console.log(middle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));15var middle = require('bestlibrary').middle;16console.log(middle
Using AI Code Generation
1var best = require('./best.js');2console.log(best.middle('Hello World'));3var good = require('./good.js');4console.log(good.middle('Hello World'));5var bad = require('./bad.js');6console.log(bad.middle('Hello World'));
Using AI Code Generation
1var BestRoute = require('bestroute');2var route = new BestRoute();3route.add('A', 'B', 5);4route.add('B', 'C', 10);5route.add('C', 'D', 5);6route.add('D', 'E', 10);7route.add('E', 'F', 5);8route.add('F', 'G', 10);9route.add('G', 'H', 5);10route.add('H', 'I', 10);11route.add('I', 'J', 5);12route.add('J', 'K', 10);13route.add('K', 'L', 5);14route.add('L', 'M', 10);15route.add('M', 'N', 5);16route.add('N', 'O', 10);17route.add('O', 'P', 5);18route.add('P', 'Q', 10);19route.add('Q', 'R', 5);20route.add('R', 'S', 10);21route.add('S', 'T', 5);22route.add('T', 'U', 10);23route.add('U', 'V', 5);24route.add('V', 'W', 10);25route.add('W', 'X', 5);26route.add('X', 'Y', 10);27route.add('Y', 'Z', 5);28route.add('Z', 'A', 10);29route.add('A', 'C', 15);30route.add('C', 'E', 15);31route.add('E', 'G', 15);32route.add('G', 'I', 15);33route.add('I', 'K', 15);34route.add('K', 'M', 15);35route.add('M', 'O', 15);36route.add('O', 'Q', 15);37route.add('Q', 'S', 15);38route.add('S', 'U', 15);39route.add('U', 'W', 15);40route.add('W', 'Y', 15);41route.add('Y', 'A', 15);42route.add('A', 'E', 25);43route.add('E', 'I', 25);44route.add('I', 'M', 25);45route.add('M', 'Q', 25);46route.add('Q', 'U',
Using AI Code Generation
1var BestRoute = require('./BestRoute');2var bestRoute = new BestRoute();3var route = bestRoute.middle('A','C','B');4console.log(route);5var BestRoute = function() {6};7BestRoute.prototype.middle = function(start, middle, end) {8 return start + '->' + middle + '->' + end;9};10module.exports = BestRoute;
Using AI Code Generation
1var bestMiddle = require('./BestMiddle.js');2var middle = bestMiddle([1,2,3,4,5]);3console.log(middle);4module.exports = function(array){5 var middle = Math.floor(array.length / 2);6 return array[middle];7};8var bestMiddle = require('./BestMiddle.js');9var middle = bestMiddle([1,2,3,4,5]);10console.log(middle);11module.exports = function(array){12 var middle = Math.floor(array.length / 2);13 return array[middle];14};15var bestMiddle = require('./BestMiddle.js');16var middle = bestMiddle([1,2,3,4,5]);17console.log(middle);18module.exports = function(array){19 var middle = Math.floor(array.length / 2);20 return array[middle];21};22var bestMiddle = require('./BestMiddle.js');23var middle = bestMiddle([1,2,3,4,5]);24console.log(middle);25module.exports = function(array){26 var middle = Math.floor(array.length / 2);27 return array[middle];28};
Using AI Code Generation
1var BestRoute = require('./BestRoute.js');2 {from: 'Seattle', to: 'San Francisco', cost: 300},3 {from: 'Seattle', to: 'San Francisco', cost: 200},4 {from: 'Seattle', to: 'San Francisco', cost: 100},5 {from: 'San Francisco', to: 'Chicago', cost: 100},6 {from: 'San Francisco', to: 'Chicago', cost: 200},7 {from: 'San Francisco', to: 'Chicago', cost: 300},8 {from: 'Chicago', to: 'New York', cost: 100},9 {from: 'Chicago', to: 'New York', cost: 200},10 {from: 'Chicago', to: 'New York', cost: 300},11 {from: 'New York', to: 'San Francisco', cost: 100},12 {from: 'New York', to: 'San Francisco', cost: 200},13 {from: 'New York', to: 'San Francisco', cost: 300},14 {from: 'Seattle', to: 'New York', cost: 100},15 {from: 'Seattle', to: 'New York', cost: 200},16 {from: 'Seattle', to: 'New York', cost: 300},17 {from: 'New York', to: 'Seattle', cost: 100},18 {from: 'New York', to: 'Seattle', cost: 200},19 {from: 'New York', to: 'Seattle', cost: 300},20 {from: 'San Francisco', to: 'Seattle', cost: 100},21 {from: 'San Francisco', to: 'Seattle', cost: 200},22 {from: 'San Francisco', to: 'Seattle', cost: 300},23 {from: 'Chicago', to: 'San Francisco', cost: 100},24 {from: 'Chicago', to: 'San Francisco', cost: 200},25 {from: 'Chicago', to: 'San Francisco', cost: 300},26 {from: 'Seattle', to: 'Chicago', cost: 100},27 {from
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!