1 openerp.testing.section('editor', {
2 dependencies: ['web.list_editable'],
5 setup: function (instance, $s, mock) {
6 mock('test.model:create', function () {
9 mock('test.model:onchange', function () {
12 mock('test.model:default_get', function () {
19 * @param {String} name
20 * @param {Object} [attrs]
21 * @param {String} [attrs.type="char"]
22 * @param {Boolean} [attrs.required]
23 * @param {Boolean} [attrs.invisible]
24 * @param {Boolean} [attrs.readonly]
27 function field(name, attrs) {
30 return _.defaults(attrs, {
36 * @param {Array} [fields]
39 function makeFormView(fields) {
41 _(fields).each(function (field) {
47 var children = _(fields).map(function (field) {
52 modifiers: JSON.stringify({
53 required: field.required,
54 invisible: field.invisible,
55 readonly: field.readonly
65 'class': 'oe_form_container'
73 test('base-state', {asserts: 2}, function (instance, $fix) {
74 var e = new instance.web.list.Editor({
76 edition_view: function () {
77 return makeFormView();
80 return e.appendTo($fix)
82 ok(!e.is_editing(), "should not be editing");
83 ok(e.form instanceof instance.web.FormView,
84 "should use default form type");
87 test('toggle-edition-save', {
89 setup: function (instance, $s, mock) {
90 mock('test.model:search_read', function () {
91 return [{id: 42, a: false, b: false, c: false}];
94 }, function (instance, $fix) {
95 var e = new instance.web.list.Editor({
96 dataset: new instance.web.DataSetSearch(null, 'test.model'),
97 prepends_on_create: function () { return false; },
98 edition_view: function () {
99 return makeFormView([ field('a'), field('b'), field('c') ]);
103 return e.appendTo($fix)
105 return e.edit({}, function () {
109 .then(function (form) {
110 ok(e.is_editing(), "should be editing");
111 equal(counter, 3, "should have configured all fields");
114 .done(function (record) {
115 ok(!e.is_editing(), "should have stopped editing");
116 equal(record.id, 42, "should have newly created id");
119 test('toggle-edition-cancel', { asserts: 2 }, function (instance, $fix) {
120 var e = new instance.web.list.Editor({
121 dataset: new instance.web.DataSetSearch(null, 'test.model'),
122 prepends_on_create: function () { return false; },
123 edition_view: function () {
124 return makeFormView([ field('a'), field('b'), field('c') ]);
128 return e.appendTo($fix)
130 return e.edit({}, function () {
134 .then(function (form) {
137 .done(function (record) {
138 ok(!e.is_editing(), "should have stopped editing");
139 ok(!record.id, "should have no id");
142 test('toggle-save-required', {
144 fail_on_rejection: false
145 }, function (instance, $fix) {
146 var e = new instance.web.list.Editor({
147 do_warn: function () {
150 dataset: new instance.web.DataSetSearch(null, 'test.model'),
151 prepends_on_create: function () { return false; },
152 edition_view: function () {
153 return makeFormView([
154 field('a', {required: true}), field('b'), field('c') ]);
159 return e.appendTo($fix)
161 return e.edit({}, function () {
165 .then(function (form) {
168 .done(function () { ok(false, "cancel should not succeed"); })
170 equal(warnings, 1, "should have been warned");
171 ok(e.is_editing(), "should have kept editing");
175 openerp.testing.section('list.edition', {
176 dependencies: ['web.list_editable'],
179 setup: function (instance, $s, mock) {
181 mock('demo:create', function (args) {
182 records[42] = _.extend({}, args[0]);
185 mock('demo:read', function (args) {
188 return [records[id]];
192 mock('demo:search_read', function (args) {
193 // args[0][0] = ["id", "=", 42]
195 var id = args[0][0][2];
197 return [records[id]];
201 mock('demo:fields_view_get', function () {
205 a: {type: 'char', string: "A"},
206 b: {type: 'char', string: "B"},
207 c: {type: 'char', string: "C"}
209 arch: '<tree><field name="a"/><field name="b"/><field name="c"/></tree>',
212 mock('demo:onchange', function () {
217 test('newrecord', {asserts: 7}, function (instance, $fix, mock) {
218 var got_defaults = false;
219 mock('demo:default_get', function (args) {
220 var fields = args[0];
222 fields, ['a', 'b', 'c'],
223 "should ask defaults for all fields");
225 return { a: "qux", b: "quux" };
228 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
229 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
231 return l.appendTo($fix)
232 .then(l.proxy('reload_content'))
234 return l.start_edition();
237 ok(got_defaults, "should have fetched default values for form");
239 return l.save_edition();
241 .then(function (result) {
242 ok(result.created, "should yield newly created record");
243 equal(result.record.get('a'), "qux",
244 "should have used default values");
245 equal(result.record.get('b'), "quux",
246 "should have used default values");
247 ok(!result.record.get('c'),
248 "should have no value if there was no default");
252 openerp.testing.section('list.edition.events', {
253 dependencies: ['web.list_editable'],
256 setup: function (instance, $s, mock) {
257 mock('demo:read', function () {
258 return [{ id: 1, a: 'foo', b: 'bar', c: 'baz' }];
260 mock('demo:fields_view_get', function () {
264 a: {type: 'char', string: "A"},
265 b: {type: 'char', string: "B"},
266 c: {type: 'char', string: "C"}
268 arch: '<tree><field name="a"/><field name="b"/><field name="c"/></tree>',
273 test('edition events', {asserts: 4}, function (instance, $fix) {
274 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
277 onEvent: function (e) { this.counter++; }
279 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
280 l.on('edit:before edit:after', o, o.onEvent);
281 return l.appendTo($fix)
282 .then(l.proxy('reload_content'))
284 ok(l.options.editable, "should be editable");
285 equal(o.counter, 0, "should have seen no event yet");
286 return l.start_edition(l.records.get(1));
289 ok(l.editor.is_editing(), "should be editing");
290 equal(o.counter, 2, "should have seen two edition events");
294 test('edition events: cancelling', {asserts: 3}, function (instance, $fix) {
295 var edit_after = false;
296 var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]);
297 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
298 l.on('edit:before', {}, function (e) {
301 l.on('edit:after', {}, function () {
304 return l.appendTo($fix)
305 .then(l.proxy('reload_content'))
307 ok(l.options.editable, "should be editable");
308 return l.start_edition();
310 // cancelling an event rejects the deferred
311 .then($.Deferred().reject(), function () {
312 ok(!l.editor.is_editing(), "should not be editing");
313 ok(!edit_after, "should not have fired the edit:after event");
319 openerp.testing.section('list.edition.onwrite', {
320 dependencies: ['web.list_editable'],
323 setup: function (instance, $s, mock) {
324 mock('demo:onchange', function () {
329 test('record-to-read', {asserts: 4}, function (instance, $fix, mock) {
330 mock('demo:fields_view_get', function () {
334 a: {type: 'char', string: "A"}
336 arch: '<tree on_write="on_write" colors="red:a == \'foo\'"><field name="a"/></tree>',
339 mock('demo:read', function (args, kwargs) {
340 if (_.isEmpty(args[0])) {
343 throw new Error(JSON.stringify(_.toArray(arguments)));
345 mock('demo:search_read', function (args, kwargs) {
346 if (_.isEqual(args[0], [['id', 'in', [1]]])) {
347 return [{id: 1, a: 'some value'}];
348 } else if (_.isEqual(args[0], [['id', 'in', [42]]])) {
349 return [ {id: 42, a: 'foo'} ];
351 throw new Error(JSON.stringify(_.toArray(arguments)));
353 mock('demo:default_get', function () { return {}; });
354 mock('demo:create', function () { return 1; });
355 mock('demo:on_write', function () { return [42]; });
357 var ds = new instance.web.DataSetStatic(null, 'demo', null, []);
358 var l = new instance.web.ListView({}, ds, false, {editable: 'top'});
359 return l.appendTo($fix)
360 .then(l.proxy('reload_content'))
362 return l.start_edition();
365 $fix.find('.oe_form_field input').val("some value").change();
368 return l.save_edition();
371 strictEqual(ds.ids.length, 2,
372 'should have id of created + on_write');
373 strictEqual(l.records.length, 2,
374 'should have record of created + on_write');
376 $fix.find('tbody tr:eq(1)').css('color'), 'rgb(255, 0, 0)',
377 'shoud have color applied');
379 $fix.find('tbody tr:eq(2)').css('color'), 'rgb(255, 0, 0)',
380 'should have default color applied');