| 1 |
|
| 2 |
|
| 3 |
import { Component, xml } from "@odoo/owl"; |
| 4 |
import { makeTestEnv } from "@web/../tests/helpers/mock_env"; |
| 5 |
import { |
| 6 |
click, |
| 7 |
editInput, |
| 8 |
getFixture, |
| 9 |
mount, |
| 10 |
mouseEnter, |
| 11 |
triggerEvent, |
| 12 |
triggerEvents, |
| 13 |
} from "@web/../tests/helpers/utils"; |
| 14 |
import { commandService } from "@web/core/commands/command_service"; |
| 15 |
import { dialogService } from "@web/core/dialog/dialog_service"; |
| 16 |
import { fieldService } from "@web/core/field_service"; |
| 17 |
import { treeProcessorService } from "@web/core/tree_editor/tree_processor"; |
| 18 |
import { hotkeyService } from "@web/core/hotkeys/hotkey_service"; |
| 19 |
import { notificationService } from "@web/core/notifications/notification_service"; |
| 20 |
import { ormService } from "@web/core/orm_service"; |
| 21 |
import { popoverService } from "@web/core/popover/popover_service"; |
| 22 |
import { registry } from "@web/core/registry"; |
| 23 |
import { CustomFavoriteItem } from "@web/search/custom_favorite_item/custom_favorite_item"; |
| 24 |
import { WithSearch } from "@web/search/with_search/with_search"; |
| 25 |
import { getDefaultConfig } from "@web/views/view"; |
| 26 |
import { viewService } from "@web/views/view_service"; |
| 27 |
import { actionService } from "@web/webclient/actions/action_service"; |
| 28 |
import { MainComponentsContainer } from "@web/core/main_components_container"; |
| 29 |
import { nameService } from "@web/core/name_service"; |
| 30 |
import { datetimePickerService } from "@web/core/datetime/datetimepicker_service"; |
| 31 |
|
| 32 |
const serviceRegistry = registry.category("services"); |
| 33 |
const favoriteMenuRegistry = registry.category("favoriteMenu"); |
| 34 |
|
| 35 |
export function setupControlPanelServiceRegistry() { |
| 36 |
serviceRegistry.add("action", actionService); |
| 37 |
serviceRegistry.add("dialog", dialogService); |
| 38 |
serviceRegistry.add("field", fieldService); |
| 39 |
serviceRegistry.add("tree_processor", treeProcessorService); |
| 40 |
serviceRegistry.add("hotkey", hotkeyService); |
| 41 |
serviceRegistry.add("name", nameService); |
| 42 |
serviceRegistry.add("notification", notificationService); |
| 43 |
serviceRegistry.add("orm", ormService); |
| 44 |
serviceRegistry.add("popover", popoverService); |
| 45 |
serviceRegistry.add("view", viewService); |
| 46 |
serviceRegistry.add("command", commandService); |
| 47 |
serviceRegistry.add("datetime_picker", datetimePickerService); |
| 48 |
} |
| 49 |
|
| 50 |
export function setupControlPanelFavoriteMenuRegistry() { |
| 51 |
favoriteMenuRegistry.add( |
| 52 |
"custom-favorite-item", |
| 53 |
{ Component: CustomFavoriteItem, groupNumber: 3 }, |
| 54 |
{ sequence: 0 } |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
export async function makeWithSearch(params) { |
| 59 |
const props = { ...params }; |
| 60 |
|
| 61 |
const serverData = props.serverData || undefined; |
| 62 |
const mockRPC = props.mockRPC || undefined; |
| 63 |
const config = { |
| 64 |
...getDefaultConfig(), |
| 65 |
...props.config, |
| 66 |
}; |
| 67 |
|
| 68 |
delete props.serverData; |
| 69 |
delete props.mockRPC; |
| 70 |
delete props.config; |
| 71 |
const componentProps = props.componentProps || {}; |
| 72 |
delete props.componentProps; |
| 73 |
delete props.Component; |
| 74 |
|
| 75 |
class Parent extends Component { |
| 76 |
static template = xml` |
| 77 |
<WithSearch t-props="withSearchProps" t-slot-scope="search"> |
| 78 |
<Component t-props="getProps(search)"/> |
| 79 |
</WithSearch> |
| 80 |
<MainComponentsContainer /> |
| 81 |
`; |
| 82 |
static components = { Component: params.Component, WithSearch, MainComponentsContainer }; |
| 83 |
static props = ["*"]; |
| 84 |
setup() { |
| 85 |
this.withSearchProps = props; |
| 86 |
} |
| 87 |
|
| 88 |
getProps(search) { |
| 89 |
const props = Object.assign({}, componentProps, { |
| 90 |
context: search.context, |
| 91 |
domain: search.domain, |
| 92 |
groupBy: search.groupBy, |
| 93 |
orderBy: search.orderBy, |
| 94 |
comparison: search.comparison, |
| 95 |
display: Object.assign({}, search.display, componentProps.display), |
| 96 |
}); |
| 97 |
return filterPropsForComponent(params.Component, props); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
const env = await makeTestEnv({ serverData, mockRPC }); |
| 102 |
const searchEnv = Object.assign(Object.create(env), { config }); |
| 103 |
const parent = await mount(Parent, getFixture(), { env: searchEnv, props }); |
| 104 |
const parentNode = parent.__owl__; |
| 105 |
const withSearchNode = getUniqueChild(parentNode); |
| 106 |
const componentNode = getUniqueChild(withSearchNode); |
| 107 |
const component = componentNode.component; |
| 108 |
return component; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
function filterPropsForComponent(Component, props) { |
| 122 |
|
| 123 |
if (Component.props) { |
| 124 |
let componentKeys = null; |
| 125 |
if (Component.props instanceof Array) { |
| 126 |
componentKeys = Component.props.map((x) => x.replace("?", "")); |
| 127 |
} else { |
| 128 |
componentKeys = Object.keys(Component.props); |
| 129 |
} |
| 130 |
if (componentKeys.includes("*")) { |
| 131 |
return props; |
| 132 |
} else { |
| 133 |
return Object.keys(props) |
| 134 |
.filter((k) => componentKeys.includes(k)) |
| 135 |
.reduce((o, k) => { |
| 136 |
o[k] = props[k]; |
| 137 |
return o; |
| 138 |
}, {}); |
| 139 |
} |
| 140 |
} else { |
| 141 |
return props; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
function getUniqueChild(node) { |
| 146 |
return Object.values(node.children)[0]; |
| 147 |
} |
| 148 |
|
| 149 |
function getNode(target) { |
| 150 |
return target instanceof Component ? target.el : target; |
| 151 |
} |
| 152 |
|
| 153 |
export function findItem(target, selector, finder = 0) { |
| 154 |
const el = getNode(target); |
| 155 |
const elems = [...el.querySelectorAll(selector)]; |
| 156 |
if (Number.isInteger(finder)) { |
| 157 |
return elems[finder]; |
| 158 |
} |
| 159 |
return elems.find((el) => el.innerText.trim().toLowerCase() === String(finder).toLowerCase()); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
export async function toggleMenu(el, menuFinder) { |
| 165 |
const menu = findItem(el, `button.dropdown-toggle`, menuFinder); |
| 166 |
await click(menu); |
| 167 |
} |
| 168 |
|
| 169 |
export async function toggleMenuItem(el, itemFinder) { |
| 170 |
const item = findItem(el, `.o_menu_item`, itemFinder); |
| 171 |
if (item.classList.contains("dropdown-toggle")) { |
| 172 |
await mouseEnter(item); |
| 173 |
} else { |
| 174 |
await click(item); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
export async function toggleMenuItemOption(el, itemFinder, optionFinder) { |
| 179 |
const item = findItem(el, `.o_menu_item`, itemFinder); |
| 180 |
const option = findItem(item.parentNode, ".o_item_option", optionFinder); |
| 181 |
if (option.classList.contains("dropdown-toggle")) { |
| 182 |
await mouseEnter(option); |
| 183 |
} else { |
| 184 |
await click(option); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
export function isItemSelected(el, itemFinder) { |
| 189 |
const item = findItem(el, `.o_menu_item`, itemFinder); |
| 190 |
return item.classList.contains("selected"); |
| 191 |
} |
| 192 |
|
| 193 |
export function isOptionSelected(el, itemFinder, optionFinder) { |
| 194 |
const item = findItem(el, `.o_menu_item`, itemFinder); |
| 195 |
const option = findItem(item.parentNode, ".o_item_option", optionFinder); |
| 196 |
return option.classList.contains("selected"); |
| 197 |
} |
| 198 |
|
| 199 |
export function getMenuItemTexts(target) { |
| 200 |
const el = getNode(target); |
| 201 |
return [...el.querySelectorAll(`.dropdown-menu .o_menu_item`)].map((e) => e.innerText.trim()); |
| 202 |
} |
| 203 |
|
| 204 |
export function getVisibleButtons(el) { |
| 205 |
return [ |
| 206 |
...$(el).find( |
| 207 |
[ |
| 208 |
"div.o_control_panel_breadcrumbs button:visible", |
| 209 |
"div.o_control_panel_actions button:visible", |
| 210 |
].join(",") |
| 211 |
), |
| 212 |
]; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
export async function openAddCustomFilterDialog(el) { |
| 218 |
await click(findItem(el, `.o_filter_menu .o_menu_item.o_add_custom_filter`)); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
export async function selectGroup(el, fieldName) { |
| 224 |
el.querySelector(".o_add_custom_group_menu").value = fieldName; |
| 225 |
await triggerEvent(el, ".o_add_custom_group_menu", "change"); |
| 226 |
} |
| 227 |
|
| 228 |
export async function groupByMenu(el, fieldName) { |
| 229 |
await toggleSearchBarMenu(el); |
| 230 |
await selectGroup(el, fieldName); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
export async function deleteFavorite(el, favoriteFinder) { |
| 236 |
const favorite = findItem(el, `.o_favorite_menu .o_menu_item`, favoriteFinder); |
| 237 |
await click(findItem(favorite, "i.fa-trash-o")); |
| 238 |
} |
| 239 |
|
| 240 |
export async function toggleSaveFavorite(el) { |
| 241 |
await click(findItem(el, `.o_favorite_menu .o_add_favorite`)); |
| 242 |
} |
| 243 |
|
| 244 |
export async function editFavoriteName(el, name) { |
| 245 |
const input = findItem( |
| 246 |
el, |
| 247 |
`.o_favorite_menu .o_add_favorite + .o_accordion_values input[type="text"]` |
| 248 |
); |
| 249 |
input.value = name; |
| 250 |
await triggerEvents(input, null, ["input", "change"]); |
| 251 |
} |
| 252 |
|
| 253 |
export async function saveFavorite(el) { |
| 254 |
await click(findItem(el, `.o_favorite_menu .o_add_favorite + .o_accordion_values button`)); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
export function getFacetTexts(target) { |
| 260 |
const el = getNode(target); |
| 261 |
return [...el.querySelectorAll(`div.o_searchview_facet`)].map((facet) => |
| 262 |
facet.innerText.trim() |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
export async function removeFacet(el, facetFinder = 0) { |
| 267 |
const facet = findItem(el, `div.o_searchview_facet`, facetFinder); |
| 268 |
await click(facet.querySelector(".o_facet_remove")); |
| 269 |
} |
| 270 |
|
| 271 |
export async function editSearch(el, value) { |
| 272 |
const input = findItem(el, `.o_searchview input`); |
| 273 |
input.value = value; |
| 274 |
await triggerEvent(input, null, "input"); |
| 275 |
} |
| 276 |
|
| 277 |
export async function validateSearch(el) { |
| 278 |
const input = findItem(el, `.o_searchview input`); |
| 279 |
await triggerEvent(input, null, "keydown", { key: "Enter" }); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
export async function switchView(el, viewType) { |
| 285 |
await click(findItem(el, `button.o_switch_view.o_${viewType}`)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
export function getPagerValue(el) { |
| 291 |
const valueEl = findItem(el, ".o_pager .o_pager_value"); |
| 292 |
return valueEl.innerText.trim().split("-").map(Number); |
| 293 |
} |
| 294 |
|
| 295 |
export function getPagerLimit(el) { |
| 296 |
const limitEl = findItem(el, ".o_pager .o_pager_limit"); |
| 297 |
return Number(limitEl.innerText.trim()); |
| 298 |
} |
| 299 |
|
| 300 |
export async function pagerNext(el) { |
| 301 |
await click(findItem(el, ".o_pager button.o_pager_next")); |
| 302 |
} |
| 303 |
|
| 304 |
export async function pagerPrevious(el) { |
| 305 |
await click(findItem(el, ".o_pager button.o_pager_previous")); |
| 306 |
} |
| 307 |
|
| 308 |
export async function editPager(el, value) { |
| 309 |
await click(findItem(el, ".o_pager .o_pager_value")); |
| 310 |
await editInput(getNode(el), ".o_pager .o_pager_value.o_input", value); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
export async function toggleActionMenu(el) { |
| 322 |
await click(el.querySelector(".o_cp_action_menus .dropdown-toggle")); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
export async function toggleSearchBarMenu(el) { |
| 327 |
await click(findItem(el, `.o_searchview_dropdown_toggler`)); |
| 328 |
} |