From 1b08fa43abc046b3952f2c7c636931cdef4f0adf Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Fri, 24 Mar 2017 16:25:37 +0100 Subject: [PATCH] [ADD] web: add label_selection widget tests and documentation --- .../web/static/src/js/fields/basic_fields.js | 19 ++- .../static/test/fields/basic_fields_tests.js | 121 ++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index e62eaf7b8e3..e5950a5623d 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1259,10 +1259,25 @@ var StateSelectionWidget = AbstractField.extend({ }); var LabelSelection = AbstractField.extend({ + readonly: true, + supportedFieldTypes: ['selection'], + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * This widget renders a simple non-editable label. Color classes can be set + * using the 'classes' key from the options tag, such as: + * + * + * @private + * @override + */ _render: function () { this.classes = this.nodeOptions && this.nodeOptions.classes || {}; - var lbl_class = this.classes[this.value] || 'primary'; - this.$el.addClass('label label-' + lbl_class).text(this._formatValue(this.value)); + var labelClass = this.classes[this.value] || 'primary'; + this.$el.addClass('label label-' + labelClass).text(this._formatValue(this.value)); }, }); diff --git a/addons/web/static/test/fields/basic_fields_tests.js b/addons/web/static/test/fields/basic_fields_tests.js index d489db97572..b57c6246b5f 100644 --- a/addons/web/static/test/fields/basic_fields_tests.js +++ b/addons/web/static/test/fields/basic_fields_tests.js @@ -2081,6 +2081,127 @@ QUnit.module('basic_fields', { }); + QUnit.module('LabelSelectionWidget'); + + QUnit.test('label_selection widget in form view', function (assert) { + assert.expect(12); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + }); + + assert.strictEqual(form.$('.o_form_field.label.label-warning').length, 1, + "should have a warning status label since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-default').length, 0, + "should not have a default status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-success').length, 0, + "should not have a success status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-warning').text(), 'Blocked', + "the label should say 'Blocked' since this is the label value for that state"); + + // // switch to edit mode and check the result + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_form_field.label.label-warning').length, 1, + "should have a warning status label since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-default').length, 0, + "should not have a default status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-success').length, 0, + "should not have a success status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-warning').text(), 'Blocked', + "the label should say 'Blocked' since this is the label value for that state"); + + // save + form.$buttons.find('.o_form_button_save').click(); + assert.strictEqual(form.$('.o_form_field.label.label-warning').length, 1, + "should have a warning status label since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-default').length, 0, + "should not have a default status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-success').length, 0, + "should not have a success status since selection is the second, blocked state"); + assert.strictEqual(form.$('.o_form_field.label.label-warning').text(), 'Blocked', + "the label should say 'Blocked' since this is the label value for that state"); + + form.destroy(); + }); + + QUnit.test('label_selection widget in editable list view', function(assert) { + assert.expect(21); + + var list = createView({ + View: ListView, + model: 'partner', + data: this.data, + arch: '' + + '' + + '' + + '', + }); + + assert.strictEqual(list.$('.o_field_widget.label:not(:empty)').length, 3, + "should have three visible status labels"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').length, 1, + "should have one warning status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').text(), 'Blocked', + "the warning label should read 'Blocked'"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').length, 1, + "should have one default status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').text(), 'Normal', + "the default label should read 'Normal'"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').length, 1, + "should have one success status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').text(), 'Done', + "the success label should read 'Done'"); + + // switch to edit mode and check the result + list.$('tbody td:not(.o_list_record_selector)').first().click(); + assert.strictEqual(list.$('.o_field_widget.label:not(:empty)').length, 3, + "should have three visible status labels"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').length, 1, + "should have one warning status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').text(), 'Blocked', + "the warning label should read 'Blocked'"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').length, 1, + "should have one default status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').text(), 'Normal', + "the default label should read 'Normal'"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').length, 1, + "should have one success status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').text(), 'Done', + "the success label should read 'Done'"); + + // save and check the result + list.$buttons.find('.o_list_button_save').click(); + assert.strictEqual(list.$('.o_field_widget.label:not(:empty)').length, 3, + "should have three visible status labels"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').length, 1, + "should have one warning status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-warning').text(), 'Blocked', + "the warning label should read 'Blocked'"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').length, 1, + "should have one default status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-default').text(), 'Normal', + "the default label should read 'Normal'"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').length, 1, + "should have one success status label"); + assert.strictEqual(list.$('.o_field_widget.label.label-success').text(), 'Done', + "the success label should read 'Done'"); + + list.destroy(); + }); + + QUnit.module('FieldDomain'); QUnit.test('basic domain field usage is ok', function (assert) {