Skip to content

Commit

Permalink
Migrate tests into src/ dir (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 authored Nov 26, 2023
1 parent cdd6e9f commit 71bc89d
Show file tree
Hide file tree
Showing 7 changed files with 647 additions and 647 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "npm run build && cp -R dist/* test-vault/.obsidian/plugins/todoist-sync-plugin/",
"build": "svelte-check && rollup -c",
"test": "mocha -r ts-node/register tests/*.ts src/**/*.test.ts",
"test": "mocha -r ts-node/register src/**/*.test.ts",
"format": "prettier --write src/**/*",
"lint": "prettier --check src/**/*"
},
Expand Down
173 changes: 173 additions & 0 deletions src/api/projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import "mocha";
import { assert } from "chai";
import type { LabelID, ProjectID, SectionID } from "./models";
import { Project } from "./models";
import type { ITaskRaw, IProjectRaw, ISectionRaw } from "./raw_models";
import type { ITodoistMetadata } from "./api";
import { ExtendedMap } from "../utils";

describe("Project tree parsing", () => {
it("Projects are parented correctly", () => {
const rawTasks: ITaskRaw[] = [
{
id: "1",
project_id: "1",
section_id: null,
labels: [],
content: "",
description: "",
priority: 1,
order: 1,
},
{
id: "2",
project_id: "2",
section_id: null,
labels: [],
content: "",
description: "",
priority: 1,
order: 2,
},
];

const metadata = createMetadata({
projects: [
{
id: "1",
order: 1,
name: "Parent",
},
{
id: "2",
order: 2,
parent_id: "1",
name: "Child",
},
],
});

const projectTree = Project.buildProjectTree(rawTasks, metadata);
assert.lengthOf(projectTree, 1, "Resulting tree has one parent node");

const parent = projectTree[0];
assert.lengthOf(
parent.subProjects,
1,
"Parent project has one sub-project"
);

assert.equal(parent.projectID, "1");
assert.lengthOf(parent.tasks, 1, "Parent has one task");
assert.equal(parent.tasks[0].id, "1", "Parent has correct task");

const child = parent.subProjects[0];
assert.equal(child.projectID, "2");
assert.lengthOf(child.tasks, 1, "Child has one task");
assert.equal(child.tasks[0].id, "2", "Child has correct task");
});

it("Sections are parented correctly", () => {
const rawTask: ITaskRaw[] = [
{
id: "1",
project_id: "1",
section_id: "1",
labels: [],
content: "",
description: "",
priority: 1,
order: 1,
},
];

const metadata = createMetadata({
projects: [
{
id: "1",
order: 1,
name: "Parent",
},
],
sections: [
{
id: "1",
project_id: "1",
order: 0,
name: "Section",
},
],
});

const projects = Project.buildProjectTree(rawTask, metadata);
assert.lengthOf(projects, 1, "Resulting tree has one parent node.");

const parent = projects[0];
assert.lengthOf(parent.sections, 1, "Parent has one section");
assert.lengthOf(parent.tasks, 0, "Parent has no tasks");

const section = parent.sections[0];
assert.equal(section.sectionID, "1");
assert.lengthOf(section.tasks, 1, "Section has one task");
});

it("Unknown project or sections are marked as such", () => {
const tasks: ITaskRaw[] = [
{
id: "1",
project_id: "1",
section_id: null,
labels: [],
content: "",
description: "",
priority: 1,
order: 1,
},
{
id: "2",
project_id: "2",
section_id: "1",
labels: [],
content: "",
description: "",
priority: 1,
order: 2,
},
];

const projects = Project.buildProjectTree(tasks, createMetadata({}));
assert.lengthOf(projects, 1, "Only one project is returned");

const parent = projects[0];
assert.equal(parent.projectID, "-1", "Unknown project ID");
assert.lengthOf(parent.tasks, 1, "Parent has one task");
assert.equal(parent.tasks[0].id, "1", "Parent has 'correct' task");

assert.lengthOf(parent.sections, 1, "Parent has one section");
const section = parent.sections[0];
assert.equal(section.sectionID, "-1", "Unknown section ID");
assert.lengthOf(section.tasks, 1, "Parent has one task");
assert.equal(section.tasks[0].id, "2", "Parent has 'correct' task");
});
});

function createMetadata(options: {
projects?: IProjectRaw[];
sections?: ISectionRaw[];
}): ITodoistMetadata {
const metadata: ITodoistMetadata = {
projects: new ExtendedMap<ProjectID, IProjectRaw>(),
sections: new ExtendedMap<SectionID, ISectionRaw>(),
labels: new ExtendedMap<LabelID, string>(),
};

if (options.projects) {
options.projects.forEach((prj) => metadata.projects.set(prj.id, prj));
}

if (options.sections) {
options.sections.forEach((sect) => metadata.sections.set(sect.id, sect));
}

return metadata;
}
Loading

0 comments on commit 71bc89d

Please sign in to comment.