From 53726d093fb54c18362d0ab36b1fe41147c7aeb2 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Mon, 27 Mar 2017 14:31:57 +0200 Subject: [PATCH] [ADD] web: add statinfo widget tests and documentation --- .../web/static/src/js/fields/basic_fields.js | 29 +++- .../static/test/fields/basic_fields_tests.js | 157 ++++++++++++++++++ 2 files changed, 183 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index e5950a5623d..c16020b4b65 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1345,6 +1345,32 @@ var FieldBooleanButton = AbstractField.extend({ var StatInfo = AbstractField.extend({ supportedFieldTypes: ['integer', 'float'], + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * StatInfo widgets are always set since they basically only display info. + * + * @override + */ + isSet: function () { + return true; + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Renders the field value using the StatInfo template. The text part of the + * widget is either the string attribute of this node in the view or the + * label of the field itself if no string attribute is given. + * + * @override + * @private + */ _render: function () { var options = { value: this.value || 0, @@ -1359,9 +1385,6 @@ var StatInfo = AbstractField.extend({ this.$el.html(qweb.render("StatInfo", options)); this.$el.addClass('o_stat_info'); }, - isSet: function () { - return true; - }, }); var FieldPercentPie = AbstractField.extend({ diff --git a/addons/web/static/test/fields/basic_fields_tests.js b/addons/web/static/test/fields/basic_fields_tests.js index b57c6246b5f..afcc04aa693 100644 --- a/addons/web/static/test/fields/basic_fields_tests.js +++ b/addons/web/static/test/fields/basic_fields_tests.js @@ -2202,6 +2202,163 @@ QUnit.module('basic_fields', { }); + QUnit.module('StatInfo'); + + QUnit.test('statinfo widget in form view', function (assert) { + assert.expect(9); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '
' + + '' + + '
' + + '' + + '' + + '' + + '
' + + '
', + res_id: 1, + }); + + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'int_field', "should have 'int_field' as text"); + + // switch to edit mode and check the result + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should still have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should still have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'int_field', "should have 'int_field' as text"); + + // save + form.$buttons.find('.o_form_button_save').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'int_field', "should have 'int_field' as text"); + + form.destroy(); + }); + + QUnit.test('statinfo widget in form view with specific label_field', function (assert) { + assert.expect(9); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '
' + + '' + + '
' + + '' + + '' + + '' + + '' + + '
' + + '
', + res_id: 1, + }); + + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'yop', "should have 'yop' as text, since it is the value of field foo"); + + // switch to edit mode and check the result + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should still have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should still have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'yop', "should have 'yop' as text, since it is the value of field foo"); + + // save + form.$buttons.find('.o_form_button_save').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + 'yop', "should have 'yop' as text, since it is the value of field foo"); + + form.destroy(); + }); + + QUnit.test('statinfo widget in form view with no label', function (assert) { + assert.expect(9); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '
' + + '' + + '
' + + '' + + '' + + '' + + '' + + '
' + + '
', + res_id: 1, + }); + + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + '', "should not have any label"); + + // switch to edit mode and check the result + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should still have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should still have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + '', "should not have any label"); + + // save + form.$buttons.find('.o_form_button_save').click(); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info').length, 1, + "should have one stat button"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_value').text(), + '10', "should have 10 as value"); + assert.strictEqual(form.$('.oe_stat_button .o_form_field.o_stat_info .o_stat_text').text(), + '', "should not have any label"); + + form.destroy(); + }); + + QUnit.module('FieldDomain'); QUnit.test('basic domain field usage is ok', function (assert) {