core: add extensive grouping/sorting tests

This commit is contained in:
Abdullah Atta
2024-03-26 07:55:23 +05:00
parent 3a5ea3e8ef
commit e007f2592c
3 changed files with 1290 additions and 142 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -30,6 +30,7 @@ import {
} from "./utils"; } from "./utils";
import { test, expect } from "vitest"; import { test, expect } from "vitest";
import { MONTHS_FULL } from "../src/utils/date"; import { MONTHS_FULL } from "../src/utils/date";
import { GroupOptions, Note } from "../src/types";
async function createAndAddNoteToNotebook( async function createAndAddNoteToNotebook(
db: Database, db: Database,
@@ -380,122 +381,162 @@ test("adding a note with an invalid tag should clean the tag array", () =>
).toBe(0); ).toBe(0);
})); }));
test("get grouped notes by abc", () => const groups: { notes: () => Partial<Note>[]; groupOptions: GroupOptions[] }[] =
databaseTest().then(async (db) => { [
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; {
for (const letter of alphabet) { notes: () => {
for (const letter2 of alphabet) { const alphabet =
await db.notes.add({ title: `${letter}${letter2}` }); "6789ABCDEFGHIJKLMNOPQRSTUVWMNOPQGHIJKLMNOPQRS0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/.=-";
const notes: Partial<Note>[] = [];
for (let i = 0; i < alphabet.length; ++i) {
const letter = alphabet[i];
const letter2 = alphabet[alphabet.length - i];
notes.push({ title: `${letter}${letter2}` });
} }
} return notes;
await db.notes.add({ title: `Pinned note`, pinned: true }); },
await db.notes.add({ groupOptions: [
title: `Conflicted`, {
conflicted: true
});
const grouping = await db.notes.all.grouped({
groupBy: "abc", groupBy: "abc",
sortDirection: "asc", sortDirection: "asc",
sortBy: "title" sortBy: "title"
}); },
{
expect((await grouping.item(0))?.group?.title).toBe("Conflicted"); groupBy: "abc",
expect((await grouping.item(1))?.group?.title).toBe("Pinned"); sortDirection: "desc",
for (let i = 0; i < alphabet.length; ++i) { sortBy: "title"
expect((await grouping.item(i * alphabet.length + 2))?.group?.title).toBe( },
alphabet[i] {
); groupBy: "none",
sortDirection: "asc",
sortBy: "title"
},
{
groupBy: "none",
sortDirection: "desc",
sortBy: "title"
} }
})); ]
},
test("get grouped notes by month", () => {
databaseTest().then(async (db) => { notes: () => {
for (let month = 0; month <= 11; ++month) { const notes: Partial<Note>[] = [];
for (let i = 0; i < 5; ++i) { const now = dayjs(1711420693667);
const date = dayjs().month(month).toDate().getTime(); const months = [3, 8, 6, 4, 7, 9, 1, 4, 6, 7, 10, 11];
await db.notes.add({ for (const month of months) {
title: `Note in ${month} - ${i}`, const date = now.add(month, "month").valueOf();
dateCreated: date notes.push({
title: `Note in ${now.add(month, "month").month()} - ${month}`,
dateCreated: date,
dateEdited: date
}); });
} }
} return notes;
},
const grouping = await db.notes.all.grouped({ groupOptions: [
{
groupBy: "month", groupBy: "month",
sortDirection: "desc", sortDirection: "desc",
sortBy: "dateCreated" sortBy: "dateCreated"
}); },
{
for (let month = 11; month >= 0; --month) { groupBy: "month",
expect((await grouping.item((11 - month) * 5))?.group?.title).toContain( sortDirection: "asc",
MONTHS_FULL[month] sortBy: "dateCreated"
); },
} {
})); groupBy: "month",
sortDirection: "asc",
test("get grouped notes by year", () => sortBy: "dateEdited"
databaseTest().then(async (db) => { },
for (let year = 2020; year <= 2025; ++year) { {
for (let i = 0; i < 5; ++i) { groupBy: "month",
const date = dayjs().year(year).toDate().getTime(); sortDirection: "desc",
await db.notes.add({ sortBy: "dateEdited"
title: `Note in ${year} - ${i}`, },
dateCreated: date {
}); groupBy: "none",
} sortDirection: "desc",
} sortBy: "dateEdited"
},
const grouping = await db.notes.all.grouped({ {
groupBy: "year", groupBy: "none",
sortDirection: "asc",
sortBy: "dateEdited"
},
{
groupBy: "none",
sortDirection: "asc",
sortBy: "dateCreated"
},
{
groupBy: "none",
sortDirection: "desc", sortDirection: "desc",
sortBy: "dateCreated" sortBy: "dateCreated"
}); },
{
for (let year = 2020; year <= 2025; ++year) { groupBy: "week",
expect((await grouping.item((2025 - year) * 5))?.group?.title).toBe( sortDirection: "asc",
year.toString() sortBy: "dateCreated"
); },
} {
}));
test("get grouped notes by week", () =>
databaseTest().then(async (db) => {
for (let i = 1; i <= 6; ++i) {
for (let week = 1; week <= 4; ++week) {
const date = dayjs()
.month(i)
.date(week * 7)
.year(2023)
.startOf("week")
.toDate()
.getTime();
await db.notes.add({
title: `Note in ${week} - ${i}`,
dateCreated: date
});
}
}
const grouping = await db.notes.all.grouped({
groupBy: "week", groupBy: "week",
sortDirection: "desc", sortDirection: "desc",
sortBy: "dateCreated" sortBy: "dateCreated"
}); },
{
const weeks = [ groupBy: "week",
"19 - 25 Jun, 2023", sortDirection: "asc",
"22 - 28 May, 2023", sortBy: "dateEdited"
"17 - 23 Apr, 2023", },
"20 - 26 Mar, 2023", {
"20 - 26 Feb, 2023" groupBy: "week",
]; sortDirection: "desc",
for (let i = 1; i <= 5; ++i) { sortBy: "dateEdited"
expect((await grouping.item(i * 4))?.group?.title).toBe(weeks[i - 1]);
} }
})); ]
},
test("get grouped notes default", () => {
databaseTest().then(async (db) => { notes: () => {
const notes: Partial<Note>[] = [];
const now = dayjs(1711420693667);
const years = [3, 8, 6, 4, 7, 9, 1, 4, 6, 7, 10, 11];
for (const year of years) {
const date = now.add(year, "year").valueOf();
notes.push({
title: `Note in ${now.add(year, "year").year()} - ${year}`,
dateCreated: date,
dateEdited: date
});
}
return notes;
},
groupOptions: [
{
groupBy: "year",
sortDirection: "desc",
sortBy: "dateCreated"
},
{
groupBy: "year",
sortDirection: "asc",
sortBy: "dateCreated"
},
{
groupBy: "year",
sortDirection: "asc",
sortBy: "dateEdited"
},
{
groupBy: "year",
sortDirection: "desc",
sortBy: "dateEdited"
}
]
},
{
notes: () => {
const notes: Partial<Note>[] = [];
const ranges = { const ranges = {
Recent: [0, 7], Recent: [0, 7],
"Last week": [7, 14], "Last week": [7, 14],
@@ -506,22 +547,61 @@ test("get grouped notes default", () =>
for (let i = range[0]; i < range[1]; i++) { for (let i = range[0]; i < range[1]; i++) {
const date = dayjs().subtract(i, "days").toDate().getTime(); const date = dayjs().subtract(i, "days").toDate().getTime();
await db.notes.add({ notes.push({
title: `Note in ${key} - ${i}`, title: `Note in ${key} - ${i}`,
dateCreated: date dateCreated: date,
dateEdited: date
}); });
} }
} }
const grouping = await db.notes.all.grouped({ return notes;
},
groupOptions: [
{
groupBy: "default", groupBy: "default",
sortDirection: "desc", sortDirection: "desc",
sortBy: "dateCreated" sortBy: "dateCreated"
}); },
{
let i = 0; groupBy: "default",
for (const key in ranges) { sortDirection: "asc",
expect((await grouping.item(i * 7))?.group?.title).toBe(key); sortBy: "dateCreated"
++i; },
{
groupBy: "default",
sortDirection: "asc",
sortBy: "dateEdited"
},
{
groupBy: "default",
sortDirection: "desc",
sortBy: "dateEdited"
} }
]
}
];
for (const group of groups) {
const notes = group.notes();
for (const groupOptions of group.groupOptions) {
const title = `group notes by ${groupOptions.groupBy}, sort by ${groupOptions.sortBy}, direction ${groupOptions.sortDirection}`;
test(`${title}`, () =>
databaseTest().then(async (db) => {
await Promise.all(notes.map((note) => db.notes.add(note)));
await db.notes.add({ title: `Pinned note`, pinned: true });
await db.notes.add({
title: `Conflicted`,
conflicted: true
});
const grouping = await db.notes.all.grouped(groupOptions);
const items: string[] = [];
for (let i = 0; i < grouping.length; i++) {
const item = await grouping.item(i);
if (item.group) items.push(`GROUP: ${item.group.title}`);
if (item.item) items.push(item.item.title);
}
expect(items).toMatchSnapshot();
})); }));
}
}

View File

@@ -527,7 +527,8 @@ export class FilteredSelector<T extends Item> {
const sortBy: Set<SortOptions["sortBy"]> = new Set(); const sortBy: Set<SortOptions["sortBy"]> = new Set();
if (isGroupOptions(options)) { if (isGroupOptions(options)) {
if (options.groupBy === "abc") sortBy.add("title"); if (options.groupBy === "abc") sortBy.add("title");
else if (options.sortBy === "title") sortBy.add("dateCreated"); else if (options.sortBy === "title" && options.groupBy !== "none")
sortBy.add("dateCreated");
} }
sortBy.add(options.sortBy); sortBy.add(options.sortBy);