Skip to content

Commit c9f648f

Browse files
committed
fix(tree): #top/#bottom reversed in desc order (closes #7716)
1 parent 948688a commit c9f648f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

apps/server/src/services/tree.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import BBranch from "../becca/entities/bbranch.js";
55
import BNote from "../becca/entities/bnote.js";
66
import tree from "./tree.js";
77
import cls from "./cls.js";
8+
import { buildNote } from "../test/becca_easy_mocking.js";
89

910
describe("Tree", () => {
1011
let rootNote!: NoteBuilder;
@@ -73,4 +74,43 @@ describe("Tree", () => {
7374
expect(order).toStrictEqual(expectedOrder);
7475
}
7576
});
77+
78+
it("pins to the top and bottom", () => {
79+
const note = buildNote({
80+
children: [
81+
{ title: "bottom", "#bottom": "" },
82+
{ title: "5" },
83+
{ title: "3" },
84+
{ title: "2" },
85+
{ title: "1" },
86+
{ title: "top", "#top": "" }
87+
],
88+
"#sorted": ""
89+
});
90+
cls.init(() => {
91+
tree.sortNotesIfNeeded(note.noteId);
92+
});
93+
const orderedTitles = note.children.map((child) => child.title);
94+
expect(orderedTitles).toStrictEqual([ "top", "1", "2", "3", "5", "bottom" ]);
95+
});
96+
97+
it("pins to the top and bottom in reverse order", () => {
98+
const note = buildNote({
99+
children: [
100+
{ title: "bottom", "#bottom": "" },
101+
{ title: "1" },
102+
{ title: "2" },
103+
{ title: "3" },
104+
{ title: "5" },
105+
{ title: "top", "#top": "" }
106+
],
107+
"#sorted": "",
108+
"#sortDirection": "desc"
109+
});
110+
cls.init(() => {
111+
tree.sortNotesIfNeeded(note.noteId);
112+
});
113+
const orderedTitles = note.children.map((child) => child.title);
114+
expect(orderedTitles).toStrictEqual([ "top", "5", "3", "2", "1", "bottom" ]);
115+
});
76116
});

apps/server/src/services/tree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
136136
const topBEl = fetchValue(b, "top");
137137

138138
if (topAEl !== topBEl) {
139-
if (topAEl === null) return 1;
140-
if (topBEl === null) return -1;
139+
if (topAEl === null) return reverse ? -1 : 1;
140+
if (topBEl === null) return reverse ? 1 : -1;
141141

142142
// since "top" should not be reversible, we'll reverse it once more to nullify this effect
143143
return compare(topAEl, topBEl) * (reverse ? -1 : 1);
@@ -147,8 +147,8 @@ function sortNotes(parentNoteId: string, customSortBy: string = "title", reverse
147147
const bottomBEl = fetchValue(b, "bottom");
148148

149149
if (bottomAEl !== bottomBEl) {
150-
if (bottomAEl === null) return -1;
151-
if (bottomBEl === null) return 1;
150+
if (bottomAEl === null) return reverse ? 1 : -1;
151+
if (bottomBEl === null) return reverse ? -1 : 1;
152152

153153
// since "bottom" should not be reversible, we'll reverse it once more to nullify this effect
154154
return compare(bottomBEl, bottomAEl) * (reverse ? -1 : 1);

0 commit comments

Comments
 (0)