diff --git a/addons/mail/static/src/components/discuss/discuss.js b/addons/mail/static/src/components/discuss/discuss.js index e85bea436cb..f565e18e208 100644 --- a/addons/mail/static/src/components/discuss/discuss.js +++ b/addons/mail/static/src/components/discuss/discuss.js @@ -2,6 +2,7 @@ import { registerMessagingComponent } from '@mail/utils/messaging_component'; import { link, unlink } from '@mail/model/model_field_command'; +import { useUpdate } from '@mail/component_hooks/use_update/use_update'; const { Component } = owl; const { useRef } = owl.hooks; @@ -24,28 +25,19 @@ export class Discuss extends Component { // bind since passed as props this._onMobileAddItemHeaderInputSelect = this._onMobileAddItemHeaderInputSelect.bind(this); this._onMobileAddItemHeaderInputSource = this._onMobileAddItemHeaderInputSource.bind(this); + useUpdate({ func: () => this._update() }); } - mounted() { + _update() { if (!this.discuss) { return; } this.discuss.update({ isOpen: true }); if (this.discuss.thread) { this.trigger('o-push-state-action-manager'); - } else if (this.discuss.messaging.isInitialized) { + } else if (!this._activeThreadCache && this.discuss.messaging.isInitialized) { this.discuss.openInitThread(); } - this._updateLocalStoreProps(); - } - - patched() { - if (!this.discuss) { - return; - } - if (this.discuss.thread) { - this.trigger('o-push-state-action-manager'); - } if ( this.discuss.thread && this.discuss.thread === this.messaging.inbox && diff --git a/addons/mail/static/src/components/discuss/tests/discuss_tests.js b/addons/mail/static/src/components/discuss/tests/discuss_tests.js index 245d05c2847..d94b624ed81 100644 --- a/addons/mail/static/src/components/discuss/tests/discuss_tests.js +++ b/addons/mail/static/src/components/discuss/tests/discuss_tests.js @@ -23,14 +23,16 @@ QUnit.module('discuss_tests.js', { beforeEach(this); this.start = async params => { - const { afterEvent, env, widget } = await start(Object.assign({}, params, { + const res = await start(Object.assign({}, params, { autoOpenDiscuss: true, data: this.data, hasDiscuss: true, })); + const { afterEvent, env, widget } = res; this.afterEvent = afterEvent; this.env = env; this.widget = widget; + return res; }; }, afterEach() { @@ -50,6 +52,34 @@ QUnit.test('messaging not created', async function (assert) { messagingBeforeCreationDeferred.resolve(); }); +QUnit.test('discuss should be marked as opened if the component is already rendered and messaging becomes created afterwards', async function (assert) { + assert.expect(1); + + const messagingBeforeCreationDeferred = makeTestPromise(); + await this.start({ + messagingBeforeCreationDeferred, + waitUntilMessagingCondition: 'none', + }); + + await afterNextRender(() => messagingBeforeCreationDeferred.resolve()); + assert.ok( + this.messaging.discuss.isOpen, + "discuss should be marked as opened if the component is already rendered and messaging becomes created afterwards" + ); +}); + +QUnit.test('discuss should be marked as closed when the component is unmounted', async function (assert) { + assert.expect(1); + + const { widget } = await this.start(); + + await afterNextRender(() => widget.destroy()); + assert.notOk( + this.messaging.discuss.isOpen, + "discuss should be marked as closed when the component is unmounted" + ); +}); + QUnit.test('messaging not initialized', async function (assert) { assert.expect(1);