Skip to content

Commit

Permalink
chore: switch to vitest for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Sep 1, 2023
1 parent 15bf107 commit 7e5a52c
Show file tree
Hide file tree
Showing 12 changed files with 3,171 additions and 834 deletions.
3,493 changes: 2,837 additions & 656 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "node ./esbuild.config.mjs production && npm test",
"dev": "node ./esbuild.config.mjs",
"release-as": "scripty",
"test": "jest"
"test": "vitest"
},
"keywords": [],
"author": "",
Expand All @@ -29,9 +29,11 @@
"esbuild-plugin-inline-worker": "github:mitschabaude/esbuild-plugin-inline-worker",
"esbuild-svelte": "^0.6.1",
"fast-copy": "^2.1.1",
"happy-dom": "^10.11.2",
"he": "^1.2.0",
"jest": "^27.2.5",
"moment": "2.29.4",
"monkey-around": "^2.3.0",
"obsidian": "^1.2.8",
"scripty": "^2.1.1",
"standard-version": "^9.3.2",
Expand All @@ -43,6 +45,7 @@
"ts-jest": "^27.0.5",
"tslib": "^2.3.0",
"typescript": "^4.2.4",
"vitest": "^0.34.3",
"xmldom": "^0.6.0"
},
"dependencies": {
Expand Down
43 changes: 43 additions & 0 deletions test/calendar.Importer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @vitest-environment happy-dom
*/
import Importer from "../src/settings/import/importer";
import { vi, test, expect } from "vitest";

const GREGORIAN_EXPORT = require("./data/exports/gregorian-export.json");
const HARPTOS_EXPORT = require("./data/exports/harptos-custom-export.json");
const IMPERIAL_EXPORT = require("./data/exports/imperial-export.json");

global.createDiv = vi.fn(
(
o?: DomElementInfo | string,
callback?: (el: HTMLDivElement) => void
): HTMLDivElement => {
return document.createElement("div") as HTMLDivElement;
}
);

test("Import Gregorian", () => {
const importedCalendars = Importer.import([GREGORIAN_EXPORT]);
expect(importedCalendars.length).toBe(1);

const calendar = importedCalendars[0];

expect(calendar.name).toBe("Gregorian Calendar");
});
test("Import Harptos", () => {
const importedCalendars = Importer.import([HARPTOS_EXPORT]);
expect(importedCalendars.length).toBe(1);

const calendar = importedCalendars[0];

expect(calendar.name).toBe("Events");
});
test("Import Imperial", () => {
const importedCalendars = Importer.import([IMPERIAL_EXPORT]);
expect(importedCalendars.length).toBe(1);

const calendar = importedCalendars[0];

expect(calendar.name).toBe("Third Imperium");
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 36 additions & 20 deletions test/event.parseAltFormat.test.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,86 @@
/**
* @vitest-environment happy-dom
*/
import { CalEventDate } from "../src/@types";
import { CalEventHelper } from "../src/events/event.helper";
import { PRESET_CALENDARS } from "../src/utils/presets";
import { test, expect } from "vitest";

import Moment from 'moment';
Object.defineProperty(window, 'moment', { value: Moment });
import Moment from "moment";
Object.defineProperty(window, "moment", { value: Moment });

const GREGORIAN = PRESET_CALENDARS.find((p) => p.name == "Gregorian Calendar");
const file = {
path: "path",
basename: "basename"
}
basename: "basename",
};

const input: CalEventDate = {
year: 1400,
month: 1,
day: 28
day: 28,
};

test("YYYY-MM-DD", () => {
const YMD = new CalEventHelper(GREGORIAN, true);
const datestring = '1400-02-28';
expect(YMD.formatDigest).toEqual('YMD');
const datestring = "1400-02-28";
expect(YMD.formatDigest).toEqual("YMD");
expect(YMD.toCalDateString(input)).toEqual(datestring);
expect(YMD.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
expect(YMD.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});
test("YYYY-MMM-DD", () => {
GREGORIAN.dateFormat = "YYYY-MMM-DD";
const YMD = new CalEventHelper(GREGORIAN, true);
const datestring = '1400-February-28';
expect(YMD.formatDigest).toEqual('YMD');
const datestring = "1400-February-28";
expect(YMD.formatDigest).toEqual("YMD");
expect(YMD.toCalDateString(input)).toEqual(datestring);
expect(YMD.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
expect(YMD.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});

test("M-D-Y", () => {
GREGORIAN.dateFormat = "M-D-Y";
const MDY = new CalEventHelper(GREGORIAN, true);
const datestring = "2-28-1400";
expect(MDY.formatDigest).toEqual('MDY');
expect(MDY.formatDigest).toEqual("MDY");
expect(MDY.toCalDateString(input)).toEqual(datestring);
expect(MDY.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
expect(MDY.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});

test("MM-D-Y", () => {
GREGORIAN.dateFormat = "MM-D-Y";
const MDY = new CalEventHelper(GREGORIAN, true);
const datestring = "02-28-1400";
expect(MDY.formatDigest).toEqual('MDY');
expect(MDY.formatDigest).toEqual("MDY");
expect(MDY.toCalDateString(input)).toEqual(datestring);
expect(MDY.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
expect(MDY.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});

test("DD-M-Y", () => {
GREGORIAN.dateFormat = "DD-M-Y";
const DMY = new CalEventHelper(GREGORIAN, true);
const datestring = "28-2-1400";
expect(DMY.formatDigest).toEqual('DMY');
expect(DMY.formatDigest).toEqual("DMY");
expect(DMY.toCalDateString(input)).toEqual(datestring);
expect(DMY.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
expect(DMY.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});

test("DD-MMM-Y", () => {
GREGORIAN.dateFormat = "DD-MMM-Y";
const DMY = new CalEventHelper(GREGORIAN, true);
const datestring = "28-February-1400";
expect(DMY.formatDigest).toEqual('DMY');
expect(DMY.formatDigest).toEqual("DMY");
expect(DMY.toCalDateString(input)).toEqual(datestring);
expect(DMY.parseCalDateString(datestring, file)).toEqual(expect.objectContaining(input));
});
expect(DMY.parseCalDateString(datestring, file)).toEqual(
expect.objectContaining(input)
);
});
50 changes: 31 additions & 19 deletions test/event.parseFcString.gregorian.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/**
* @vitest-environment happy-dom
*/
import { CalEvent } from "../src/@types";
import { CalEventHelper, ParseDate } from "../src/events/event.helper";
import { sortEventList } from "../src/utils/functions";
import { PRESET_CALENDARS } from "../src/utils/presets";
import { vi, test, expect } from "vitest";

import Moment from 'moment';
Object.defineProperty(window, 'moment', { value: Moment });
import Moment from "moment";
Object.defineProperty(window, "moment", { value: Moment });

const GREGORIAN = PRESET_CALENDARS.find((p) => p.name == "Gregorian Calendar");
const helper = new CalEventHelper(GREGORIAN, false);
const file = {
path: "path",
basename: "basename"
}
basename: "basename",
};

// test("Leap Days (Gregorian)", () => {
// expect(leapDaysBeforeYear(1, GREGORIAN)).toBe(0);
Expand All @@ -27,7 +31,7 @@ test("Parse January", () => {
year: 0,
month: 0,
day: 1,
order: ''
order: "",
};
// Mess around with year 0 for fun
expect(helper.parseCalDateString("0", file)).toEqual(expected);
Expand All @@ -41,7 +45,7 @@ test("Parse February", () => {
year: 0,
month: 1,
day: 1,
order: ''
order: "",
};
expect(helper.parseCalDateString("0-02", file)).toEqual(expected);
expect(helper.parseCalDateString("0-02-01", file)).toEqual(expected);
Expand All @@ -57,21 +61,21 @@ test("Parse February: Leap year", () => {
year: 0,
month: 1,
day: 29,
order: ''
order: "",
};

// Leap years
expect(helper.parseCalDateString("1996-February-29", file)).toEqual({
...expected,
year: 1996
year: 1996,
});
expect(helper.parseCalDateString("1600-February-29", file)).toEqual({
...expected,
year: 1600
year: 1600,
});
expect(helper.parseCalDateString("2000-February-29", file)).toEqual({
...expected,
year: 2000
year: 2000,
});
// Not leap years
expect(helper.parseCalDateString("1700-Feb-29", file)).toBeNull();
Expand All @@ -83,18 +87,26 @@ test("Parse March", () => {
year: 0,
month: 2,
day: 31,
order: 'some extra'
order: "some extra",
};
// Mess around with year 0 for fun
expect(helper.parseCalDateString("0-03-31-some extra", file)).toEqual(expected);
expect(helper.parseCalDateString("0-Mar-31-some extra", file)).toEqual(expected);
expect(helper.parseCalDateString("0-March-31-some extra", file)).toEqual(expected);
expect(helper.parseCalDateString("0-03-31-some extra", file)).toEqual(
expected
);
expect(helper.parseCalDateString("0-Mar-31-some extra", file)).toEqual(
expected
);
expect(helper.parseCalDateString("0-March-31-some extra", file)).toEqual(
expected
);
});


test("Sort Gregorian dates", () => {
const events = [
helper.parseCalDateString("1954-January-01-all the things happened", file), // 0
helper.parseCalDateString(
"1954-January-01-all the things happened",
file
), // 0
helper.parseCalDateString("1954-January-01-misc", file), // 1
helper.parseCalDateString("1954-January-01", file), // 2
helper.parseCalDateString("0-February-01-other stuff", file), // 3
Expand All @@ -112,8 +124,8 @@ test("Sort Gregorian dates", () => {
note: "Test",
category: "Test",
sort: helper.parsedToTimestamp(x),
type: "Test"
}
type: "Test",
};
});

const sorted = sortEventList(fcEvents);
Expand All @@ -128,4 +140,4 @@ test("Sort Gregorian dates", () => {
expect(sorted[6].date).toEqual(events[5]);

console.log(sorted);
});
});
Loading

0 comments on commit 7e5a52c

Please sign in to comment.