FIX: Do not async already loaded categories (#25607)

Use sync findByIds to return the category list if all of them have been
previously loaded.
This commit is contained in:
Bianca Nenciu 2024-02-08 18:36:31 +02:00 committed by GitHub
parent 6bd26e81c1
commit 95555b9a97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -123,7 +123,7 @@ export default class Category extends RestModel {
static async asyncFindByIds(ids = []) {
ids = ids.map((x) => parseInt(x, 10));
if (!Site.current().lazy_load_categories) {
if (!Site.current().lazy_load_categories || this.hasAsyncFoundAll(ids)) {
return this.findByIds(ids);
}

View File

@ -3,6 +3,7 @@ import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import Category from "discourse/models/category";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Unit | Model | category", function (hooks) {
setupTest(hooks);
@ -447,4 +448,27 @@ module("Unit | Model | category", function (hooks) {
"Test2 Sub Sub",
]);
});
test("asyncFindByIds - do not request categories that have been loaded already", async function (assert) {
const requestedIds = [];
pretender.get("/categories/find", (request) => {
const ids = request.queryParams.ids.map((id) => parseInt(id, 10));
requestedIds.push(ids);
return response({
categories: ids.map((id) => ({ id, slug: `category-${id}` })),
});
});
const site = this.owner.lookup("service:site");
site.set("lazy_load_categories", true);
await Category.asyncFindByIds([12345, 12346]);
assert.deepEqual(requestedIds, [[12345, 12346]]);
await Category.asyncFindByIds([12345, 12346, 12347]);
assert.deepEqual(requestedIds, [[12345, 12346], [12347]]);
await Category.asyncFindByIds([12345]);
assert.deepEqual(requestedIds, [[12345, 12346], [12347]]);
});
});