| 1 |
|
| 2 |
|
| 3 |
import { makeTestEnv } from "@web/../tests/helpers/mock_env"; |
| 4 |
import { getFixture, mount, nextTick } from "@web/../tests/helpers/utils"; |
| 5 |
import { createDebugContext } from "@web/core/debug/debug_context"; |
| 6 |
import { Dialog } from "@web/core/dialog/dialog"; |
| 7 |
import { MainComponentsContainer } from "@web/core/main_components_container"; |
| 8 |
import { registry } from "@web/core/registry"; |
| 9 |
import { View, getDefaultConfig } from "@web/views/view"; |
| 10 |
import { |
| 11 |
makeFakeLocalizationService, |
| 12 |
patchUserWithCleanup, |
| 13 |
} from "../helpers/mock_services"; |
| 14 |
import { |
| 15 |
setupControlPanelFavoriteMenuRegistry, |
| 16 |
setupControlPanelServiceRegistry, |
| 17 |
} from "../search/helpers"; |
| 18 |
|
| 19 |
import { Component, useSubEnv, xml } from "@odoo/owl"; |
| 20 |
|
| 21 |
const serviceRegistry = registry.category("services"); |
| 22 |
|
| 23 |
const rootDialogTemplate = xml`<Dialog><View t-props="props.viewProps"/></Dialog>`; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
async function _makeView(params, inDialog = false) { |
| 42 |
const props = { resId: false, ...params }; |
| 43 |
const serverData = props.serverData; |
| 44 |
const mockRPC = props.mockRPC; |
| 45 |
const config = { |
| 46 |
...getDefaultConfig(), |
| 47 |
...props.config, |
| 48 |
}; |
| 49 |
|
| 50 |
delete props.serverData; |
| 51 |
delete props.mockRPC; |
| 52 |
delete props.config; |
| 53 |
|
| 54 |
if (props.arch) { |
| 55 |
serverData.views = serverData.views || {}; |
| 56 |
props.viewId = params.viewId || 100000001; |
| 57 |
serverData.views[`${props.resModel},${props.viewId},${props.type}`] = props.arch; |
| 58 |
delete props.arch; |
| 59 |
props.searchViewId = 100000002; |
| 60 |
const searchViewArch = props.searchViewArch || "<search/>"; |
| 61 |
serverData.views[`${props.resModel},${props.searchViewId},search`] = searchViewArch; |
| 62 |
delete props.searchViewArch; |
| 63 |
} |
| 64 |
|
| 65 |
const env = await makeTestEnv({ serverData, mockRPC }); |
| 66 |
Object.assign(env, createDebugContext(env)); |
| 67 |
|
| 68 |
const target = getFixture(); |
| 69 |
const viewEnv = Object.assign(Object.create(env), { config }); |
| 70 |
|
| 71 |
await mount(MainComponentsContainer, target, { env }); |
| 72 |
let viewNode; |
| 73 |
if (inDialog) { |
| 74 |
let root; |
| 75 |
class RootDialog extends Component { |
| 76 |
static components = { Dialog, View }; |
| 77 |
static template = rootDialogTemplate; |
| 78 |
static props = ["*"]; |
| 79 |
setup() { |
| 80 |
root = this; |
| 81 |
useSubEnv(viewEnv); |
| 82 |
} |
| 83 |
} |
| 84 |
env.services.dialog.add(RootDialog, { viewProps: props }); |
| 85 |
await nextTick(); |
| 86 |
const rootNode = root.__owl__; |
| 87 |
const dialogNode = Object.values(rootNode.children)[0]; |
| 88 |
viewNode = Object.values(dialogNode.children)[0]; |
| 89 |
} else { |
| 90 |
const view = await mount(View, target, { env: viewEnv, props }); |
| 91 |
await nextTick(); |
| 92 |
viewNode = view.__owl__; |
| 93 |
} |
| 94 |
const withSearchNode = Object.values(viewNode.children)[0]; |
| 95 |
const concreteViewNode = Object.values(withSearchNode.children)[0]; |
| 96 |
const concreteView = concreteViewNode.component; |
| 97 |
|
| 98 |
return concreteView; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
export function makeView(params) { |
| 105 |
return _makeView(params); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
export function makeViewInDialog(params) { |
| 112 |
return _makeView(params, true); |
| 113 |
} |
| 114 |
|
| 115 |
export function setupViewRegistries() { |
| 116 |
setupControlPanelFavoriteMenuRegistry(); |
| 117 |
setupControlPanelServiceRegistry(); |
| 118 |
patchUserWithCleanup({ |
| 119 |
hasGroup: async (group) => { |
| 120 |
return [ |
| 121 |
"base.group_allow_export", |
| 122 |
"base.group_user", |
| 123 |
].includes(group); |
| 124 |
}, |
| 125 |
isInternalUser: true, |
| 126 |
}); |
| 127 |
serviceRegistry.add("localization", makeFakeLocalizationService()); |
| 128 |
} |