From d218902af5babfe371d0cde5c7851282759b4899 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Tue, 14 Mar 2017 11:02:45 +0100 Subject: [PATCH] [ADD] web: add email widget tests and documentation --- .../web/static/src/js/fields/basic_fields.js | 17 ++++ .../static/test/fields/basic_fields_tests.js | 77 +++++++++++++++++-- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index c78b36f907c..f168d8cc69f 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -590,10 +590,27 @@ var HandleWidget = AbstractField.extend({ var EmailWidget = InputField.extend({ prefix: 'mailto', supportedFieldTypes: ['char'], + + /** + * In readonly, emails should be a link, not a span. + * + * @override + */ init: function () { this._super.apply(this, arguments); this.tagName = this.mode === 'readonly' ? 'a' : 'input'; }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * In readonly, emails should be a mailto: link with proper formatting. + * + * @override + * @private + */ _renderReadonly: function () { this.$el.text(this.value) .addClass('o_form_uri o_text_overflow') diff --git a/addons/web/static/test/fields/basic_fields_tests.js b/addons/web/static/test/fields/basic_fields_tests.js index 32f327bc7b3..c4d1fa32d50 100644 --- a/addons/web/static/test/fields/basic_fields_tests.js +++ b/addons/web/static/test/fields/basic_fields_tests.js @@ -351,9 +351,8 @@ QUnit.module('basic_fields', { QUnit.module('EmailWidget'); - - QUnit.test('email widget works correctly', function (assert) { - assert.expect(1); + QUnit.test('email field in form view', function (assert) { + assert.expect(7); var form = createView({ View: FormView, @@ -369,11 +368,79 @@ QUnit.module('basic_fields', { res_id: 1, }); - assert.strictEqual(form.$('a.o_form_uri.o_form_field.o_text_overflow').length, 1, - "should have a anchor with correct classes"); + var $mailtoLink = form.$('a.o_form_uri.o_form_field.o_text_overflow'); + assert.strictEqual($mailtoLink.length, 1, + "should have a anchor with correct classes"); + assert.strictEqual($mailtoLink.text(), 'yop', + "the value should be displayed properly"); + assert.strictEqual($mailtoLink.attr('href'), 'mailto:yop', + "should have proper mailto prefix"); + + // switch to edit mode and check the result + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('input[type="text"].o_form_input.o_form_field').length, 1, + "should have an input for the email field"); + assert.strictEqual(form.$('input[type="text"].o_form_input.o_form_field').val(), 'yop', + "input should contain field value in edit mode"); + + // change value in edit mode + form.$('input[type="text"].o_form_input.o_form_field').val('new').trigger('input'); + + // save + form.$buttons.find('.o_form_button_save').click(); + $mailtoLink = form.$('a.o_form_uri.o_form_field.o_text_overflow'); + assert.strictEqual($mailtoLink.text(), 'new', + "new value should be displayed properly"); + assert.strictEqual($mailtoLink.attr('href'), 'mailto:new', + "should still have proper mailto prefix"); + form.destroy(); }); + QUnit.test('email field in editable list view', function(assert) { + assert.expect(10); + + var list = createView({ + View: ListView, + model: 'partner', + data: this.data, + arch: '', + }); + + assert.strictEqual(list.$('tbody td:not(.o_list_record_selector)').length, 5, + "should have 5 cells"); + assert.strictEqual(list.$('tbody td:not(.o_list_record_selector)').first().text(), 'yop', + "value should be displayed properly as text"); + + var $mailtoLink = list.$('a.o_form_uri.o_field_widget.o_text_overflow'); + assert.strictEqual($mailtoLink.length, 5, + "should have anchors with correct classes"); + assert.strictEqual($mailtoLink.first().attr('href'), 'mailto:yop', + "should have proper mailto prefix"); + + // Edit a line and check the result + var $cell = list.$('tbody td:not(.o_list_record_selector)').first(); + $cell.click(); + assert.ok($cell.hasClass('o_edit_mode'), 'should be set as edit mode'); + assert.strictEqual($cell.find('input').val(), 'yop', + 'should have the corect value in internal input'); + $cell.find('input').val('new').trigger('input');; + + // save + list.$buttons.find('.o_list_button_save').click(); + $cell = list.$('tbody td:not(.o_list_record_selector)').first(); + assert.ok(!$cell.hasClass('o_edit_mode'), 'should not be in edit mode anymore'); + assert.strictEqual(list.$('tbody td:not(.o_list_record_selector)').first().text(), 'new', + "value should be properly updated"); + $mailtoLink = list.$('a.o_form_uri.o_field_widget.o_text_overflow'); + assert.strictEqual($mailtoLink.length, 5, + "should still have anchors with correct classes"); + assert.strictEqual($mailtoLink.first().attr('href'), 'mailto:new', + "should still have proper mailto prefix"); + + list.destroy(); + }); + QUnit.module('FieldChar');