generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Mearman/add-date-format-testing
Add date format testing
- Loading branch information
Showing
4 changed files
with
195 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { OmnivoreSettings } from "./main"; | ||
|
||
export const DEFAULT_SETTINGS: OmnivoreSettings = { | ||
dateHighlightedFormat: "yyyy-MM-dd HH:mm:ss", | ||
dateSavedFormat: "yyyy-MM-dd HH:mm:ss", | ||
apiKey: "", | ||
filter: "HIGHLIGHTS", | ||
syncAt: "", | ||
customQuery: "", | ||
template: `--- | ||
id: {{{id}}} | ||
title: {{{title}}} | ||
{{#author}} | ||
author: {{{author}}} | ||
{{/author}} | ||
{{#labels.length}} | ||
tags: | ||
{{#labels}} - {{{name}}} | ||
{{/labels}} | ||
{{/labels.length}} | ||
date_saved: {{{dateSaved}}} | ||
{{#datePublished}} | ||
date_published: {{{datePublished}}} | ||
{{/datePublished}} | ||
--- | ||
# {{{title}}} | ||
#Omnivore | ||
[Read on Omnivore]({{{omnivoreUrl}}}) | ||
[Read Original]({{{originalUrl}}}) | ||
{{#highlights.length}} | ||
## Highlights | ||
{{#highlights}} | ||
> {{{text}}} [⤴️]({{{highlightUrl}}}) {{#labels}} #{{name}} {{/labels}} | ||
{{#note}} | ||
{{{note}}} | ||
{{/note}} | ||
{{/highlights}} | ||
{{/highlights.length}}`, | ||
highlightOrder: "LOCATION", | ||
syncing: false, | ||
folder: "Omnivore/{{date}}", | ||
folderDateFormat: "yyyy-MM-dd", | ||
endpoint: "https://api-prod.omnivore.app/api/graphql", | ||
filename: "{{{title}}}" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { DEFAULT_SETTINGS } from "../DEFAULT_SETTINGS"; | ||
import { formatDate } from "../util"; | ||
|
||
const jsDate = new Date("2023-02-18 13:02:08.169"); | ||
const apiDate = jsDate.toISOString(); // API returns ISO 8601 date strings | ||
|
||
type testCase = { | ||
format: string, | ||
date: string | ||
expected: string | ||
} | ||
|
||
|
||
const luxonHierarchicalFormatWithTime = { | ||
date: apiDate, | ||
expected: "2023/2023-02/2023-02-18/130208", | ||
format: "yyyy/yyyy-MM/yyyy-MM-dd/HHmmss" | ||
}; | ||
const luxonHierarchicalFormat = { | ||
date: apiDate, | ||
expected: "2023/2023-02/2023-02-18", | ||
format: "yyyy/yyyy-MM/yyyy-MM-dd" | ||
}; | ||
const defaultDateHighlightedFormatTestCase: testCase = { | ||
date: apiDate, | ||
expected: "2023-02-18 13:02:08", | ||
format: DEFAULT_SETTINGS.dateHighlightedFormat | ||
}; | ||
const defaultDateSavedFormatTestCase: testCase = { | ||
date: apiDate, | ||
expected: "2023-02-18 13:02:08", | ||
format: DEFAULT_SETTINGS.dateSavedFormat | ||
}; | ||
const defaultFolderDateFormatTestCase: testCase = { | ||
date: apiDate, | ||
expected: "2023-02-18", | ||
format: DEFAULT_SETTINGS.folderDateFormat | ||
}; | ||
const testCases: testCase[] = [ | ||
defaultDateHighlightedFormatTestCase, | ||
defaultDateSavedFormatTestCase, | ||
defaultFolderDateFormatTestCase, | ||
luxonHierarchicalFormat, | ||
luxonHierarchicalFormatWithTime | ||
]; | ||
describe("ensure default formats are as expected", () => { | ||
test("dateHighlightedFormat", () => { | ||
expect(DEFAULT_SETTINGS.dateHighlightedFormat).toBe("yyyy-MM-dd HH:mm:ss"); | ||
}); | ||
test("dateSavedFormat", () => { | ||
expect(DEFAULT_SETTINGS.dateSavedFormat).toBe("yyyy-MM-dd HH:mm:ss"); | ||
}); | ||
test("folderDateFormat", () => { | ||
expect(DEFAULT_SETTINGS.folderDateFormat).toBe("yyyy-MM-dd"); | ||
}); | ||
}); | ||
|
||
describe("formatDate on known formats", () => { | ||
test.each(testCases)("should correctly format %s", (testCase) => { | ||
const result = formatDate(testCase.date, testCase.format); | ||
expect(result).toBe(testCase.expected); | ||
}); | ||
}); | ||
|
||
const { DateTime } = require("luxon"); | ||
|
||
function generateRandomISODateStrings(quantity: number) { | ||
const randomISODateStrings = []; | ||
const timeZones: any[] = Intl.DateTimeFormat().resolvedOptions().timeZone.split(","); | ||
|
||
for (let i = 0; i < quantity; i++) { | ||
let date = new Date( | ||
Date.UTC( | ||
Math.floor(Math.random() * (2038 - 1970) + 1970), | ||
Math.floor(Math.random() * 12), | ||
Math.floor(Math.random() * 28), | ||
Math.floor(Math.random() * 24), | ||
Math.floor(Math.random() * 60), | ||
Math.floor(Math.random() * 60), | ||
Math.floor(Math.random() * 1000) | ||
) | ||
); | ||
|
||
// Randomly select a timezone from the available time zones | ||
const randomTimeZone = timeZones[Math.floor(Math.random() * timeZones.length)]; | ||
|
||
// Convert the generated date to the randomly selected timezone | ||
// const dateTimeWithZone = DateTime.fromJSDate(date, { zone: randomTimeZone }).toUTC(); | ||
const jsDateTimeWithZone = new Date(date.toLocaleString("en-US", { timeZone: randomTimeZone })); | ||
const luxonDate = DateTime.fromJSDate(jsDateTimeWithZone); | ||
randomISODateStrings.push(luxonDate.toISO()); | ||
} | ||
|
||
return randomISODateStrings; | ||
} | ||
|
||
describe("formatDate on random dates", () => { | ||
test.each(generateRandomISODateStrings(100))("should correctly format %s", (date) => { | ||
const result = formatDate(date, "yyyy-MM-dd HH:mm:ss"); | ||
// test with regex to ensure the format is correct | ||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/); | ||
}); | ||
}); | ||
|
||
function getCasesWithRandomDates(testFormats: string[], quantity = 10) { | ||
return testFormats.flatMap(luxonFormat => | ||
generateRandomISODateStrings(quantity).map(date => ({ date, luxonFormat })) | ||
); | ||
} | ||
|
||
describe("round trip on random dates", () => { | ||
const testFormats = [ | ||
defaultDateHighlightedFormatTestCase.format, | ||
defaultDateSavedFormatTestCase.format, | ||
defaultFolderDateFormatTestCase.format | ||
]; | ||
// generate permutations of testCases.formats and 10 generated each | ||
const casesWithRandomDates = getCasesWithRandomDates(testFormats); | ||
test.each(casesWithRandomDates)("should be unchanged after round trip %s", (testCase) => { | ||
const result = formatDate(testCase.date, testCase.luxonFormat); | ||
const result2 = formatDate(result, testCase.luxonFormat); | ||
expect(result2).toBe(result); | ||
}); | ||
|
||
const atypicalFormats = [ | ||
luxonHierarchicalFormat.format, | ||
luxonHierarchicalFormatWithTime.format | ||
]; | ||
test.each(getCasesWithRandomDates(atypicalFormats))("should be unchanged after round trip with atypical format %s", (testCase) => { | ||
const formattedDate = formatDate(testCase.date, testCase.luxonFormat); | ||
const parsedDate = DateTime.fromFormat(formattedDate, testCase.luxonFormat); | ||
expect(parsedDate.isValid).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters