Debian Codegraph @

addons/web/static/tests/views/form/form_renderer.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import { queryAllTexts, queryOne } from "@odoo/hoot-dom";
3 import { Component, xml } from "@odoo/owl";
4 import { defineModels, fields, models, mountView, contains } from "@web/../tests/web_test_helpers";
5
6 import { registry } from "@web/core/registry";
7
8 class Partner extends models.Model {
9 name = fields.Char();
10 charfield = fields.Char();
11
12 _records = [{ id: 1, name: "firstRecord", charfield: "content of charfield" }];
13 }
14 defineModels([Partner]);
15
16 test("compile form with modifiers", async () => {
17 Partner._views = {
18 form: /*xml*/ `
19 <form>
20 <div invisible="display_name == uid">
21 <field name="charfield"/>
22 </div>
23 <field name="display_name" readonly="display_name == uid"/>
24 </form>
25 `,
26 };
27
28 await mountView({
29 resModel: "partner",
30 type: "form",
31 resId: 1,
32 });
33 expect(`.o_form_editable input`).toHaveCount(2);
34 });
35
36 test("compile notebook with modifiers", async () => {
37 Partner._views = {
38 form: /*xml*/ `
39 <form>
40 <sheet>
41 <notebook>
42 <page name="p1" invisible="display_name == 'lol'"><field name="charfield"/></page>
43 <page name="p2"><field name="display_name"/></page>
44 </notebook>
45 </sheet>
46 </form>
47 `,
48 };
49
50 await mountView({
51 resModel: "partner",
52 type: "form",
53 resId: 1,
54 });
55 expect(queryAllTexts`.o_notebook_headers .nav-item`).toEqual(["p1", "p2"]);
56 });
57
58 test("compile header and buttons", async () => {
59 Partner._views = {
60 form: /*xml*/ `
61 <form>
62 <header>
63 <button string="ActionButton" class="oe_highlight" name="action_button" type="object"/>
64 </header>
65 </form>
66 `,
67 };
68
69 await mountView({
70 resModel: "partner",
71 type: "form",
72 resId: 1,
73 });
74 expect(`.o_statusbar_buttons button[name=action_button]:contains(ActionButton)`).toHaveCount(1);
75 });
76
77 test("render field with placeholder", async () => {
78 registry.category("fields").add(
79 "char",
80 {
81 component: class CharField extends Component {
82 static props = ["*"];
83 static template = xml`<div/>`;
84 setup() {
85 expect.step("setup field component");
86 expect(this.props.placeholder).toBe("e.g. Contact's Name or //someinfo...");
87 }
88 },
89 extractProps: ({ attrs }) => ({ placeholder: attrs.placeholder }),
90 },
91 { force: true }
92 );
93
94 Partner._views = {
95 form: /*xml*/ `
96 <form>
97 <field name="display_name" placeholder="e.g. Contact's Name or //someinfo..." />
98 </form>
99 `,
100 };
101
102 await mountView({
103 resModel: "partner",
104 type: "form",
105 resId: 1,
106 });
107 expect.verifySteps(["setup field component"]);
108 });
109
110 test.tags("desktop");
111 test("compile a button with id on desktop", async () => {
112 Partner._views = {
113 form: /*xml*/ `
114 <form>
115 <header>
116 <button id="action_button" string="ActionButton"/>
117 </header>
118 </form>
119 `,
120 };
121
122 await mountView({
123 resModel: "partner",
124 type: "form",
125 resId: 1,
126 });
127 expect(`button[id=action_button]`).toHaveCount(1);
128 });
129
130 test.tags("mobile");
131 test("compile a button with id on mobile", async () => {
132 Partner._views = {
133 form: /*xml*/ `
134 <form>
135 <header>
136 <button id="action_button" string="ActionButton"/>
137 </header>
138 </form>
139 `,
140 };
141
142 await mountView({
143 resModel: "partner",
144 type: "form",
145 resId: 1,
146 });
147 await contains(`.o_cp_action_menus button:has(.fa-cog)`).click();
148 expect(`button[id=action_button]`).toHaveCount(1);
149 });
150
151 test("compile a button with disabled", async () => {
152 Partner._views = {
153 form: /*xml*/ `
154 <form>
155 <button id="action_button" string="ActionButton" name="action_button" type="object" disabled="disabled"/>
156 </form>
157 `,
158 };
159
160 await mountView({
161 resModel: "partner",
162 type: "form",
163 resId: 1,
164 });
165 expect(`button[id=action_button]`).toHaveAttribute("disabled");
166 });
167
168 test.tags("desktop");
169 test("statusbar stay visible when scrolling (sticky)", async () => {
170 Partner._views = {
171 form: /*xml*/ `
172 <form>
173 <header>
174 <button id="action_button" string="ActionButton"/>
175 </header>
176 <sheet>
177 <h2>My Sheet</h2>
178 <div class="position-relative" style="height: 200vh"></div>
179 </sheet>
180 </form>
181 `,
182 };
183
184 await mountView({
185 resModel: "partner",
186 type: "form",
187 resId: 1,
188 });
189
190 const statusBar = queryOne(".o_form_view .o_form_statusbar");
191
192 const scrollTarget = queryOne(".o_form_view .o_content");
193 const scrollRect = scrollTarget.getBoundingClientRect();
194 expect(statusBar.getBoundingClientRect().top).toBeWithin(scrollRect.top, scrollRect.bottom);
195 scrollTarget.scrollTop = scrollTarget.scrollHeight; // scroll to bottom
196 expect(statusBar.getBoundingClientRect().top).toBeWithin(scrollRect.top, scrollRect.bottom);
197 });
198
199 test.tags("mobile");
200 test("statusbar is non-sticky on mobile", async () => {
201 Partner._views = {
202 form: /*xml*/ `
203 <form>
204 <header>
205 <button id="action_button" string="ActionButton"/>
206 </header>
207 <sheet>
208 <h2>My Sheet</h2>
209 <div class="position-relative" style="height: 200vh"></div>
210 </sheet>
211 </form>
212 `,
213 };
214
215 await mountView({
216 resModel: "partner",
217 type: "form",
218 resId: 1,
219 });
220
221 const statusBar = queryOne(".o_form_view .o_form_statusbar");
222
223 const scrollTarget = queryOne(".o_form_view .o_form_view_container");
224 const scrollRect = scrollTarget.getBoundingClientRect();
225 expect(statusBar.getBoundingClientRect().top).toBeWithin(scrollRect.top, scrollRect.bottom);
226 scrollTarget.scrollTop = scrollTarget.scrollHeight; // scroll to bottom
227 expect(statusBar.getBoundingClientRect().top).not.toBeWithin(scrollRect.top, scrollRect.bottom);
228 });