From 978f8ca4ef6bbae25589976e83c63000a127c1c6 Mon Sep 17 00:00:00 2001 From: Xavier Dubuc Date: Tue, 14 Jul 2020 11:24:22 +0000 Subject: [PATCH] [FIX] mail: fix moderation accept multiple messages button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Once global "Accept" button is clicked, the accepted messages were not unchecked causing the button to stay hidden until next page reload. task-2285430 closes odoo/odoo#54549 X-original-commit: 5594de16f463507498e852c2d49d1cbe74ed7381 Signed-off-by: Alexandre Kühn (aku) --- .../discuss/tests/discuss_moderation_tests.js | 180 ++++++++++++++++++ .../src/models/thread_cache/thread_cache.js | 15 ++ 2 files changed, 195 insertions(+) diff --git a/addons/mail/static/src/components/discuss/tests/discuss_moderation_tests.js b/addons/mail/static/src/components/discuss/tests/discuss_moderation_tests.js index adda97827b5..2a0f5679349 100644 --- a/addons/mail/static/src/components/discuss/tests/discuss_moderation_tests.js +++ b/addons/mail/static/src/components/discuss/tests/discuss_moderation_tests.js @@ -1227,6 +1227,186 @@ QUnit.test('new pending moderation message posted by someone else when moderator ); }); +QUnit.test('accept multiple moderation messages', async function (assert) { + assert.expect(5); + + Object.assign(this.data.initMessaging, { + channel_slots: { + channel_channel: [{ + channel_type: 'channel', + id: 20, + is_pinned: true, + moderation: true, + name: "general", + }], + }, + is_moderator: true, + moderation_channel_ids: [20], + moderation_counter: 3, + }); + this.data['mail.message'].records = [{ + author_id: [2, "Someone"], + body: "

test

", + id: 100, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }, { + author_id: [2, "Someone"], + body: "

test 2

", + id: 101, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }, { + author_id: [2, "Someone"], + body: "

test 3

", + id: 102, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }]; + + await this.start({ + discuss: { + params: { + default_active_id: 'mail.box_moderation', + }, + }, + }); + + assert.containsN( + document.body, + '.o_Message', + 3, + "should initially display 3 messages" + ); + + await afterNextRender(() => { + document.querySelectorAll('.o_Message_checkbox')[0].click(); + document.querySelectorAll('.o_Message_checkbox')[1].click(); + }); + assert.containsN( + document.body, + '.o_Message_checkbox:checked', + 2, + "2 messages should have been checked after clicking on their respective checkbox" + ); + assert.doesNotHaveClass( + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept'), + 'o_hidden', + "global accept button should be displayed as two messages are selected" + ); + + await afterNextRender(() => + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept').click() + ); + assert.containsN( + document.body, + '.o_Message', + 1, + "should display 1 message as the 2 others have been accepted" + ); + assert.hasClass( + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept'), + 'o_hidden', + "global accept button should no longer be displayed as messages have been unselected" + ); +}); + +QUnit.test('accept multiple moderation messages after having accepted other messages', async function (assert) { + assert.expect(5); + + Object.assign(this.data.initMessaging, { + channel_slots: { + channel_channel: [{ + channel_type: 'channel', + id: 20, + is_pinned: true, + moderation: true, + name: "general", + }], + }, + is_moderator: true, + moderation_channel_ids: [20], + moderation_counter: 3, + }); + this.data['mail.message'].records = [{ + author_id: [2, "Someone"], + body: "

test

", + id: 100, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }, { + author_id: [2, "Someone"], + body: "

test 2

", + id: 101, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }, { + author_id: [2, "Someone"], + body: "

test 3

", + id: 102, + model: 'mail.channel', + moderation_status: 'pending_moderation', + need_moderation: true, + res_id: 20, + }]; + + await this.start({ + discuss: { + params: { + default_active_id: 'mail.box_moderation', + }, + }, + }); + assert.containsN( + document.body, + '.o_Message', + 3, + "should initially display 3 messages" + ); + await afterNextRender(() => { + document.querySelectorAll('.o_Message_checkbox')[0].click(); + }); + await afterNextRender(() => + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept').click() + ); + + await afterNextRender(() => document.querySelectorAll('.o_Message_checkbox')[0].click()); + assert.containsOnce( + document.body, + '.o_Message_checkbox:checked', + "a message should have been checked after clicking on its checkbox" + ); + assert.doesNotHaveClass( + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept'), + 'o_hidden', + "global accept button should be displayed as a message is selected" + ); + + await afterNextRender(() => + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept').click() + ); + assert.containsOnce( + document.body, + '.o_Message', + "should display only one message left after the two others has been accepted" + ); + assert.hasClass( + document.querySelector('.o_widget_Discuss_controlPanelButtonModeration.o-accept'), + 'o_hidden', + "global accept button should no longer be displayed as message has been unselected" + ); +}); + }); }); }); diff --git a/addons/mail/static/src/models/thread_cache/thread_cache.js b/addons/mail/static/src/models/thread_cache/thread_cache.js index b942e0a6e08..4fdcaed6114 100644 --- a/addons/mail/static/src/models/thread_cache/thread_cache.js +++ b/addons/mail/static/src/models/thread_cache/thread_cache.js @@ -94,6 +94,16 @@ function factory(dependencies) { // Private //---------------------------------------------------------------------- + /** + * @private + */ + _computeCheckedMessages() { + const messagesWithoutCheckbox = this.checkedMessages.filter( + message => !message.hasCheckbox + ); + return [['unlink', messagesWithoutCheckbox]]; + } + /** * @private * @returns {mail.message[]} @@ -286,6 +296,11 @@ function factory(dependencies) { ThreadCache.fields = { checkedMessages: many2many('mail.message', { + compute: '_computeCheckedMessages', + dependencies: [ + 'checkedMessages', + 'messagesCheckboxes', + ], inverse: 'checkedThreadCaches', }), /**