| 1 |
import { after, before, expect, test } from "@odoo/hoot"; |
| 2 |
import { xml } from "@odoo/owl"; |
| 3 |
import { contains, onRpc } from "@web/../tests/web_test_helpers"; |
| 4 |
import { |
| 5 |
addOption, |
| 6 |
defineWebsiteModels, |
| 7 |
setupWebsiteBuilder, |
| 8 |
} from "@website/../tests/builder/website_helpers"; |
| 9 |
|
| 10 |
defineWebsiteModels(); |
| 11 |
|
| 12 |
let originalWindowOpen; |
| 13 |
function mockWindowOpen() { |
| 14 |
originalWindowOpen = window.open; |
| 15 |
window.open = (...args) => { |
| 16 |
expect.step(`callWindowOpen ${args[0]}`); |
| 17 |
}; |
| 18 |
} |
| 19 |
function unmockWindowOpen() { |
| 20 |
window.open = originalWindowOpen; |
| 21 |
} |
| 22 |
function mockGetSuggestedLinks(callback = undefined) { |
| 23 |
onRpc("/website/get_suggested_links", () => { |
| 24 |
callback?.(); |
| 25 |
return { |
| 26 |
matching_pages: [ |
| 27 |
{ |
| 28 |
value: "/page1", |
| 29 |
label: "/page1 (Page 1)", |
| 30 |
}, |
| 31 |
{ |
| 32 |
value: "/page2", |
| 33 |
label: "/page2 (Page 2)", |
| 34 |
}, |
| 35 |
], |
| 36 |
others: [ |
| 37 |
{ |
| 38 |
title: "Last modified pages", |
| 39 |
values: [ |
| 40 |
{ |
| 41 |
value: "/page3", |
| 42 |
label: "/page3 (Page 3)", |
| 43 |
}, |
| 44 |
], |
| 45 |
}, |
| 46 |
{ |
| 47 |
title: "Apps url", |
| 48 |
values: [ |
| 49 |
{ |
| 50 |
value: "/app1", |
| 51 |
label: "/app1 (App 1)", |
| 52 |
icon: "app1_icon", |
| 53 |
}, |
| 54 |
], |
| 55 |
}, |
| 56 |
], |
| 57 |
}; |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
before(() => { |
| 62 |
mockWindowOpen(); |
| 63 |
}); |
| 64 |
after(() => { |
| 65 |
unmockWindowOpen(); |
| 66 |
}); |
| 67 |
|
| 68 |
test("link button opens in new window if url not empty", async () => { |
| 69 |
addOption({ |
| 70 |
selector: ".test-options-target", |
| 71 |
template: xml`<WebsiteUrlPicker dataAttributeAction="'url'"/>`, |
| 72 |
}); |
| 73 |
await setupWebsiteBuilder(`<div class="test-options-target">b</div>`); |
| 74 |
await contains(":iframe .test-options-target").click(); |
| 75 |
|
| 76 |
await contains(".we-bg-options-container button").click(); |
| 77 |
|
| 78 |
await contains(".we-bg-options-container input").edit("/url"); |
| 79 |
await contains(".we-bg-options-container button").click(); |
| 80 |
expect.verifySteps(["callWindowOpen /url"]); |
| 81 |
|
| 82 |
await contains(".we-bg-options-container input").edit(""); |
| 83 |
await contains(".we-bg-options-container button").click(); |
| 84 |
}); |
| 85 |
|
| 86 |
test("opens dropdown when typing /", async () => { |
| 87 |
mockGetSuggestedLinks(() => { |
| 88 |
expect.step("button_immediate_install"); |
| 89 |
}); |
| 90 |
addOption({ |
| 91 |
selector: ".test-options-target", |
| 92 |
template: xml`<WebsiteUrlPicker dataAttributeAction="'url'"/>`, |
| 93 |
}); |
| 94 |
await setupWebsiteBuilder(`<div class="test-options-target">b</div>`); |
| 95 |
await contains(":iframe .test-options-target").click(); |
| 96 |
|
| 97 |
await contains(".we-bg-options-container input").edit("/"); |
| 98 |
await contains(".we-bg-options-container input").click(); |
| 99 |
expect.verifySteps(["button_immediate_install"]); |
| 100 |
expect(document.querySelector(".o_website_ui_autocomplete")).toBeVisible(); |
| 101 |
}); |
| 102 |
|
| 103 |
test("selects and commits value from dropdown", async () => { |
| 104 |
mockGetSuggestedLinks(); |
| 105 |
addOption({ |
| 106 |
selector: ".test-options-target", |
| 107 |
template: xml`<WebsiteUrlPicker dataAttributeAction="'url'"/>`, |
| 108 |
}); |
| 109 |
await setupWebsiteBuilder(`<div class="test-options-target">b</div>`); |
| 110 |
await contains(":iframe .test-options-target").click(); |
| 111 |
|
| 112 |
await contains(".we-bg-options-container input").edit("/"); |
| 113 |
await contains(".we-bg-options-container input").click(); |
| 114 |
await contains(document.querySelector(".o_website_ui_autocomplete > li:first-child a")).click(); |
| 115 |
expect(document.querySelector(".o_website_ui_autocomplete")).toBe(null); |
| 116 |
expect(".we-bg-options-container input").toHaveValue("/page1"); |
| 117 |
expect(":iframe .test-options-target").toHaveAttribute("data-url", "/page1"); |
| 118 |
}); |
| 119 |
|
| 120 |
test("collects anchors in current page and suggests them", async () => { |
| 121 |
mockGetSuggestedLinks(); |
| 122 |
addOption({ |
| 123 |
selector: ".test-options-target", |
| 124 |
template: xml`<WebsiteUrlPicker dataAttributeAction="'url'"/>`, |
| 125 |
}); |
| 126 |
await setupWebsiteBuilder(` |
| 127 |
<div class="test-options-target">b</div> |
| 128 |
<div id="anchor1" data-anchor="true">anchor1</div> |
| 129 |
<div id="anchor2" data-anchor="true">anchor2</div> |
| 130 |
`); |
| 131 |
await contains(":iframe .test-options-target").click(); |
| 132 |
await contains(".we-bg-options-container input").edit("#"); |
| 133 |
await contains(".we-bg-options-container input").click(); |
| 134 |
|
| 135 |
// Check autocomplete suggests both anchors |
| 136 |
const els = document.querySelectorAll(".o_website_ui_autocomplete > li a"); |
| 137 |
expect(els).toHaveLength(4); // Our anchors, #top and #bottom |
| 138 |
expect(els[1].innerText).toBe("#anchor1"); |
| 139 |
expect(els[2].innerText).toBe("#anchor2"); |
| 140 |
|
| 141 |
// Check clicking on one of them properly applies |
| 142 |
await contains(els[1]).click(); |
| 143 |
expect(".we-bg-options-container input").toHaveValue("#anchor1"); |
| 144 |
await expect(":iframe .test-options-target").toHaveAttribute("data-url", "#anchor1"); |
| 145 |
}); |