1 openerp.testing.section('editor', {
2 dependencies: ['web.list_editable'],
5 setup: function (instance, $s, mock) {
6 mock('test.model:create', function () {
13 * @param {String} name
14 * @param {Object} [attrs]
15 * @param {String} [attrs.type="char"]
16 * @param {Boolean} [attrs.required]
17 * @param {Boolean} [attrs.invisible]
18 * @param {Boolean} [attrs.readonly]
21 function field(name, attrs) {
24 return _.defaults(attrs, {
30 * @param {Array} [fields]
33 function makeFormView(fields) {
35 _(fields).each(function (field) {
41 var children = _(fields).map(function (field) {
46 modifiers: JSON.stringify({
47 required: field.required,
48 invisible: field.invisible,
49 readonly: field.readonly
59 'class': 'oe_form_container'
67 test('base-state', {asserts: 2}, function (instance, $fix) {
68 var e = new instance.web.list.Editor({
70 edition_view: function () {
71 return makeFormView();
74 return e.appendTo($fix)
76 ok(!e.is_editing(), "should not be editing");
77 ok(e.form instanceof instance.web.FormView,
78 "should use default form type");
81 test('toggle-edition-save', {
83 setup: function (instance, $s, mock) {
84 mock('test.model:search_read', function () {
85 return [{id: 42, a: false, b: false, c: false}];
88 }, function (instance, $fix) {
89 var e = new instance.web.list.Editor({
90 dataset: new instance.web.DataSetSearch(null, 'test.model'),
91 prepends_on_create: function () { return false; },
92 edition_view: function () {
93 return makeFormView([ field('a'), field('b'), field('c') ]);
97 return e.appendTo($fix)
99 return e.edit({}, function () {
103 .then(function (form) {
104 ok(e.is_editing(), "should be editing");
105 equal(counter, 3, "should have configured all fields");
108 .done(function (record) {
109 ok(!e.is_editing(), "should have stopped editing");
110 equal(record.id, 42, "should have newly created id");
113 test('toggle-edition-cancel', { asserts: 2 }, function (instance, $fix) {
114 var e = new instance.web.list.Editor({
115 dataset: new instance.web.DataSetSearch(null, 'test.model'),
116 prepends_on_create: function () { return false; },
117 edition_view: function () {
118 return makeFormView([ field('a'), field('b'), field('c') ]);
122 return e.appendTo($fix)
124 return e.edit({}, function () {
128 .then(function (form) {
131 .done(function (record) {
132 ok(!e.is_editing(), "should have stopped editing");
133 ok(!record.id, "should have no id");
136 test('toggle-save-required', {
138 fail_on_rejection: false
139 }, function (instance, $fix) {
140 var e = new instance.web.list.Editor({
141 do_warn: function () {
144 dataset: new instance.web.DataSetSearch(null, 'test.model'),
145 prepends_on_create: function () { return false; },
146 edition_view: function () {
147 return makeFormView([
148 field('a', {required: true}), field('b'), field('c') ]);
153 return e.appendTo($fix)
155 return e.edit({}, function () {
159 .then(function (form) {
162 .done(function () { ok(false, "cancel should not succeed"); })
164 equal(warnings, 1, "should have been warned");
165 ok(e.is_editing(), "should have kept editing");
169 openerp.testing.section('list.edition', {
170 dependencies: ['web.list_editable'],
173 setup: function (instance, $s, mock) {
175 mock('demo:create', function (args) {
176 records[42] = _.extend({}, args[0]);
179 mock('demo:read', function (args) {
182 return [records[id]];
186 mock('demo:search_read', function (args) {
187 // args[0][0] = ["id", "=", 42]
189 var id = args[0][0][2];
191 return [records[id]];
195 mock('demo:fields_view_get', function () {
199 a: {type: 'char', string: "A"},
200 b: {type: 'char', string: "B"},
201 c: {type: 'char', string: "C"}
203 arch: '<tree><field name="a"/><field name="b"/><field name="c"/></tree>',
208 test('newrecord', {asserts: 6}, function (instance, $fix, mock) {
209 var got_defaults = false;
210 mock('demo:default_get', function (args) {
211 var fields = args[0];
213 fields, ['a', 'b', 'c'],
214 "should ask defaults for all fields");
216 return { a: "qux", b: "quux" };
219 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
220 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
222 return l.appendTo($fix)
223 .then(l.proxy('reload_content'))
225 return l.start_edition();
228 ok(got_defaults, "should have fetched default values for form");
230 return l.save_edition();
232 .then(function (result) {
233 ok(result.created, "should yield newly created record");
234 equal(result.record.get('a'), "qux",
235 "should have used default values");
236 equal(result.record.get('b'), "quux",
237 "should have used default values");
238 ok(!result.record.get('c'),
239 "should have no value if there was no default");
243 openerp.testing.section('list.edition.events', {
244 dependencies: ['web.list_editable'],
247 setup: function (instance, $s, mock) {
248 mock('demo:read', function () {
249 return [{ id: 1, a: 'foo', b: 'bar', c: 'baz' }];
251 mock('demo:fields_view_get', function () {
255 a: {type: 'char', string: "A"},
256 b: {type: 'char', string: "B"},
257 c: {type: 'char', string: "C"}
259 arch: '<tree><field name="a"/><field name="b"/><field name="c"/></tree>',
264 test('edition events', {asserts: 4}, function (instance, $fix) {
265 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
268 onEvent: function (e) { this.counter++; }
270 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
271 l.on('edit:before edit:after', o, o.onEvent);
272 return l.appendTo($fix)
273 .then(l.proxy('reload_content'))
275 ok(l.options.editable, "should be editable");
276 equal(o.counter, 0, "should have seen no event yet");
277 return l.start_edition(l.records.get(1));
280 ok(l.editor.is_editing(), "should be editing");
281 equal(o.counter, 2, "should have seen two edition events");
285 test('edition events: cancelling', {asserts: 3}, function (instance, $fix) {
286 var edit_after = false;
287 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
288 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
289 l.on('edit:before', {}, function (e) {
292 l.on('edit:after', {}, function () {
295 return l.appendTo($fix)
296 .then(l.proxy('reload_content'))
298 ok(l.options.editable, "should be editable");
299 return l.start_edition();
301 // cancelling an event rejects the deferred
302 .then($.Deferred().reject(), function () {
303 ok(!l.editor.is_editing(), "should not be editing");
304 ok(!edit_after, "should not have fired the edit:after event");
310 openerp.testing.section('list.edition.onwrite', {
311 dependencies: ['web.list_editable'],
315 test('record-to-read', {asserts: 4}, function (instance, $fix, mock) {
316 mock('demo:fields_view_get', function () {
320 a: {type: 'char', string: "A"}
322 arch: '<tree on_write="on_write" colors="red:a == \'foo\'"><field name="a"/></tree>',
325 mock('demo:read', function (args, kwargs) {
326 if (_.isEmpty(args[0])) {
329 throw new Error(JSON.stringify(_.toArray(arguments)));
331 mock('demo:search_read', function (args, kwargs) {
332 if (_.isEqual(args[0], [['id', 'in', [1]]])) {
333 return [{id: 1, a: 'some value'}];
334 } else if (_.isEqual(args[0], [['id', 'in', [42]]])) {
335 return [ {id: 42, a: 'foo'} ];
337 throw new Error(JSON.stringify(_.toArray(arguments)));
339 mock('demo:default_get', function () { return {}; });
340 mock('demo:create', function () { return 1; });
341 mock('demo:on_write', function () { return [42]; });
343 var ds = new instance.web.DataSetStatic(null, 'demo', null, []);
344 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
345 return l.appendTo($fix)
346 .then(l.proxy('reload_content'))
348 return l.start_edition();
351 $fix.find('.oe_form_field input').val("some value").change();
354 return l.save_edition();
357 strictEqual(ds.ids.length, 2,
358 'should have id of created + on_write');
359 strictEqual(l.records.length, 2,
360 'should have record of created + on_write');
362 $fix.find('tbody tr:eq(1)').css('color'), 'rgb(255, 0, 0)',
363 'shoud have color applied');
365 $fix.find('tbody tr:eq(2)').css('color'), 'rgb(255, 0, 0)',
366 'should have default color applied');