From 1fa8f679264b7b93b34991f40faabb25875884ed Mon Sep 17 00:00:00 2001 From: Dipalee Bhalodia Date: Wed, 21 Mar 2018 11:55:00 +0530 Subject: [PATCH] [FIX] mail: click on unstar all button should reset the starredcounter Before this commit: Clicking on 'unstar all' button removes all messages from starred channel but it only decreases the counter as many as there are displayed messages on screen. For example, if there are 40 messages starred, only 30 messages are on screen (default limit) but unstar all will decrease counter only for those messages instead of 40. After this commit: clicking on 'unstar all' button will correctly take in account all fetched messages and not only displayed messages. So now, after clicking on 'unstar all' button will update the starred counter to 0. task-ID: 33958 --- .../static/src/js/services/chat_manager.js | 9 +- addons/mail/static/tests/discuss_tests.js | 104 ++++++++++++++++++ .../mail/static/tests/helpers/mock_server.js | 17 ++- .../mail/static/tests/helpers/test_utils.js | 6 + 4 files changed, 133 insertions(+), 3 deletions(-) diff --git a/addons/mail/static/src/js/services/chat_manager.js b/addons/mail/static/src/js/services/chat_manager.js index 8ae45486e16..ec8e4c64299 100644 --- a/addons/mail/static/src/js/services/chat_manager.js +++ b/addons/mail/static/src/js/services/chat_manager.js @@ -1582,16 +1582,21 @@ var ChatManager = AbstractService.extend({ message.is_starred = data.starred; if (!message.is_starred) { self._removeMessageFromChannel("channel_starred", message); - self.starredCounter--; } else { self._addToCache(message, []); var channelStarred = self.getChannel('channel_starred'); channelStarred.cache = _.pick(channelStarred.cache, "[]"); - self.starredCounter++; } self.chatBus.trigger('update_message', message); } }); + + if (data.starred) { // increase starred counter if message is marked as star + this.starredCounter += data.message_ids.length; + } else { // decrease starred counter if message is remove from star if unstar_all then it will set to 0. + this.starredCounter -= data.message_ids.length; + } + this.chatBus.trigger('update_starred', this.starredCounter); }, /** diff --git a/addons/mail/static/tests/discuss_tests.js b/addons/mail/static/tests/discuss_tests.js index 862b643abb4..af600ef3087 100644 --- a/addons/mail/static/tests/discuss_tests.js +++ b/addons/mail/static/tests/discuss_tests.js @@ -375,5 +375,109 @@ QUnit.test('older messages are loaded on scroll', function (assert) { }); }); +QUnit.test('"Unstar all" button should reset the starred counter', function (assert) { + assert.expect(2); + var done = assert.async(); + + var bus = new Bus(); + var BusService = createBusService(bus); + var msgData = []; + _.each(_.range(1, 41), function (num) { + msgData.push({ + id: num, + body: "

test" + num + "

", + author_id: ["1", "Me"], + channel_ids: [1], + starred: true, + starred_partner_ids: [1], + } + ); + }); + + this.data = { + initMessaging: { + channel_slots: { + channel_channel: [{ + id: 1, + channel_type: "channel", + name: "general", + }], + }, + starred_counter: msgData.length, + }, + 'mail.message': { + fields: { + body: { + string: "Contents", + type: 'html', + }, + author_id: { + string: "Author", + relation: 'res.partner', + }, + channel_ids: { + string: "Channels", + type: 'many2many', + relation: 'mail.channel', + }, + starred: { + string: "Starred", + type: 'boolean', + }, + needaction: { + string: "Need Action", + type: 'boolean', + }, + starred_partner_ids: { + string: "partner ids", + type: 'integer', + } + }, + records: msgData, + }, + }; + + createDiscuss({ + id: 1, + context: {}, + params: {}, + data: this.data, + services: [ChatManager, BusService], + mockRPC: function (route, args) { + if (args.method === 'unstar_all') { + var data = { + message_ids: _.range(1, 41), + starred: false, + type: 'toggle_star', + }; + var notification = [[false, 'res.partner'], data]; + bus.trigger('notification', [notification]); + return $.when(42); + } + return this._super.apply(this, arguments); + }, + session: {partner_id: 1}, + }) + .then(function (discuss) { + var $starred = discuss.$('.o_mail_chat_sidebar').find('.o_mail_chat_title_starred'); + var $starredCounter = $('.o_mail_chat_title_starred > .o_mail_sidebar_needaction'); + + // Go to Starred channel + $starred.click(); + // Test Initial Value + assert.strictEqual($starredCounter.text().trim(), "40", "40 messages should be starred"); + + // Unstar all and wait 'update_starred' + $('.o_control_panel .o_mail_chat_button_unstar_all').click(); + $starredCounter = $('.o_mail_chat_title_starred > .o_mail_sidebar_needaction'); + assert.strictEqual($starredCounter.text().trim(), "0", + "All messages should be unstarred"); + + discuss.destroy(); + done(); + }); +}); + + }); }); diff --git a/addons/mail/static/tests/helpers/mock_server.js b/addons/mail/static/tests/helpers/mock_server.js index 01ce78221f0..98c5a4eb375 100644 --- a/addons/mail/static/tests/helpers/mock_server.js +++ b/addons/mail/static/tests/helpers/mock_server.js @@ -39,6 +39,21 @@ MockServer.include({ 'menu_id': false, }); }, + + /** + * Simulate the 'message_fetch' Python method + * + * @return {Object} + */ + _mockMessageFetch: function (args) { + args.kwargs.offset = Math.max(this.data['mail.message'].records.length - args.kwargs.limit, 0); + + return this._mockSearchRead('mail.message', [ + args.args[0], + _.keys(this.data['mail.message'].fields), + ], args.kwargs); + }, + /** * @override */ @@ -47,7 +62,7 @@ MockServer.include({ return $.when(this._mockInitMessaging(args)); } if (args.method === 'message_fetch') { - return $.when([]); + return $.when(this._mockMessageFetch(args)); } if (args.method === 'channel_fetch_listeners') { return $.when([]); diff --git a/addons/mail/static/tests/helpers/test_utils.js b/addons/mail/static/tests/helpers/test_utils.js index cd933bc2483..f7292c7a67e 100644 --- a/addons/mail/static/tests/helpers/test_utils.js +++ b/addons/mail/static/tests/helpers/test_utils.js @@ -5,6 +5,7 @@ var Discuss = require('mail.chat_discuss'); var AbstractService = require('web.AbstractService'); var Bus = require('web.Bus'); +var ControlPanel = require('web.ControlPanel'); var testUtils = require('web.test_utils'); var Widget = require('web.Widget'); @@ -75,6 +76,8 @@ function createDiscuss(params) { var discuss = new Discuss(parent, params); discuss.set_cp_bus(new Widget()); var selector = params.debug ? 'body' : '#qunit-fixture'; + var controlPanel = new ControlPanel(parent); + controlPanel.appendTo($(selector)); discuss.appendTo($(selector)); // override 'destroy' of discuss so that it calls 'destroy' on the parent @@ -86,6 +89,9 @@ function createDiscuss(params) { parent.destroy(); }; + // link the view to the control panel + discuss.set_cp_bus(controlPanel.get_bus()); + return discuss.call('chat_manager', 'isReady').then(function () { return discuss; });