| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { |
| 4 |
animationFrame, |
| 5 |
click, |
| 6 |
describe, |
| 7 |
expect, |
| 8 |
formatXml, |
| 9 |
getActiveElement, |
| 10 |
getFixture, |
| 11 |
getFocusableElements, |
| 12 |
getNextFocusableElement, |
| 13 |
getPreviousFocusableElement, |
| 14 |
isDisplayed, |
| 15 |
isEditable, |
| 16 |
isFocusable, |
| 17 |
isInDOM, |
| 18 |
isVisible, |
| 19 |
mockTouch, |
| 20 |
queryAll, |
| 21 |
queryAllRects, |
| 22 |
queryAllTexts, |
| 23 |
queryFirst, |
| 24 |
queryOne, |
| 25 |
queryRect, |
| 26 |
test, |
| 27 |
waitFor, |
| 28 |
waitForNone, |
| 29 |
} from "@odoo/hoot"; |
| 30 |
import { getParentFrame } from "@web/../lib/hoot-dom/helpers/dom"; |
| 31 |
import { mountForTest, parseUrl } from "../local_helpers"; |
| 32 |
|
| 33 |
const $ = queryFirst; |
| 34 |
const $1 = queryOne; |
| 35 |
const $$ = queryAll; |
| 36 |
|
| 37 |
/** |
| 38 |
* @param {...string} queryAllSelectors |
| 39 |
*/ |
| 40 |
const expectSelector = (...queryAllSelectors) => { |
| 41 |
/** |
| 42 |
* @param {string} nativeSelector |
| 43 |
*/ |
| 44 |
const toEqualNodes = (nativeSelector, options) => { |
| 45 |
if (typeof nativeSelector !== "string") { |
| 46 |
throw new Error(`Invalid selector: ${nativeSelector}`); |
| 47 |
} |
| 48 |
let root = options?.root || getFixture(); |
| 49 |
if (typeof root === "string") { |
| 50 |
root = getFixture().querySelector(root); |
| 51 |
if (root.tagName === "IFRAME") { |
| 52 |
root = root.contentDocument; |
| 53 |
} |
| 54 |
} |
| 55 |
let nodes = nativeSelector ? [...root.querySelectorAll(nativeSelector)] : []; |
| 56 |
if (Number.isInteger(options?.index)) { |
| 57 |
nodes = [nodes.at(options.index)]; |
| 58 |
} |
| 59 |
|
| 60 |
const selector = queryAllSelectors.join(", "); |
| 61 |
const fnNodes = $$(selector); |
| 62 |
expect(fnNodes).toEqual($$`${selector}`, { |
| 63 |
message: `should return the same result from a tagged template literal`, |
| 64 |
}); |
| 65 |
expect(fnNodes).toEqual(nodes, { |
| 66 |
message: `should match ${nodes.length} nodes`, |
| 67 |
}); |
| 68 |
}; |
| 69 |
|
| 70 |
return { toEqualNodes }; |
| 71 |
}; |
| 72 |
|
| 73 |
/** |
| 74 |
* @param {Document} document |
| 75 |
* @param {HTMLElement} [root] |
| 76 |
* @returns {Promise<HTMLIFrameElement>} |
| 77 |
*/ |
| 78 |
const makeIframe = (document, root) => |
| 79 |
new Promise((resolve) => { |
| 80 |
const iframe = document.createElement("iframe"); |
| 81 |
iframe.addEventListener("load", () => resolve(iframe)); |
| 82 |
iframe.srcdoc = "<body></body>"; |
| 83 |
(root || document.body).appendChild(iframe); |
| 84 |
}); |
| 85 |
|
| 86 |
const FULL_HTML_TEMPLATE = /* html */ ` |
| 87 |
<header> |
| 88 |
<h1 class="title">Title</h1> |
| 89 |
</header> |
| 90 |
<main id="custom-html"> |
| 91 |
<h5 class="title">List header</h5> |
| 92 |
<ul colspan="1" class="overflow-auto" style="max-height: 80px"> |
| 93 |
<li class="text highlighted">First item</li> |
| 94 |
<li class="text">Second item</li> |
| 95 |
<li class="text">Last item</li> |
| 96 |
</ul> |
| 97 |
<p colspan="2" class="text"> |
| 98 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo |
| 99 |
velit, tristique vitae neque a, faucibus mollis dui. Aliquam iaculis |
| 100 |
sodales mi id posuere. Proin malesuada bibendum pellentesque. Phasellus |
| 101 |
mattis at massa quis gravida. Morbi luctus interdum mi, quis dapibus |
| 102 |
augue. Vivamus condimentum nunc mi, vitae suscipit turpis dictum nec. |
| 103 |
Sed varius diam dui, eget ultricies ante dictum ac. |
| 104 |
</p> |
| 105 |
<div class="hidden" style="display: none;">Invisible section</div> |
| 106 |
<svg></svg> |
| 107 |
<form class="overflow-auto" style="max-width: 100px"> |
| 108 |
<h5 class="title">Form title</h5> |
| 109 |
<input name="name" type="text" value="John Doe (JOD)" /> |
| 110 |
<input name="email" type="email" value="[email protected]" /> |
| 111 |
<select name="title" value="mr"> |
| 112 |
<option>Select an option</option> |
| 113 |
<option value="mr" selected="selected">Mr.</option> |
| 114 |
<option value="mrs">Mrs.</option> |
| 115 |
</select> |
| 116 |
<select name="job"> |
| 117 |
<option selected="selected">Select an option</option> |
| 118 |
<option value="employer">Employer</option> |
| 119 |
<option value="employee">Employee</option> |
| 120 |
</select> |
| 121 |
<button type="submit">Submit</button> |
| 122 |
<button type="submit" disabled="disabled">Cancel</button> |
| 123 |
</form> |
| 124 |
<iframe srcdoc="<p>Iframe text content</p>"></iframe> |
| 125 |
</main> |
| 126 |
<footer> |
| 127 |
<em>Footer</em> |
| 128 |
<button type="button">Back to top</button> |
| 129 |
</footer> |
| 130 |
`; |
| 131 |
|
| 132 |
customElements.define( |
| 133 |
"hoot-test-shadow-root", |
| 134 |
class ShadowRoot extends HTMLElement { |
| 135 |
constructor() { |
| 136 |
super(); |
| 137 |
const shadow = this.attachShadow({ mode: "open" }); |
| 138 |
|
| 139 |
const p = document.createElement("p"); |
| 140 |
p.textContent = "Shadow content"; |
| 141 |
|
| 142 |
const input = document.createElement("input"); |
| 143 |
|
| 144 |
shadow.append(p, input); |
| 145 |
} |
| 146 |
} |
| 147 |
); |
| 148 |
|
| 149 |
describe.tags("ui"); |
| 150 |
describe(parseUrl(import.meta.url), () => { |
| 151 |
test("formatXml", () => { |
| 152 |
expect(formatXml("")).toBe(""); |
| 153 |
expect(formatXml("<input />")).toBe("<input/>"); |
| 154 |
expect( |
| 155 |
formatXml(/* xml */ ` |
| 156 |
<div> |
| 157 |
A |
| 158 |
</div> |
| 159 |
`) |
| 160 |
).toBe(`<div>\n A\n</div>`); |
| 161 |
expect(formatXml(/* xml */ `<div>A</div>`)).toBe(`<div>\n A\n</div>`); |
| 162 |
|
| 163 |
// Inline |
| 164 |
expect( |
| 165 |
formatXml( |
| 166 |
/* xml */ ` |
| 167 |
<div> |
| 168 |
A |
| 169 |
</div> |
| 170 |
`, |
| 171 |
{ keepInlineTextNodes: true } |
| 172 |
) |
| 173 |
).toBe(`<div>\n A\n</div>`); |
| 174 |
expect(formatXml(/* xml */ `<div>A</div>`, { keepInlineTextNodes: true })).toBe( |
| 175 |
`<div>A</div>` |
| 176 |
); |
| 177 |
}); |
| 178 |
|
| 179 |
test("getActiveElement", async () => { |
| 180 |
await mountForTest(/* xml */ `<iframe srcdoc="<input >"></iframe>`); |
| 181 |
|
| 182 |
expect(":iframe input").not.toBeFocused(); |
| 183 |
|
| 184 |
const input = $1(":iframe input"); |
| 185 |
await click(input); |
| 186 |
|
| 187 |
expect(":iframe input").toBeFocused(); |
| 188 |
expect(getActiveElement()).toBe(input); |
| 189 |
}); |
| 190 |
|
| 191 |
test("getActiveElement: shadow dom", async () => { |
| 192 |
await mountForTest(/* xml */ `<hoot-test-shadow-root />`); |
| 193 |
|
| 194 |
expect("hoot-test-shadow-root:shadow input").not.toBeFocused(); |
| 195 |
|
| 196 |
const input = $1("hoot-test-shadow-root:shadow input"); |
| 197 |
await click(input); |
| 198 |
|
| 199 |
expect("hoot-test-shadow-root:shadow input").toBeFocused(); |
| 200 |
expect(getActiveElement()).toBe(input); |
| 201 |
}); |
| 202 |
|
| 203 |
test("getFocusableElements", async () => { |
| 204 |
await mountForTest(/* xml */ ` |
| 205 |
<input class="input" /> |
| 206 |
<div class="div" tabindex="0">aaa</div> |
| 207 |
<span class="span" tabindex="-1">aaa</span> |
| 208 |
<button class="disabled-button" disabled="disabled">Disabled button</button> |
| 209 |
<button class="button" tabindex="1">Button</button> |
| 210 |
`); |
| 211 |
|
| 212 |
expect(getFocusableElements().map((el) => el.className)).toEqual([ |
| 213 |
"button", |
| 214 |
"span", |
| 215 |
"input", |
| 216 |
"div", |
| 217 |
]); |
| 218 |
|
| 219 |
expect(getFocusableElements({ tabbable: true }).map((el) => el.className)).toEqual([ |
| 220 |
"button", |
| 221 |
"input", |
| 222 |
"div", |
| 223 |
]); |
| 224 |
}); |
| 225 |
|
| 226 |
test("getNextFocusableElement", async () => { |
| 227 |
await mountForTest(/* xml */ ` |
| 228 |
<input class="input" /> |
| 229 |
<div class="div" tabindex="0">aaa</div> |
| 230 |
<button class="disabled-button" disabled="disabled">Disabled button</button> |
| 231 |
<button class="button" tabindex="1">Button</button> |
| 232 |
`); |
| 233 |
|
| 234 |
await click(".input"); |
| 235 |
|
| 236 |
expect(getNextFocusableElement()).toHaveClass("div"); |
| 237 |
}); |
| 238 |
|
| 239 |
test("getParentFrame", async () => { |
| 240 |
await mountForTest(/* xml */ ` |
| 241 |
<div class="root"></div> |
| 242 |
`); |
| 243 |
|
| 244 |
const parent = await makeIframe(document, $1(".root")); |
| 245 |
const child = await makeIframe(parent.contentDocument); |
| 246 |
|
| 247 |
const content = child.contentDocument.createElement("div"); |
| 248 |
child.contentDocument.body.appendChild(content); |
| 249 |
|
| 250 |
expect(getParentFrame(content)).toBe(child); |
| 251 |
expect(getParentFrame(child)).toBe(parent); |
| 252 |
expect(getParentFrame(parent)).toBe(null); |
| 253 |
}); |
| 254 |
|
| 255 |
test("getPreviousFocusableElement", async () => { |
| 256 |
await mountForTest(/* xml */ ` |
| 257 |
<input class="input" /> |
| 258 |
<div class="div" tabindex="0">aaa</div> |
| 259 |
<button class="disabled-button" disabled="disabled">Disabled button</button> |
| 260 |
<button class="button" tabindex="1">Button</button> |
| 261 |
`); |
| 262 |
|
| 263 |
await click(".input"); |
| 264 |
|
| 265 |
expect(getPreviousFocusableElement()).toHaveClass("button"); |
| 266 |
}); |
| 267 |
|
| 268 |
test("isEditable", async () => { |
| 269 |
expect(isEditable(document.createElement("input"))).toBe(true); |
| 270 |
expect(isEditable(document.createElement("textarea"))).toBe(true); |
| 271 |
expect(isEditable(document.createElement("select"))).toBe(false); |
| 272 |
|
| 273 |
const editableDiv = document.createElement("div"); |
| 274 |
expect(isEditable(editableDiv)).toBe(false); |
| 275 |
editableDiv.setAttribute("contenteditable", "true"); |
| 276 |
expect(isEditable(editableDiv)).toBe(false); // not supported |
| 277 |
}); |
| 278 |
|
| 279 |
test("isFocusable", async () => { |
| 280 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 281 |
|
| 282 |
expect(isFocusable("input:first")).toBe(true); |
| 283 |
expect(isFocusable("li:first")).toBe(false); |
| 284 |
}); |
| 285 |
|
| 286 |
test("isInDom", async () => { |
| 287 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 288 |
|
| 289 |
expect(isInDOM(document)).toBe(true); |
| 290 |
expect(isInDOM(document.body)).toBe(true); |
| 291 |
expect(isInDOM(document.head)).toBe(true); |
| 292 |
expect(isInDOM(document.documentElement)).toBe(true); |
| 293 |
|
| 294 |
const form = $1`form`; |
| 295 |
expect(isInDOM(form)).toBe(true); |
| 296 |
|
| 297 |
form.remove(); |
| 298 |
|
| 299 |
expect(isInDOM(form)).toBe(false); |
| 300 |
|
| 301 |
const paragraph = $1`:iframe p`; |
| 302 |
expect(isInDOM(paragraph)).toBe(true); |
| 303 |
|
| 304 |
paragraph.remove(); |
| 305 |
|
| 306 |
expect(isInDOM(paragraph)).toBe(false); |
| 307 |
}); |
| 308 |
|
| 309 |
test("isDisplayed", async () => { |
| 310 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 311 |
|
| 312 |
expect(isDisplayed(document)).toBe(true); |
| 313 |
expect(isDisplayed(document.body)).toBe(true); |
| 314 |
expect(isDisplayed(document.head)).toBe(true); |
| 315 |
expect(isDisplayed(document.documentElement)).toBe(true); |
| 316 |
expect(isDisplayed("form")).toBe(true); |
| 317 |
|
| 318 |
expect(isDisplayed(".hidden")).toBe(false); |
| 319 |
expect(isDisplayed("body")).toBe(false); // not available from fixture |
| 320 |
}); |
| 321 |
|
| 322 |
test("isVisible", async () => { |
| 323 |
await mountForTest(FULL_HTML_TEMPLATE + "<hoot-test-shadow-root />"); |
| 324 |
|
| 325 |
expect(isVisible(document)).toBe(true); |
| 326 |
expect(isVisible(document.body)).toBe(true); |
| 327 |
expect(isVisible(document.head)).toBe(false); |
| 328 |
expect(isVisible(document.documentElement)).toBe(true); |
| 329 |
expect(isVisible("form")).toBe(true); |
| 330 |
expect(isVisible("hoot-test-shadow-root:shadow input")).toBe(true); |
| 331 |
|
| 332 |
expect(isVisible(".hidden")).toBe(false); |
| 333 |
expect(isVisible("body")).toBe(false); // not available from fixture |
| 334 |
}); |
| 335 |
|
| 336 |
test("matchMedia", async () => { |
| 337 |
// Invalid syntax |
| 338 |
expect(matchMedia("aaaa").matches).toBe(false); |
| 339 |
expect(matchMedia("display-mode: browser").matches).toBe(false); |
| 340 |
|
| 341 |
// Does not exist |
| 342 |
expect(matchMedia("(a)").matches).toBe(false); |
| 343 |
expect(matchMedia("(a: b)").matches).toBe(false); |
| 344 |
|
| 345 |
// Defaults |
| 346 |
expect(matchMedia("(display-mode:browser)").matches).toBe(true); |
| 347 |
expect(matchMedia("(display-mode: standalone)").matches).toBe(false); |
| 348 |
expect(matchMedia("not (display-mode: standalone)").matches).toBe(true); |
| 349 |
expect(matchMedia("(prefers-color-scheme :light)").matches).toBe(true); |
| 350 |
expect(matchMedia("(prefers-color-scheme : dark)").matches).toBe(false); |
| 351 |
expect(matchMedia("not (prefers-color-scheme: dark)").matches).toBe(true); |
| 352 |
expect(matchMedia("(prefers-reduced-motion: reduce)").matches).toBe(true); |
| 353 |
expect(matchMedia("(prefers-reduced-motion: no-preference)").matches).toBe(false); |
| 354 |
|
| 355 |
// Touch feature |
| 356 |
expect(window.matchMedia("(pointer: coarse)").matches).toBe(false); |
| 357 |
expect(window.ontouchstart).toBe(undefined); |
| 358 |
|
| 359 |
mockTouch(true); |
| 360 |
|
| 361 |
expect(window.matchMedia("(pointer: coarse)").matches).toBe(true); |
| 362 |
expect(window.ontouchstart).not.toBe(undefined); |
| 363 |
}); |
| 364 |
|
| 365 |
test("waitFor: already in fixture", async () => { |
| 366 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 367 |
|
| 368 |
waitFor(".title").then((el) => { |
| 369 |
expect.step(el.className); |
| 370 |
return el; |
| 371 |
}); |
| 372 |
|
| 373 |
expect.verifySteps([]); |
| 374 |
|
| 375 |
await animationFrame(); |
| 376 |
|
| 377 |
expect.verifySteps(["title"]); |
| 378 |
}); |
| 379 |
|
| 380 |
test("waitFor: rejects", async () => { |
| 381 |
await expect(waitFor("never", { timeout: 1 })).rejects.toThrow( |
| 382 |
`expected at least 1 element after 1ms and found 0 elements: 0 matching "never"` |
| 383 |
); |
| 384 |
}); |
| 385 |
|
| 386 |
test("waitFor: add new element", async () => { |
| 387 |
const el1 = document.createElement("div"); |
| 388 |
el1.className = "new-element"; |
| 389 |
|
| 390 |
const el2 = document.createElement("div"); |
| 391 |
el2.className = "new-element"; |
| 392 |
|
| 393 |
const promise = waitFor(".new-element").then((el) => { |
| 394 |
expect.step(el.className); |
| 395 |
return el; |
| 396 |
}); |
| 397 |
|
| 398 |
await animationFrame(); |
| 399 |
|
| 400 |
expect.verifySteps([]); |
| 401 |
|
| 402 |
getFixture().append(el1, el2); |
| 403 |
|
| 404 |
await expect(promise).resolves.toBe(el1); |
| 405 |
|
| 406 |
expect.verifySteps(["new-element"]); |
| 407 |
}); |
| 408 |
|
| 409 |
test("waitForNone: DOM empty", async () => { |
| 410 |
waitForNone(".title").then(() => expect.step("none")); |
| 411 |
expect.verifySteps([]); |
| 412 |
|
| 413 |
await animationFrame(); |
| 414 |
|
| 415 |
expect.verifySteps(["none"]); |
| 416 |
}); |
| 417 |
|
| 418 |
test("waitForNone: rejects", async () => { |
| 419 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 420 |
|
| 421 |
await expect(waitForNone(".title", { timeout: 1 })).rejects.toThrow(); |
| 422 |
}); |
| 423 |
|
| 424 |
test("waitForNone: delete elements", async () => { |
| 425 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 426 |
|
| 427 |
waitForNone(".title").then(() => expect.step("none")); |
| 428 |
expect(".title").toHaveCount(3); |
| 429 |
|
| 430 |
for (const title of $$(".title")) { |
| 431 |
expect.verifySteps([]); |
| 432 |
|
| 433 |
title.remove(); |
| 434 |
|
| 435 |
await animationFrame(); |
| 436 |
} |
| 437 |
|
| 438 |
expect.verifySteps(["none"]); |
| 439 |
}); |
| 440 |
|
| 441 |
describe("query", () => { |
| 442 |
test("native selectors", async () => { |
| 443 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 444 |
|
| 445 |
expect($$()).toEqual([]); |
| 446 |
for (const selector of [ |
| 447 |
"main", |
| 448 |
`.${"title"}`, |
| 449 |
`${"ul"}${" "}${`${"li"}`}`, |
| 450 |
".title", |
| 451 |
"ul > li", |
| 452 |
"form:has(.title:not(.haha)):not(.huhu) input[name='email']:enabled", |
| 453 |
"[colspan='1']", |
| 454 |
]) { |
| 455 |
expectSelector(selector).toEqualNodes(selector); |
| 456 |
} |
| 457 |
}); |
| 458 |
|
| 459 |
test("custom pseudo-classes", async () => { |
| 460 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 461 |
|
| 462 |
// :first, :last, :only & :eq |
| 463 |
expectSelector(".title:first").toEqualNodes(".title", { index: 0 }); |
| 464 |
expectSelector(".title:last").toEqualNodes(".title", { index: -1 }); |
| 465 |
expectSelector(".title:eq(-1)").toEqualNodes(".title", { index: -1 }); |
| 466 |
expectSelector("main:only").toEqualNodes("main"); |
| 467 |
expectSelector(".title:only").toEqualNodes(""); |
| 468 |
expectSelector(".title:eq(1)").toEqualNodes(".title", { index: 1 }); |
| 469 |
expectSelector(".title:eq('1')").toEqualNodes(".title", { index: 1 }); |
| 470 |
expectSelector('.title:eq("1")').toEqualNodes(".title", { index: 1 }); |
| 471 |
|
| 472 |
// :contains |
| 473 |
expectSelector("main > .text:contains(ipsum)").toEqualNodes("p"); |
| 474 |
expectSelector(".text:contains(/\\bL\\w+\\b\\sipsum/)").toEqualNodes("p"); |
| 475 |
expectSelector(".text:contains(item)").toEqualNodes("li"); |
| 476 |
|
| 477 |
// :text |
| 478 |
expectSelector(".text:text(item)").toEqualNodes(""); |
| 479 |
expectSelector(".text:text(first item)").toEqualNodes("li:first-of-type"); |
| 480 |
|
| 481 |
// :value |
| 482 |
expectSelector("input:value(john)").toEqualNodes("[name=name],[name=email]"); |
| 483 |
expectSelector("input:value(john doe)").toEqualNodes("[name=name]"); |
| 484 |
expectSelector("input:value('John Doe (JOD)')").toEqualNodes("[name=name]"); |
| 485 |
expectSelector(`input:value("(JOD)")`).toEqualNodes("[name=name]"); |
| 486 |
expectSelector("input:value(johndoe)").toEqualNodes("[name=email]"); |
| 487 |
expectSelector("select:value(mr)").toEqualNodes("[name=title]"); |
| 488 |
expectSelector("select:value(unknown value)").toEqualNodes(""); |
| 489 |
|
| 490 |
// :selected |
| 491 |
expectSelector("option:selected").toEqualNodes( |
| 492 |
"select[name=title] option[value=mr],select[name=job] option:first-child" |
| 493 |
); |
| 494 |
|
| 495 |
// :iframe |
| 496 |
expectSelector("iframe p:contains(iframe text content)").toEqualNodes(""); |
| 497 |
expectSelector("div:iframe p").toEqualNodes(""); |
| 498 |
expectSelector(":iframe p:contains(iframe text content)").toEqualNodes("p", { |
| 499 |
root: "iframe", |
| 500 |
}); |
| 501 |
}); |
| 502 |
|
| 503 |
test("query options", async () => { |
| 504 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 505 |
|
| 506 |
expect($$("input", { count: 2 })).toHaveLength(2); |
| 507 |
expect(() => $$("input", { count: 1 })).toThrow(); |
| 508 |
|
| 509 |
expect($$("option", { count: 6 })).toHaveLength(6); |
| 510 |
expect($$("option", { count: 3, root: "[name=title]" })).toHaveLength(3); |
| 511 |
expect(() => $$("option", { count: 6, root: "[name=title]" })).toThrow(); |
| 512 |
}); |
| 513 |
|
| 514 |
test("advanced use cases", async () => { |
| 515 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 516 |
|
| 517 |
// Comma-separated selectors |
| 518 |
expectSelector(":has(form:contains('Form title')),p:contains(ipsum)").toEqualNodes( |
| 519 |
"p,main" |
| 520 |
); |
| 521 |
|
| 522 |
// :has & :not combinations with custom pseudo-classes |
| 523 |
expectSelector(`select:has(:contains(Employer))`).toEqualNodes("select[name=job]"); |
| 524 |
expectSelector(`select:not(:has(:contains(Employer)))`).toEqualNodes( |
| 525 |
"select[name=title]" |
| 526 |
); |
| 527 |
expectSelector( |
| 528 |
`main:first-of-type:not(:has(:contains(This text does not exist))):contains('List header') > form:has([name="name"]):contains("Form title"):nth-child(6).overflow-auto:visible select[name=job] option:selected` |
| 529 |
).toEqualNodes("select[name=job] option:first-child"); |
| 530 |
|
| 531 |
// :contains & commas |
| 532 |
expectSelector(`p:contains(velit,)`).toEqualNodes("p"); |
| 533 |
expectSelector(`p:contains('velit,')`).toEqualNodes("p"); |
| 534 |
expectSelector(`p:contains(", tristique")`).toEqualNodes("p"); |
| 535 |
expectSelector(`p:contains(/\\bvelit,/)`).toEqualNodes("p"); |
| 536 |
}); |
| 537 |
|
| 538 |
// Whatever, at this point I'm just copying failing selectors and creating |
| 539 |
// fake contexts accordingly as I'm fixing them. |
| 540 |
|
| 541 |
test("comma-separated long selector: no match", async () => { |
| 542 |
await mountForTest(/* xml */ ` |
| 543 |
<div class="o_we_customize_panel"> |
| 544 |
<we-customizeblock-option class="snippet-option-ImageTools"> |
| 545 |
<div class="o_we_so_color_palette o_we_widget_opened"> |
| 546 |
idk |
| 547 |
</div> |
| 548 |
<we-select data-name="shape_img_opt"> |
| 549 |
<we-toggler></we-toggler> |
| 550 |
</we-select> |
| 551 |
</we-customizeblock-option> |
| 552 |
</div> |
| 553 |
`); |
| 554 |
expectSelector( |
| 555 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] we-select[data-name="shape_img_opt"] we-toggler`, |
| 556 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] [title='we-select[data-name="shape_img_opt"] we-toggler']` |
| 557 |
).toEqualNodes(""); |
| 558 |
}); |
| 559 |
|
| 560 |
test("comma-separated long selector: match first", async () => { |
| 561 |
await mountForTest(/* xml */ ` |
| 562 |
<div class="o_we_customize_panel"> |
| 563 |
<we-customizeblock-option class="snippet-option-ImageTools"> |
| 564 |
<we-select data-name="shape_img_opt"> |
| 565 |
<we-toggler></we-toggler> |
| 566 |
</we-select> |
| 567 |
</we-customizeblock-option> |
| 568 |
</div> |
| 569 |
`); |
| 570 |
expectSelector( |
| 571 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] we-select[data-name="shape_img_opt"] we-toggler`, |
| 572 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] [title='we-select[data-name="shape_img_opt"] we-toggler']` |
| 573 |
).toEqualNodes("we-toggler"); |
| 574 |
}); |
| 575 |
|
| 576 |
test("comma-separated long selector: match second", async () => { |
| 577 |
await mountForTest(/* xml */ ` |
| 578 |
<div class="o_we_customize_panel"> |
| 579 |
<we-customizeblock-option class="snippet-option-ImageTools"> |
| 580 |
<div title='we-select[data-name="shape_img_opt"] we-toggler'> |
| 581 |
idk |
| 582 |
</div> |
| 583 |
</we-customizeblock-option> |
| 584 |
</div> |
| 585 |
`); |
| 586 |
expectSelector( |
| 587 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] we-select[data-name="shape_img_opt"] we-toggler`, |
| 588 |
`.o_we_customize_panel:not(:has(.o_we_so_color_palette.o_we_widget_opened)) we-customizeblock-option[class='snippet-option-ImageTools'] [title='we-select[data-name="shape_img_opt"] we-toggler']` |
| 589 |
).toEqualNodes("div[title]"); |
| 590 |
}); |
| 591 |
|
| 592 |
test("comma-separated :contains", async () => { |
| 593 |
await mountForTest(/* xml */ ` |
| 594 |
<div class="o_menu_sections"> |
| 595 |
<a class="dropdown-item">Products</a> |
| 596 |
</div> |
| 597 |
<nav class="o_burger_menu_content"> |
| 598 |
<ul> |
| 599 |
<li data-menu-xmlid="sale.menu_product_template_action"> |
| 600 |
Products |
| 601 |
</li> |
| 602 |
</ul> |
| 603 |
</nav> |
| 604 |
`); |
| 605 |
expectSelector( |
| 606 |
`.o_menu_sections .dropdown-item:contains('Products'), nav.o_burger_menu_content li[data-menu-xmlid='sale.menu_product_template_action']` |
| 607 |
).toEqualNodes(".dropdown-item,li"); |
| 608 |
}); |
| 609 |
|
| 610 |
test(":contains with line return", async () => { |
| 611 |
await mountForTest(/* xml */ ` |
| 612 |
<span> |
| 613 |
<div>Matrix (PAV11, PAV22, PAV31)</div> |
| 614 |
<div>PA4: PAV41</div> |
| 615 |
</span> |
| 616 |
`); |
| 617 |
expectSelector(`span:contains("Matrix (PAV11, PAV22, PAV31) PA4: PAV41")`).toEqualNodes( |
| 618 |
"span" |
| 619 |
); |
| 620 |
}); |
| 621 |
|
| 622 |
test(":has(...):first", async () => { |
| 623 |
await mountForTest(/* xml */ ` |
| 624 |
<a href="/web/event/1"></a> |
| 625 |
<a target="" href="/web/event/2"> |
| 626 |
<span>Conference for Architects TEST</span> |
| 627 |
</a> |
| 628 |
`); |
| 629 |
|
| 630 |
expectSelector( |
| 631 |
`a[href*="/event"]:contains("Conference for Architects TEST")` |
| 632 |
).toEqualNodes("[target]"); |
| 633 |
expectSelector( |
| 634 |
`a[href*="/event"]:contains("Conference for Architects TEST"):first` |
| 635 |
).toEqualNodes("[target]"); |
| 636 |
}); |
| 637 |
|
| 638 |
test(":eq", async () => { |
| 639 |
await mountForTest(/* xml */ ` |
| 640 |
<ul> |
| 641 |
<li>a</li> |
| 642 |
<li>b</li> |
| 643 |
<li>c</li> |
| 644 |
</ul> |
| 645 |
`); |
| 646 |
|
| 647 |
expectSelector(`li:first:contains(a)`).toEqualNodes("li:nth-child(1)"); |
| 648 |
expectSelector(`li:contains(a):first`).toEqualNodes("li:nth-child(1)"); |
| 649 |
expectSelector(`li:first:contains(b)`).toEqualNodes(""); |
| 650 |
expectSelector(`li:contains(b):first`).toEqualNodes("li:nth-child(2)"); |
| 651 |
}); |
| 652 |
|
| 653 |
test(":empty", async () => { |
| 654 |
await mountForTest(/* xml */ ` |
| 655 |
<input class="empty" /> |
| 656 |
<input class="value" value="value" /> |
| 657 |
`); |
| 658 |
|
| 659 |
expectSelector(`input:empty`).toEqualNodes(".empty"); |
| 660 |
expectSelector(`input:not(:empty)`).toEqualNodes(".value"); |
| 661 |
}); |
| 662 |
|
| 663 |
test("regular :contains", async () => { |
| 664 |
await mountForTest(/* xml */ ` |
| 665 |
<div class="website_links_click_chart"> |
| 666 |
<div class="title"> |
| 667 |
0 clicks |
| 668 |
</div> |
| 669 |
<div class="title"> |
| 670 |
1 clicks |
| 671 |
</div> |
| 672 |
<div class="title"> |
| 673 |
2 clicks |
| 674 |
</div> |
| 675 |
</div> |
| 676 |
`); |
| 677 |
|
| 678 |
expectSelector(`.website_links_click_chart .title:contains("1 clicks")`).toEqualNodes( |
| 679 |
".title:nth-child(2)" |
| 680 |
); |
| 681 |
}); |
| 682 |
|
| 683 |
test("other regular :contains", async () => { |
| 684 |
await mountForTest(/* xml */ ` |
| 685 |
<ul |
| 686 |
class="o-autocomplete--dropdown-menu ui-widget show dropdown-menu ui-autocomplete" |
| 687 |
style="position: fixed; top: 283.75px; left: 168.938px" |
| 688 |
> |
| 689 |
<li class="o-autocomplete--dropdown-item ui-menu-item block"> |
| 690 |
<a |
| 691 |
href="#" |
| 692 |
class="dropdown-item ui-menu-item-wrapper truncate ui-state-active" |
| 693 |
>Account Tax Group Partner</a |
| 694 |
> |
| 695 |
</li> |
| 696 |
<li |
| 697 |
class="o-autocomplete--dropdown-item ui-menu-item block o_m2o_dropdown_option o_m2o_dropdown_option_search_more" |
| 698 |
> |
| 699 |
<a href="#" class="dropdown-item ui-menu-item-wrapper truncate" |
| 700 |
>Search More...</a |
| 701 |
> |
| 702 |
</li> |
| 703 |
<li |
| 704 |
class="o-autocomplete--dropdown-item ui-menu-item block o_m2o_dropdown_option o_m2o_dropdown_option_create_edit" |
| 705 |
> |
| 706 |
<a href="#" class="dropdown-item ui-menu-item-wrapper truncate" |
| 707 |
>Create and edit...</a |
| 708 |
> |
| 709 |
</li> |
| 710 |
</ul> |
| 711 |
`); |
| 712 |
|
| 713 |
expectSelector(`.ui-menu-item a:contains("Account Tax Group Partner")`).toEqualNodes( |
| 714 |
"ul li:first-child a" |
| 715 |
); |
| 716 |
}); |
| 717 |
|
| 718 |
test(":iframe", async () => { |
| 719 |
await mountForTest(/* xml */ ` |
| 720 |
<iframe srcdoc="<p>Iframe text content</p>"></iframe> |
| 721 |
`); |
| 722 |
|
| 723 |
expectSelector(`:iframe html`).toEqualNodes("html", { root: "iframe" }); |
| 724 |
expectSelector(`:iframe body`).toEqualNodes("body", { root: "iframe" }); |
| 725 |
expectSelector(`:iframe head`).toEqualNodes("head", { root: "iframe" }); |
| 726 |
}); |
| 727 |
|
| 728 |
test(":contains with brackets", async () => { |
| 729 |
await mountForTest(/* xml */ ` |
| 730 |
<div class="o_content"> |
| 731 |
<div class="o_field_widget" name="messages"> |
| 732 |
<table class="o_list_view table table-sm table-hover table-striped o_list_view_ungrouped"> |
| 733 |
<tbody> |
| 734 |
<tr class="o_data_row"> |
| 735 |
<td class="o_list_record_selector"> |
| 736 |
bbb |
| 737 |
</td> |
| 738 |
<td class="o_data_cell o_required_modifier"> |
| 739 |
<span> |
| 740 |
[test_trigger] Mitchell Admin |
| 741 |
</span> |
| 742 |
</td> |
| 743 |
</tr> |
| 744 |
</tbody> |
| 745 |
</table> |
| 746 |
</div> |
| 747 |
</div> |
| 748 |
`); |
| 749 |
|
| 750 |
expectSelector( |
| 751 |
`.o_content:has(.o_field_widget[name=messages]):has(td:text(bbb)):has(td:contains(/^\\[test_trigger\\] Mitchell Admin/))` |
| 752 |
).toEqualNodes(".o_content"); |
| 753 |
}); |
| 754 |
|
| 755 |
test(":eq in the middle of a selector", async () => { |
| 756 |
await mountForTest(/* xml */ ` |
| 757 |
<ul> |
| 758 |
<li class="oe_overlay o_draggable"></li> |
| 759 |
<li class="oe_overlay o_draggable"></li> |
| 760 |
<li class="oe_overlay o_draggable oe_active"></li> |
| 761 |
<li class="oe_overlay o_draggable"></li> |
| 762 |
</ul> |
| 763 |
`); |
| 764 |
expectSelector(`.oe_overlay.o_draggable:eq(2).oe_active`).toEqualNodes( |
| 765 |
"li:nth-child(3)" |
| 766 |
); |
| 767 |
}); |
| 768 |
|
| 769 |
test("combinator +", async () => { |
| 770 |
await mountForTest(/* xml */ ` |
| 771 |
<form class="js_attributes"> |
| 772 |
<input type="checkbox" /> |
| 773 |
<label>Steel - Test</label> |
| 774 |
</form> |
| 775 |
`); |
| 776 |
|
| 777 |
expectSelector( |
| 778 |
`form.js_attributes input:not(:checked) + label:contains(Steel - Test)` |
| 779 |
).toEqualNodes("label"); |
| 780 |
}); |
| 781 |
|
| 782 |
test("multiple + combinators", async () => { |
| 783 |
await mountForTest(/* xml */ ` |
| 784 |
<div class="s_cover"> |
| 785 |
<span class="o_text_highlight"> |
| 786 |
<span class="o_text_highlight_item"> |
| 787 |
<span class="o_text_highlight_path_underline" /> |
| 788 |
</span> |
| 789 |
<br /> |
| 790 |
<span class="o_text_highlight_item"> |
| 791 |
<span class="o_text_highlight_path_underline" /> |
| 792 |
</span> |
| 793 |
</span> |
| 794 |
</div> |
| 795 |
`); |
| 796 |
|
| 797 |
expectSelector(` |
| 798 |
.s_cover span.o_text_highlight:has( |
| 799 |
.o_text_highlight_item |
| 800 |
+ br |
| 801 |
+ .o_text_highlight_item |
| 802 |
) |
| 803 |
`).toEqualNodes(".o_text_highlight"); |
| 804 |
}); |
| 805 |
|
| 806 |
test(":last", async () => { |
| 807 |
await mountForTest(/* xml */ ` |
| 808 |
<div class="o_field_widget" name="messages"> |
| 809 |
<table class="o_list_view table table-sm table-hover table-striped o_list_view_ungrouped"> |
| 810 |
<tbody> |
| 811 |
<tr class="o_data_row"> |
| 812 |
<td class="o_list_record_remove"> |
| 813 |
<button class="btn">Remove</button> |
| 814 |
</td> |
| 815 |
</tr> |
| 816 |
<tr class="o_data_row"> |
| 817 |
<td class="o_list_record_remove"> |
| 818 |
<button class="btn">Remove</button> |
| 819 |
</td> |
| 820 |
</tr> |
| 821 |
</tbody> |
| 822 |
</table> |
| 823 |
</div> |
| 824 |
`); |
| 825 |
expectSelector( |
| 826 |
`.o_field_widget[name=messages] .o_data_row td.o_list_record_remove button:visible:last` |
| 827 |
).toEqualNodes(".o_data_row:last-child button"); |
| 828 |
}); |
| 829 |
|
| 830 |
test("select :contains & :value", async () => { |
| 831 |
await mountForTest(/* xml */ ` |
| 832 |
<select class="configurator_select form-select form-select-lg"> |
| 833 |
<option value="217" selected="">Metal</option> |
| 834 |
<option value="218">Wood</option> |
| 835 |
</select> |
| 836 |
`); |
| 837 |
expectSelector(`.configurator_select:has(option:contains(Metal))`).toEqualNodes( |
| 838 |
"select" |
| 839 |
); |
| 840 |
expectSelector(`.configurator_select:has(option:value(217))`).toEqualNodes("select"); |
| 841 |
expectSelector(`.configurator_select:has(option:value(218))`).toEqualNodes("select"); |
| 842 |
expectSelector(`.configurator_select:value(217)`).toEqualNodes("select"); |
| 843 |
expectSelector(`.configurator_select:value(218)`).toEqualNodes(""); |
| 844 |
expectSelector(`.configurator_select:value(Metal)`).toEqualNodes(""); |
| 845 |
}); |
| 846 |
|
| 847 |
test("invalid selectors", async () => { |
| 848 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 849 |
|
| 850 |
expect(() => $$`[colspan=1]`).toThrow(); // missing quotes |
| 851 |
expect(() => $$`[href=/]`).toThrow(); // missing quotes |
| 852 |
expect( |
| 853 |
() => |
| 854 |
$$`_o_wblog_posts_loop:has(span:has(i.fa-calendar-o):has(a[href="/blog?search=a"])):has(span:has(i.fa-search):has(a[href^="/blog?date_begin"]))` |
| 855 |
).toThrow(); // nested :has statements |
| 856 |
}); |
| 857 |
|
| 858 |
test("queryAllRects", async () => { |
| 859 |
await mountForTest(/* xml */ ` |
| 860 |
<div style="width: 40px; height: 60px;" /> |
| 861 |
<div style="width: 20px; height: 10px;" /> |
| 862 |
`); |
| 863 |
|
| 864 |
expect(queryAllRects("div")).toEqual($$("div").map((el) => el.getBoundingClientRect())); |
| 865 |
expect(queryAllRects("div:first")).toEqual([new DOMRect({ width: 40, height: 60 })]); |
| 866 |
expect(queryAllRects("div:last")).toEqual([new DOMRect({ width: 20, height: 10 })]); |
| 867 |
}); |
| 868 |
|
| 869 |
test("queryAllTexts", async () => { |
| 870 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 871 |
|
| 872 |
expect(queryAllTexts(".title")).toEqual(["Title", "List header", "Form title"]); |
| 873 |
expect(queryAllTexts("footer")).toEqual(["FooterBack to top"]); |
| 874 |
}); |
| 875 |
|
| 876 |
test("queryOne", async () => { |
| 877 |
await mountForTest(FULL_HTML_TEMPLATE); |
| 878 |
|
| 879 |
expect($1(".title:first")).toBe(getFixture().querySelector("header .title")); |
| 880 |
|
| 881 |
expect(() => $1(".title")).toThrow(); |
| 882 |
expect(() => $1(".title", { count: 2 })).toThrow(); |
| 883 |
}); |
| 884 |
|
| 885 |
test("queryRect", async () => { |
| 886 |
await mountForTest(/* xml */ ` |
| 887 |
<div class="container"> |
| 888 |
<div class="rect" style="width: 40px; height: 60px;" /> |
| 889 |
</div> |
| 890 |
`); |
| 891 |
|
| 892 |
expect(".rect").toHaveRect(".container"); // same rect as parent |
| 893 |
expect(".rect").toHaveRect({ width: 40, height: 60 }); |
| 894 |
expect(queryRect(".rect")).toEqual($1(".rect").getBoundingClientRect()); |
| 895 |
expect(queryRect(".rect")).toEqual(new DOMRect({ width: 40, height: 60 })); |
| 896 |
}); |
| 897 |
|
| 898 |
test("queryRect with trimPadding", async () => { |
| 899 |
await mountForTest(/* xml */ ` |
| 900 |
<div style="width: 50px; height: 70px; padding: 5px; margin: 6px" /> |
| 901 |
`); |
| 902 |
|
| 903 |
expect("div").toHaveRect({ width: 50, height: 70 }); // with padding |
| 904 |
expect("div").toHaveRect({ width: 40, height: 60 }, { trimPadding: true }); |
| 905 |
}); |
| 906 |
|
| 907 |
test("not found messages", async () => { |
| 908 |
await mountForTest(/* xml */ ` |
| 909 |
<div class="tralalero"> |
| 910 |
Tralala |
| 911 |
</div> |
| 912 |
`); |
| 913 |
|
| 914 |
expect(() => $("invalid:pseudo-selector")).toThrow(); |
| 915 |
// Perform in-between valid query with custom pseudo selectors |
| 916 |
expect($`.modal:visible:contains('Tung Tung Tung Sahur')`).toBe(null); |
| 917 |
|
| 918 |
// queryOne error messages |
| 919 |
expect(() => $1()).toThrow(`found 0 elements instead of 1`); |
| 920 |
expect(() => $$([], { count: 18 })).toThrow(`found 0 elements instead of 18`); |
| 921 |
expect(() => $1("")).toThrow(`found 0 elements instead of 1: 0 matching ""`); |
| 922 |
expect(() => $$(".tralalero", { count: -20 })).toThrow( |
| 923 |
`invalid 'count' option: should be a positive integer` |
| 924 |
); |
| 925 |
expect(() => $1`.tralalero:contains(Tralala):visible:scrollable:first`).toThrow( |
| 926 |
`found 0 elements instead of 1: 0 matching ".tralalero:contains(Tralala):visible:scrollable:first" (1 element with text "Tralala" > 1 visible element > 0 scrollable elements)` |
| 927 |
); |
| 928 |
expect(() => |
| 929 |
$1(".tralalero", { |
| 930 |
contains: "Tralala", |
| 931 |
visible: true, |
| 932 |
scrollable: true, |
| 933 |
first: true, |
| 934 |
}) |
| 935 |
).toThrow( |
| 936 |
`found 0 elements instead of 1: 1 matching ".tralalero", including 1 element with text "Tralala", including 1 visible element, including 0 scrollable elements` |
| 937 |
); |
| 938 |
}); |
| 939 |
}); |
| 940 |
}); |