diff --git a/addons/mail/static/src/core/common/record.js b/addons/mail/static/src/core/common/record.js index 956dcd229b5..19cfac2c2ab 100644 --- a/addons/mail/static/src/core/common/record.js +++ b/addons/mail/static/src/core/common/record.js @@ -215,8 +215,8 @@ export class RecordList extends Array { } /** @param {R[]} records */ unshift(...records) { - for (const val of records) { - const r = this._insert(val, (r3) => { + for (let i = records.length - 1; i >= 0; i--) { + const r = this._insert(records[i], (r3) => { this.data.unshift(r3.localId); r3.__uses__.add(this); }); diff --git a/addons/mail/static/tests/core/record_tests.js b/addons/mail/static/tests/core/record_tests.js index 51b1ff4fe97..a24c6cdf455 100644 --- a/addons/mail/static/tests/core/record_tests.js +++ b/addons/mail/static/tests/core/record_tests.js @@ -149,3 +149,32 @@ QUnit.test("Trusted insert on html field with { html: true }", async (assert) => assert.strictEqual(hello.body.toString(), "
hello
"); assert.strictEqual(world.body, "world
"); }); + +QUnit.test("Unshift preserves order", async (assert) => { + (class Message extends Record { + static id = "id"; + id; + }).register(); + (class Thread extends Record { + static id = "name"; + name; + messages = Record.many("Message"); + }).register(); + const store = await start(); + const thread = store.Thread.insert({ name: "General" }); + thread.messages.unshift({ id: 3 }, { id: 2 }, { id: 1 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [3, 2, 1] + ); + thread.messages.unshift({ id: 6 }, { id: 5 }, { id: 4 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [6, 5, 4, 3, 2, 1] + ); + thread.messages.unshift({ id: 7 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [7, 6, 5, 4, 3, 2, 1] + ); +}); diff --git a/addons/mail/static/tests/thread/thread_tests.js b/addons/mail/static/tests/thread/thread_tests.js index 62a632596bb..0aa8b5a82ce 100644 --- a/addons/mail/static/tests/thread/thread_tests.js +++ b/addons/mail/static/tests/thread/thread_tests.js @@ -49,7 +49,7 @@ QUnit.test("load more messages from channel (auto-load on scroll)", async () => }); for (let i = 0; i <= 60; i++) { pyEnv["mail.message"].create({ - body: "not empty", + body: i.toString(), model: "discuss.channel", res_id: channelId, }); @@ -60,6 +60,7 @@ QUnit.test("load more messages from channel (auto-load on scroll)", async () => await contains(".o-mail-Thread", { scroll: "bottom" }); await scroll(".o-mail-Thread", 0); await contains(".o-mail-Message", { count: 60 }); + await contains(".o-mail-Message", { text: "30", after: [".o-mail-Message", { text: "29" }] }); }); QUnit.test("show message subject when subject is not the same as the thread name", async () => {