Debian Codegraph @

addons/web/static/tests/views/fields/url_field.test.js

raw
1 import { expect, getFixture, test } from "@odoo/hoot";
2 import { queryAllAttributes, queryAllTexts, queryFirst } from "@odoo/hoot-dom";
3 import {
4 contains,
5 defineModels,
6 fieldInput,
7 fields,
8 models,
9 mountView,
10 onRpc,
11 } from "../../web_test_helpers";
12
13 class Product extends models.Model {
14 url = fields.Char();
15 }
16
17 defineModels([Product]);
18
19 onRpc("has_group", () => true);
20
21 test("UrlField in form view", async () => {
22 Product._records = [{ id: 1, url: "https://www.example.com" }];
23 await mountView({
24 type: "form",
25 resModel: "product",
26 resId: 1,
27 arch: `<form><field name="url" widget="url"/></form>`,
28 });
29 expect(`.o_field_widget input[type="text"]`).toHaveCount(1);
30 expect(`.o_field_widget input[type="text"]`).toHaveValue("https://www.example.com");
31 expect(`.o_field_url a`).toHaveAttribute("href", "https://www.example.com");
32 await fieldInput("url").edit("https://www.odoo.com");
33 expect(`.o_field_widget input[type="text"]`).toHaveValue("https://www.odoo.com");
34 });
35
36 test("in form view (readonly)", async () => {
37 Product._records = [{ id: 1, url: "https://www.example.com" }];
38 await mountView({
39 type: "form",
40 resModel: "product",
41 resId: 1,
42 arch: `<form><field name="url" widget="url" readonly="1"/></form>`,
43 });
44 expect("a.o_field_widget.o_form_uri").toHaveCount(1);
45 expect("a.o_field_widget.o_form_uri").toHaveAttribute("href", "https://www.example.com");
46 expect("a.o_field_widget.o_form_uri").toHaveAttribute("target", "_blank");
47 expect("a.o_field_widget.o_form_uri").toHaveText("https://www.example.com");
48 });
49
50 test("it takes its text content from the text attribute", async () => {
51 Product._records = [{ id: 1, url: "https://www.example.com" }];
52 await mountView({
53 type: "form",
54 resModel: "product",
55 resId: 1,
56 arch: '<form><field name="url" widget="url" text="https://another.com" readonly="1"/></form>',
57 });
58 expect(`.o_field_url a`).toHaveText("https://another.com");
59 });
60
61 test("href attribute and website_path option", async () => {
62 Product._fields.url1 = fields.Char();
63 Product._fields.url2 = fields.Char();
64 Product._fields.url3 = fields.Char();
65 Product._fields.url4 = fields.Char();
66 Product._records = [
67 {
68 id: 1,
69 url1: "http://www.url1.com",
70 url2: "www.url2.com",
71 url3: "http://www.url3.com",
72 url4: "https://url4.com",
73 },
74 ];
75 await mountView({
76 type: "form",
77 resModel: "product",
78 resId: 1,
79 arch: `
80 <form>
81 <field name="url1" widget="url" readonly="1"/>
82 <field name="url2" widget="url" readonly="1" options="{'website_path': True}"/>
83 <field name="url3" widget="url" readonly="1"/>
84 <field name="url4" widget="url" readonly="1"/>
85 </form>`,
86 });
87 expect(`.o_field_widget[name="url1"] a`).toHaveAttribute("href", "http://www.url1.com");
88 expect(`.o_field_widget[name="url2"] a`).toHaveAttribute("href", "www.url2.com");
89 expect(`.o_field_widget[name="url3"] a`).toHaveAttribute("href", "http://www.url3.com");
90 expect(`.o_field_widget[name="url4"] a`).toHaveAttribute("href", "https://url4.com");
91 });
92
93 test("in editable list view", async () => {
94 Product._records = [
95 { id: 1, url: "example.com" },
96 { id: 2, url: "odoo.com" },
97 ];
98 await mountView({
99 type: "list",
100 resModel: "product",
101 arch: '<list editable="bottom"><field name="url" widget="url"/></list>',
102 });
103 expect("tbody td:not(.o_list_record_selector) a").toHaveCount(2);
104 expect(".o_field_url.o_field_widget[name='url'] a").toHaveCount(2);
105 expect(queryAllAttributes(".o_field_url.o_field_widget[name='url'] a", "href")).toEqual([
106 "http://example.com",
107 "http://odoo.com",
108 ]);
109 expect(queryAllTexts(".o_field_url.o_field_widget[name='url'] a")).toEqual([
110 "example.com",
111 "odoo.com",
112 ]);
113 let cell = queryFirst("tbody td:not(.o_list_record_selector)");
114 await contains(cell).click();
115 expect(cell.parentElement).toHaveClass("o_selected_row");
116 expect(cell.querySelector("input")).toHaveValue("example.com");
117 await fieldInput("url").edit("test");
118 await contains(getFixture()).click(); // click out
119 cell = queryFirst("tbody td:not(.o_list_record_selector)");
120 expect(cell.parentElement).not.toHaveClass("o_selected_row");
121 expect("tbody td:not(.o_list_record_selector) a").toHaveCount(2);
122 expect(".o_field_url.o_field_widget[name='url'] a").toHaveCount(2);
123 expect(queryAllAttributes(".o_field_url.o_field_widget[name='url'] a", "href")).toEqual([
124 "http://test",
125 "http://odoo.com",
126 ]);
127 expect(queryAllTexts(".o_field_url.o_field_widget[name='url'] a")).toEqual([
128 "test",
129 "odoo.com",
130 ]);
131 });
132
133 test("with falsy value", async () => {
134 Product._records = [{ id: 1, url: false }];
135 await mountView({
136 type: "form",
137 resModel: "product",
138 resId: 1,
139 arch: '<form><field name="url" widget="url"/></form>',
140 });
141 expect(`[name=url] input`).toHaveCount(1);
142 expect(`[name=url] input`).toHaveValue("");
143 });
144
145 test("onchange scenario", async () => {
146 Product._fields.url_source = fields.Char({
147 onChange: (record) => (record.url = record.url_source),
148 });
149 Product._records = [{ id: 1, url: "odoo.com", url_source: "another.com" }];
150 await mountView({
151 type: "form",
152 resModel: "product",
153 resId: 1,
154 arch: `<form><field name="url" widget="url" readonly="True"/><field name="url_source"/></form>`,
155 });
156 expect(".o_field_widget[name=url]").toHaveText("odoo.com");
157 expect(".o_field_widget[name=url_source] input").toHaveValue("another.com");
158 await fieldInput("url_source").edit("example.com");
159 expect(".o_field_widget[name=url]").toHaveText("example.com");
160 });
161
162 test("with placeholder", async () => {
163 Product._records = [{ id: 1 }];
164 await mountView({
165 type: "form",
166 resModel: "product",
167 arch: `<form><field name="url" widget="url" placeholder="Placeholder"/></form>`,
168 });
169 expect(`.o_field_widget input`).toHaveAttribute("placeholder", "Placeholder");
170 });
171
172 test("placeholder_field shows as placeholder", async () => {
173 Product._fields.url1 = fields.Char({
174 default: "http://www.url1.com",
175 });
176 await mountView({
177 type: "form",
178 resModel: "product",
179 arch: `<form>
180 <field name="url" widget="url" options="{'placeholder_field' : 'url1'}"/>
181 <field name="url1"/>
182 </form>`,
183 });
184 expect(`.o_field_url input`).toHaveAttribute("placeholder", "http://www.url1.com");
185 });
186
187 test("with non falsy, but non url value", async () => {
188 Product._fields.url.default = "odoo://hello";
189 await mountView({
190 type: "form",
191 resModel: "product",
192 arch: `<form><field name="url" widget="url"/></form>`,
193 });
194 expect(".o_field_widget[name=url] a").toHaveAttribute("href", "http://odoo://hello");
195 });