From ca4b39e8a536c1607f4fdfd02df23a599240a0e7 Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Mon, 14 Dec 2015 15:40:18 +0100 Subject: [PATCH] fix issue on form view when: quick edit the value, them move to edit mode the form, change the value then cancel the change, the value was keeping the initial values without taking account the quick edition. find by @JonathanNEMRY --- .../src/js/web_widget_boolean_switch.js | 1 + .../static/test/web_widget_boolean_switch.js | 106 ++++++++++++++---- 2 files changed, 87 insertions(+), 20 deletions(-) diff --git a/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js b/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js index d789adc4..82191e25 100644 --- a/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js +++ b/web_widget_boolean_switch/static/src/js/web_widget_boolean_switch.js @@ -81,6 +81,7 @@ openerp.web_widget_boolean_switch = function(instance){ model.call('write', [[id], values], {'context': this.build_context()}); this.internal_set_value(state, {'silent': true}); + this.getParent().datarecord[this.name] = state; } }, this)); this.on("change:readonly", this, this.switcher_states); diff --git a/web_widget_boolean_switch/static/test/web_widget_boolean_switch.js b/web_widget_boolean_switch/static/test/web_widget_boolean_switch.js index fa7e9810..645a597a 100644 --- a/web_widget_boolean_switch/static/test/web_widget_boolean_switch.js +++ b/web_widget_boolean_switch/static/test/web_widget_boolean_switch.js @@ -1,26 +1,92 @@ -openerp.testing.section('web_widget_boolean_switch', +openerp.testing.section('BooleanSwitchWidget.behaviors', + {dependences: ['web.web_widget_boolean_switch'], + rpc: 'mock', + templates: true, + }, function(test){ + test('BooleanSwitchWidget quick edit - enter in edit mode - cancel', + {asserts: 30}, function (instance, $fix, mock) { + + mock('demo:read', function () { + console.log("read data"); + return [{ id: 1, a: true, b: 'bar', c: 'baz' }]; + }); + mock('demo:fields_view_get', function () { + return { + type: 'form', + fields: { + a: {type: 'boolean', string: "A"}, + b: {type: 'char', string: "B"}, + c: {type: 'char', string: "C"} + }, + arch: '
' + + ' ' + + ' ' + + ' ' + + '', + }; + }); + var ds = new instance.web.DataSetStatic(null, 'demo', null, [1]); + ds.index = 0; + var form = new instance.web.FormView({}, ds, false, { + initial_mode: 'view', + disable_autofocus: false, + $buttons: $(), + $pager: $() + }); + var numTest = 1; + form.appendTo($fix); + var promise = form.do_show(); + check_values(form.$el.find(".oe_form_field_boolean_switch"), + true, false, false, + numTest++ + " - Init state: quick editable with true value"); + form.fields.a.switcher.checkboxes.click(); + check_values(form.$el.find('.oe_form_field_boolean_switch'), + false, false, false, + numTest++ + " - Quick edit to `false` value"); + form.to_edit_mode(); + check_values(form.$el.find('.oe_form_field_boolean_switch'), + false, false, false, + numTest++ + " - Move to edit mode value still false"); + form.fields.a.switcher.checkboxes.click(); + check_values(form.$el.find('.oe_form_field_boolean_switch'), + true, false, false, + numTest++ + " - edit to True"); + // remove class oe_form_dirty to avoid popup in unit test while canceling + form.$el.removeClass('oe_form_dirty'); + form.on_button_cancel.call(form); + check_values(form.$el.find('.oe_form_field_boolean_switch'), + false, false, false, + numTest++ + " - Cancel edit mode return still False"); + return promise; + + }); + +}); + +var check_values = function (scratchpad, value, readonly, disabled, + message){ + var $container = scratchpad.children(); + var $input = $container.find('input'); + strictEqual($input[0].checked, value, message + " - Input value"); + strictEqual($input[0].readOnly && $input[0].readOnly ? true : false, + readonly, message + " - Input readonly"); + strictEqual($input[0].disabled && $input[0].disabled ? true : false, disabled, + message + " - Input disabled"); + + var prefix = 'bootstrap-switch-'; + ok($container[0].classList.contains(prefix + (value ? 'on' : 'off')), + message + " - Bootstrap-switch value class"); + strictEqual($container[0].classList.contains(prefix + "readonly"), + readonly, message + " - Bootstrap-switch readonly class"); + strictEqual($container[0].classList.contains(prefix + "disabled"), + disabled, message + " - Bootstrap-switch disabled class"); + +}; + +openerp.testing.section('BooleanSwitchWidget.tests', {'dependences': ['web.web_widget_boolean_switch'], }, function(test){ "use strict"; - var check_values = function (scratchpad, value, readonly, disabled, - message){ - var $container = scratchpad.children(); - var $input = $container.find('input'); - strictEqual($input[0].checked, value, message + " - Input value"); - strictEqual($input[0].readOnly && $input[0].readOnly ? true : false, - readonly, message + " - Input readonly"); - strictEqual($input[0].disabled && $input[0].disabled ? true : false, disabled, - message + " - Input disabled"); - - var prefix = 'bootstrap-switch-'; - ok($container[0].classList.contains(prefix + (value ? 'on' : 'off')), - message + " - Bootstrap-switch value class"); - strictEqual($container[0].classList.contains(prefix + "readonly"), - readonly, message + " - Bootstrap-switch readonly class"); - strictEqual($container[0].classList.contains(prefix + "disabled"), - disabled, message + " - Bootstrap-switch disabled class"); - - }; var init_check_values = function(instance, scratchpad, html, options, checked, readonly, disabled, message){