1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
odoo.define('web.model_field_selector_tests', function (require) {
"use strict";
var ModelFieldSelector = require("web.ModelFieldSelector");
var testUtils = require("web.test_utils");
QUnit.module('widgets', {}, function () {
QUnit.module('ModelFieldSelector', {
beforeEach: function () {
this.data = {
partner: {
fields: {
foo: {string: "Foo", type: "char", searchable: true},
bar: {string: "Bar", type: "boolean", searchable: true},
product_id: {string: "Product", type: "many2one", relation: "product", searchable: true},
},
records: [{
id: 1,
foo: "yop",
bar: true,
product_id: 37,
}, {
id: 2,
foo: "blip",
bar: true,
product_id: false,
}, {
id: 4,
foo: "abc",
bar: false,
product_id: 41,
}],
onchanges: {},
},
product: {
fields: {
name: {string: "Product Name", type: "char", searchable: true}
},
records: [{
id: 37,
display_name: "xphone",
}, {
id: 41,
display_name: "xpad",
}]
},
};
},
}, function () {
QUnit.test("creating a field chain from scratch", async function (assert) {
assert.expect(14);
var $target = $("#qunit-fixture");
// Create the field selector and its mock environment
var fieldSelector = new ModelFieldSelector(null, "partner", [], {
readonly: false,
debugMode: true,
});
await testUtils.mock.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
var $value = fieldSelector.$("> .o_field_selector_value");
// Focusing the field selector input should open a field selector popover
fieldSelector.$el.trigger('focusin');
var $fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
assert.strictEqual($fieldSelectorPopover.length, 1,
"field selector popover should be visible");
// The field selector popover should contain the list of "partner"
// fields. "Bar" should be among them.
var $lis = $fieldSelectorPopover.find("li");
var $barLi = $();
$lis.each(function () {
var $li = $(this);
if ($li.html().indexOf("Bar") >= 0) {
$barLi = $li;
}
});
assert.strictEqual($barLi.length, 1,
"field selector popover should contain the 'Bar' field");
// Clicking the "Bar" field should close the popover and set the field
// chain to "bar" as it is a basic field
await testUtils.dom.click($barLi);
assert.notOk($fieldSelectorPopover.is("visible"),
"field selector popover should be closed now");
assert.strictEqual(getValueFromDOM($value), "Bar",
"field selector value should be displayed with a 'Bar' tag");
assert.deepEqual(fieldSelector.getSelectedField(), {
model: "partner",
name: "bar",
searchable: true,
string: "Bar",
type: "boolean",
}, "the selected field should be correctly set");
// Focusing the input again should open the same popover
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
assert.ok($fieldSelectorPopover.is(":visible"),
"field selector popover should be visible");
// The field selector popover should contain the list of "partner"
// fields. "Product" should be among them.
$lis = $fieldSelectorPopover.find("li");
var $productLi = $();
$lis.each(function () {
var $li = $(this);
if ($li.html().indexOf("Product") >= 0) {
$productLi = $li;
}
});
assert.strictEqual($productLi.length, 1,
"field selector popover should contain the 'Product' field");
// Clicking on the "Product" field should update the popover to show
// the product fields (so only "Product Name" should be there)
await testUtils.dom.click($productLi);
$lis = $fieldSelectorPopover.find("li");
assert.strictEqual($lis.length, 1,
"there should be only one field proposition for 'product' model");
assert.ok($lis.first().html().indexOf("Product Name") >= 0,
"the name of the only suggestion should be 'Product Name'");
// Clicking on "Product Name" should close the popover and set the chain
// to "product_id.name"
await testUtils.dom.click($lis.first());
assert.notOk($fieldSelectorPopover.is("visible"),
"field selector popover should be closed now");
assert.strictEqual(getValueFromDOM($value), "Product -> Product Name",
"field selector value should be displayed with two tags: 'Product' and 'Product Name'");
// Remove the current selection and recreate it again
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
await testUtils.dom.click(fieldSelector.$('.o_field_selector_prev_page'));
await testUtils.dom.click(fieldSelector.$('.o_field_selector_close'));
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
$fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
$lis = $fieldSelectorPopover.find("li");
$productLi = $();
$lis.each(function () {
var $li = $(this);
if ($li.html().indexOf("Product") >= 0) {
$productLi = $li;
}
});
assert.strictEqual($productLi.length, 1,
"field selector popover should contain the 'Product' field");
await testUtils.dom.click($productLi);
$lis = $fieldSelectorPopover.find("li");
await testUtils.dom.click($lis.first());
assert.notOk($fieldSelectorPopover.is("visible"),
"field selector popover should be closed now");
assert.strictEqual(getValueFromDOM($value), "Product -> Product Name",
"field selector value should be displayed with two tags: 'Product' and 'Product Name'");
fieldSelector.destroy();
function getValueFromDOM($dom) {
return _.map($dom.find(".o_field_selector_chain_part"), function (part) {
return $(part).text().trim();
}).join(" -> ");
}
});
QUnit.test("default field chain should set the page data correctly", async function (assert) {
assert.expect(3);
var $target = $("#qunit-fixture");
// Create the field selector and its mock environment
// passing 'product_id' as a prefilled field-chain
var fieldSelector = new ModelFieldSelector(null, "partner", ['product_id'], {
readonly: false,
debugMode: true,
});
await testUtils.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
// Focusing the field selector input should open a field selector popover
fieldSelector.$el.trigger('focusin');
var $fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
assert.strictEqual($fieldSelectorPopover.length, 1,
"field selector popover should be visible");
// The field selector popover should contain the list of "product"
// fields. "Product Name" should be among them.
var $lis = $fieldSelectorPopover.find("li");
assert.strictEqual($lis.length, 1,
"there should be only one field proposition for 'product' model");
assert.ok($lis.first().html().indexOf("Product Name") >= 0,
"the name of the only suggestion should be 'Product Name'");
fieldSelector.destroy();
});
QUnit.test("use the filter option", async function (assert) {
assert.expect(2);
var $target = $("#qunit-fixture");
// Create the field selector and its mock environment
var fieldSelector = new ModelFieldSelector(null, "partner", [], {
readonly: false,
filter: function (field) {
return field.type === 'many2one';
},
});
await testUtils.mock.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
var $fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
var $lis = $fieldSelectorPopover.find("li");
assert.strictEqual($lis.length, 1, "there should only be one element");
assert.strictEqual($lis.text().trim(), "Product", "the available field should be the many2one");
fieldSelector.destroy();
});
QUnit.test("default `showSearchInput` option", async function (assert) {
assert.expect(6);
var $target = $("#qunit-fixture");
// Create the field selector and its mock environment
var fieldSelector = new ModelFieldSelector(null, "partner", [], {
readonly: false,
});
await testUtils.mock.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
var $fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
var $searchInput = $fieldSelectorPopover.find(".o_field_selector_search input");
assert.strictEqual($searchInput.length, 1, "there should be a search input");
// without search
assert.strictEqual($fieldSelectorPopover.find("li").length, 3, "there should be three available fields");
assert.strictEqual($fieldSelectorPopover.find("li").text().trim().replace(/\s+/g, ' '), "Bar Foo Product", "the available field should be correct");
await testUtils.fields.editAndTrigger($searchInput, 'xx', 'keyup');
assert.strictEqual($fieldSelectorPopover.find("li").length, 0, "there shouldn't be any element");
await testUtils.fields.editAndTrigger($searchInput, 'Pro', 'keyup');
assert.strictEqual($fieldSelectorPopover.find("li").length, 1, "there should only be one element");
assert.strictEqual($fieldSelectorPopover.find("li").text().trim().replace(/\s+/g, ' '), "Product", "the available field should be the Product");
fieldSelector.destroy();
});
QUnit.test("false `showSearchInput` option", async function (assert) {
assert.expect(1);
var $target = $("#qunit-fixture");
// Create the field selector and its mock environment
var fieldSelector = new ModelFieldSelector(null, "partner", [], {
readonly: false,
showSearchInput: false,
});
await testUtils.mock.addMockEnvironment(fieldSelector, { data: this.data });
await fieldSelector.appendTo($target);
fieldSelector.$el.trigger('focusin');
await testUtils.nextTick();
var $fieldSelectorPopover = fieldSelector.$(".o_field_selector_popover:visible");
var $searchInput = $fieldSelectorPopover.find(".o_field_selector_search input");
assert.strictEqual($searchInput.length, 0, "there should be no search input");
fieldSelector.destroy();
});
QUnit.test("create a field chain with value 1 i.e. TRUE_LEAF", async function (assert) {
assert.expect(1);
var $target = $("#qunit-fixture");
//create the field selector with domain value ["1"]
var fieldSelector = new ModelFieldSelector(null, "partner", ["1"], {
readonly: false,
showSearchInput: false,
});
await testUtils.mock.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
var $fieldName = fieldSelector.$('.o_field_selector_chain_part');
assert.strictEqual($fieldName.text().trim(), "1",
"field name value should be 1.");
fieldSelector.destroy();
});
QUnit.test("create a field chain with value 0 i.e. FALSE_LEAF", async function (assert) {
assert.expect(1);
var $target = $("#qunit-fixture");
//create the field selector with domain value ["0"]
var fieldSelector = new ModelFieldSelector(null, "partner", ["0"], {
readonly: false,
showSearchInput: false,
});
await testUtils.mock.addMockEnvironment(fieldSelector, {data: this.data});
await fieldSelector.appendTo($target);
var $fieldName = fieldSelector.$('.o_field_selector_chain_part');
assert.strictEqual($fieldName.text().trim(), "0",
"field name value should be 0.");
fieldSelector.destroy();
});
});
});
});
|