Debian Codegraph @

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

raw
1 import { expect, getFixture, test } from "@odoo/hoot";
2 import {
3 contains,
4 defineModels,
5 fieldInput,
6 fields,
7 models,
8 mountView,
9 onRpc,
10 } from "../../web_test_helpers";
11 import { queryAllTexts, queryFirst } from "@odoo/hoot-dom";
12
13 class Contact extends models.Model {
14 email = fields.Char();
15 }
16
17 defineModels([Contact]);
18
19 onRpc("has_group", () => true);
20
21 test("in form view", async () => {
22 Contact._records = [{ id: 1, email: "[email protected]" }];
23 await mountView({
24 type: "form",
25 resModel: "contact",
26 resId: 1,
27 arch: `<form><field name="email" widget="email"/></form>`,
28 });
29 expect(`.o_field_email input[type="email"]`).toHaveCount(1);
30 expect(`.o_field_email input[type="email"]`).toHaveValue("[email protected]");
31 expect(`.o_field_email a`).toHaveCount(1);
32 expect(`.o_field_email a`).toHaveAttribute("href", "mailto:[email protected]");
33 expect(`.o_field_email a`).toHaveAttribute("target", "_blank");
34 await fieldInput("email").edit("[email protected]");
35 expect(`.o_field_email input[type="email"]`).toHaveValue("[email protected]");
36 });
37
38 test("in editable list view", async () => {
39 Contact._records = [
40 { id: 1, email: "[email protected]" },
41 { id: 2, email: "[email protected]" },
42 ];
43 await mountView({
44 type: "list",
45 resModel: "contact",
46 arch: '<list editable="bottom"><field name="email" widget="email"/></list>',
47 });
48 expect(`tbody td:not(.o_list_record_selector) a`).toHaveCount(2);
49 expect(`.o_field_email a`).toHaveCount(2);
50 expect(queryAllTexts(`tbody td:not(.o_list_record_selector) a`)).toEqual([
51 "[email protected]",
52 "[email protected]",
53 ]);
54 expect(".o_field_email a:first").toHaveAttribute("href", "mailto:[email protected]");
55 let cell = queryFirst("tbody td:not(.o_list_record_selector)");
56 await contains(cell).click();
57 expect(cell.parentElement).toHaveClass("o_selected_row");
58 expect(`.o_field_email input[type="email"]`).toHaveValue("[email protected]");
59 await fieldInput("email").edit("[email protected]");
60 await contains(getFixture()).click();
61 cell = queryFirst("tbody td:not(.o_list_record_selector)");
62 expect(cell.parentElement).not.toHaveClass("o_selected_row");
63 expect(queryAllTexts(`tbody td:not(.o_list_record_selector) a`)).toEqual([
64 "[email protected]",
65 "[email protected]",
66 ]);
67 expect(".o_field_email a:first").toHaveAttribute("href", "mailto:[email protected]");
68 });
69
70 test("with empty value", async () => {
71 await mountView({
72 type: "form",
73 resModel: "contact",
74 arch: `<form><field name="email" widget="email" placeholder="Placeholder"/></form>`,
75 });
76 expect(`.o_field_email input`).toHaveValue("");
77 });
78
79 test("with placeholder", async () => {
80 await mountView({
81 type: "form",
82 resModel: "contact",
83 arch: `<form><field name="email" widget="email" placeholder="Placeholder"/></form>`,
84 });
85 expect(`.o_field_email input`).toHaveAttribute("placeholder", "Placeholder");
86 });
87
88 test("placeholder_field shows as placeholder", async () => {
89 Contact._fields.char = fields.Char({
90 default: "My Placeholder",
91 });
92 await mountView({
93 type: "form",
94 resModel: "contact",
95 arch: `<form>
96 <field name="email" widget="email" options="{'placeholder_field' : 'char'}"/>
97 <field name="char"/>
98 </form>`,
99 });
100 expect(`.o_field_email input`).toHaveAttribute("placeholder", "My Placeholder");
101 });
102
103 test("trim user value", async () => {
104 await mountView({
105 type: "form",
106 resModel: "contact",
107 arch: '<form><field name="email" widget="email"/></form>',
108 });
109
110 await fieldInput("email").edit(" [email protected] ");
111 await contains(getFixture()).click();
112 expect(`.o_field_email input`).toHaveValue("[email protected]");
113 });
114
115 test("onchange scenario with readonly", async () => {
116 Contact._fields.phone = fields.Char({
117 onChange: (record) => {
118 record.email = "[email protected]";
119 },
120 });
121 Contact._records = [{ id: 1, email: "[email protected]" }];
122 await mountView({
123 type: "form",
124 resModel: "contact",
125 resId: 1,
126 arch: `<form><field name="phone"/><field name="email" widget="email" readonly="1"/></form>`,
127 });
128 expect(`.o_field_email`).toHaveText("[email protected]");
129 await fieldInput("phone").edit("047412345");
130 expect(`.o_field_email`).toHaveText("[email protected]");
131 });