Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import {
3 contains,
4 defineModels,
5 fieldInput,
6 fields,
7 mockService,
8 models,
9 mountView,
10 patchWithCleanup,
11 } from "@web/../tests/web_test_helpers";
12
13 class Partner extends models.Model {
14 _name = "res.partner";
15
16 char_field = fields.Char({
17 string: "Char",
18 default: "My little Char Value",
19 trim: true,
20 });
21
22 _records = [
23 {
24 id: 1,
25 char_field: "char value",
26 },
27 ];
28
29 _views = {
30 form: /* xml */ `
31 <form>
32 <sheet>
33 <group>
34 <field name="char_field" widget="CopyClipboardChar"/>
35 </group>
36 </sheet>
37 </form>`,
38 };
39 }
40
41 defineModels([Partner]);
42
43 test("Char Field: Copy to clipboard button", async () => {
44 await mountView({
45 type: "form",
46 resModel: "res.partner",
47 resId: 1,
48 });
49
50 expect(".o_clipboard_button.o_btn_char_copy").toHaveCount(1);
51 });
52
53 test("Show copy button even on empty field", async () => {
54 Partner._records.push({
55 char_field: false,
56 });
57
58 await mountView({ type: "form", resModel: "res.partner", resId: 2 });
59
60 expect(".o_field_CopyClipboardChar[name='char_field'] .o_clipboard_button").toHaveCount(1);
61 });
62
63 test("Show copy button even on readonly empty field", async () => {
64 Partner._fields.char_field.readonly = true;
65 await mountView({
66 type: "form",
67 resModel: "res.partner",
68 arch: `
69 <form>
70 <sheet>
71 <group>
72 <field name="char_field" widget="CopyClipboardChar" />
73 </group>
74 </sheet>
75 </form>`,
76 });
77
78 expect(".o_field_CopyClipboardChar[name='char_field'] .o_clipboard_button").toHaveCount(1);
79 });
80
81 test("Display a tooltip on click", async () => {
82 mockService("popover", {
83 add(el, comp, params) {
84 expect(params).toEqual({ tooltip: "Copied" });
85 expect.step("copied tooltip");
86 return () => {};
87 },
88 });
89
90 patchWithCleanup(navigator.clipboard, {
91 async writeText(text) {
92 expect.step(text);
93 },
94 });
95
96 await mountView({
97 type: "form",
98 resModel: "res.partner",
99 resId: 1,
100 });
101
102 await expect(".o_clipboard_button.o_btn_char_copy").toHaveCount(1);
103 await contains(".o_clipboard_button", { visible: false }).click();
104 expect.verifySteps(["char value", "copied tooltip"]);
105 });
106
107 test("CopyClipboardButtonField in form view", async () => {
108 patchWithCleanup(navigator.clipboard, {
109 async writeText(text) {
110 expect.step(text);
111 },
112 });
113
114 await mountView({
115 type: "form",
116 resModel: "res.partner",
117 resId: 1,
118 arch: `
119 <form>
120 <group>
121 <field name="char_field" widget="CopyClipboardButton"/>
122 </group>
123 </form>`,
124 });
125
126 expect(".o_field_widget[name=char_field] input").toHaveCount(0);
127 expect(".o_clipboard_button.o_btn_char_copy").toHaveCount(1);
128 expect(".o_clipboard_button.o_btn_char_copy").toHaveClass("btn-primary");
129 expect(".o_clipboard_button.o_btn_char_copy").not.toHaveClass("btn-secondary");
130
131 await contains(".o_clipboard_button.o_btn_char_copy").click();
132
133 expect.verifySteps(["char value"]);
134 });
135
136 test("CopyClipboardButtonField with a secondary style", async () => {
137 await mountView({
138 type: "form",
139 resModel: "res.partner",
140 resId: 1,
141 arch: `
142 <form>
143 <group>
144 <field name="char_field" widget="CopyClipboardButton" options="{'btn_class': 'secondary'}"/>
145 </group>
146 </form>`,
147 });
148
149 expect(".o_field_widget[name=char_field] input").toHaveCount(0);
150 expect(".o_clipboard_button.o_btn_char_copy").toHaveCount(1);
151 expect(".o_clipboard_button.o_btn_char_copy").not.toHaveClass("btn-primary");
152 expect(".o_clipboard_button.o_btn_char_copy").toHaveClass("btn-secondary");
153 });
154
155 test("CopyClipboardButtonField can be disabled", async () => {
156 patchWithCleanup(navigator.clipboard, {
157 async writeText(text) {
158 expect.step(text);
159 },
160 });
161
162 await mountView({
163 type: "form",
164 resModel: "res.partner",
165 resId: 1,
166 arch: `
167 <form>
168 <sheet>
169 <group>
170 <field name="char_field" disabled="char_field == 'char value'" widget="CopyClipboardButton"/>
171 <field name="char_field" widget="char"/>
172 </group>
173 </sheet>
174 </form>`,
175 });
176 expect(".o_clipboard_button.o_btn_char_copy[disabled]").toHaveCount(1);
177 await fieldInput("char_field").edit("another char value");
178 expect(".o_clipboard_button.o_btn_char_copy[disabled]").toHaveCount(0);
179 });