Skip to content

Commit

Permalink
Replaces mocha tests with jest
Browse files Browse the repository at this point in the history
  • Loading branch information
lynchjames committed Sep 18, 2023
1 parent df943dd commit 9f8661d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 68 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
"cross-env": "^7.0.3",
"ignore-styles": "^5.0.1",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.7.0",
"jsdom": "^16.6.0",
"jsdom-global": "^3.0.2",
"mocha": "^10.2.0",
"moment": "^2.29.4",
"nyc": "^15.1.0",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"rollup": "^2.52.1",
"rollup-plugin-copy": "^3.4.0",
"ts-node": "^9.1.1",
"tslib": "^2.3.0",
"typescript": "^4.3.4"
},
"dependencies": {
"moment": "^2.29.1"
}
}
8 changes: 1 addition & 7 deletions src/moment-date-regex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_DATE_FORMAT, DATE_REGEX } from './constants';
import * as moment from 'moment';
import moment from "moment";

export default class MomentDateRegex {
replace(input: string): string {
Expand Down Expand Up @@ -34,12 +34,6 @@ export default class MomentDateRegex {
}

getMoment(now: Date, dateFormat: string) {
if((window as any).moment) {
return (window as any)
.moment(now)
.format(dateFormat)
} else {
return moment(now).format(dateFormat);
}
}
}
48 changes: 24 additions & 24 deletions tests/doc.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from 'chai';
import {describe, expect, beforeAll} from '@jest/globals';
import NRDoc from '../src/doc';
import { NoteRefactorSettings } from '../src/settings';
import { promises as fs } from 'fs';
Expand All @@ -17,17 +17,17 @@ describe("Note content - Content Only", () => {

it("First line content", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(firstLine(noteContent), "Hi there! I'm a note in your vault.");
expect(firstLine(noteContent)).toBe("Hi there! I'm a note in your vault.");
});

it("Last line content", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]");
expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]");
});

it("Character count", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(noteContent.length, 746);
expect(noteContent.length).toBe(746);
});

});
Expand All @@ -44,22 +44,22 @@ describe("Note content - Content Only - Normalize header levels", () => {

it("First line content", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(firstLine(noteContent), "# I have questions.");
expect(firstLine(noteContent)).toBe("# I have questions.");
});

it("Header 3 content", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(toArray(noteContent)[4], "## Header 3");
expect(toArray(noteContent)[4]).toBe("## Header 3");
});

it("Last line content", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(lastLine(noteContent), "This is for testing normalizing header levels.");
expect(lastLine(noteContent)).toBe("This is for testing normalizing header levels.");
});

it("Character count", () => {
const noteContent = doc.noteContent(content[0], content.slice(1), true);
assert.equal(noteContent.length, 232);
expect(noteContent.length).toBe(232);
});

});
Expand All @@ -76,27 +76,27 @@ describe("Note content - First Line as File Name, exclude first line", () => {

it("First Line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(firstLine(noteContent), "At the same time, I'm also just a Markdown file sitting on your hard disk. It's all in plain text, so you don't need to worry about losing me in case [[Obsidian]] disappears one day.");
expect(firstLine(noteContent)).toBe("At the same time, I'm also just a Markdown file sitting on your hard disk. It's all in plain text, so you don't need to worry about losing me in case [[Obsidian]] disappears one day.");
});

it("Last line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]");
expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]");
});

it("External links preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[9], '- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]');
expect(toArray(noteContent)[9]).toBe('- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]');
});

it("Embeds preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[7], '- How to ![[Create notes|create new notes]].');
expect(toArray(noteContent)[7]).toBe('- How to ![[Create notes|create new notes]].');
});

it("Character count", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(noteContent.length, 709);
expect(noteContent.length).toBe(709);
});

});
Expand All @@ -116,27 +116,27 @@ describe("Note content - First Line as File Name, first line as heading", () =>

it("First Line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(firstLine(noteContent), "# Hi there! I'm a note in your vault.");
expect(firstLine(noteContent)).toBe("# Hi there! I'm a note in your vault.");
});

it("Last line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(lastLine(noteContent), "- How to [[Working with multiple notes|open multiple files side by side]]");
expect(lastLine(noteContent)).toBe("- How to [[Working with multiple notes|open multiple files side by side]]");
});

it("External links preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[11], '- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]');
expect(toArray(noteContent)[11]).toBe('- How to use [Markdown](https://www.markdownguide.org) to [[Format your notes]]');
});

it("Embeds preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[9], '- How to ![[Create notes|create new notes]].');
expect(toArray(noteContent)[9]).toBe('- How to ![[Create notes|create new notes]].');
});

it("Character count", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(noteContent.length, 748);
expect(noteContent.length).toBe(748);
});

});
Expand All @@ -154,32 +154,32 @@ describe("Note content - First Line as File Name, first line as heading (modifie

it("First Line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(firstLine(noteContent), "# Quick Start");
expect(firstLine(noteContent)).toBe("# Quick Start");
});

it("Last line text", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(lastLine(noteContent), "## Workflows");
expect(lastLine(noteContent)).toBe("## Workflows");
});

it("Internal links preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[9], '- [[Keyboard shortcuts]]');
expect(toArray(noteContent)[9]).toBe('- [[Keyboard shortcuts]]');
});

it("External links preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[18], 'If you are a [Catalyst supporter](https://obsidian.md/pricing), and want to turn on Insider Builds, see [[Insider builds]].');
expect(toArray(noteContent)[18]).toBe('If you are a [Catalyst supporter](https://obsidian.md/pricing), and want to turn on Insider Builds, see [[Insider builds]].');
});

it("Embeds preserved", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(toArray(noteContent)[20], '![Obsidian.md](https://obsidian.md/images/screenshot.png)');
expect(toArray(noteContent)[20]).toBe('![Obsidian.md](https://obsidian.md/images/screenshot.png)');
});

it("Character count", () => {
const noteContent = doc.noteContent(content[0], content.slice(1));
assert.equal(noteContent.length, 1105);
expect(noteContent.length).toBe(1105);
});

});
Expand Down
28 changes: 14 additions & 14 deletions tests/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from 'chai';
import {describe, expect, beforeAll, afterAll} from '@jest/globals';
import { mockDate} from './mocks/date';
import NRFile from '../src/file';
import { NoteRefactorSettings } from '../src/settings';
Expand All @@ -19,37 +19,37 @@ describe("File Name Prefix", () => {
it("Correct prefix for YYYYMMDDHHmm", () => {
settings.fileNamePrefix = '{{date:YYYYMMDDHHmm}}-';
const prefix = file.fileNamePrefix();
assert.equal(prefix, '202012251117-');
expect(prefix).toBe('202012251117-');
});

it("Correct prefix for YYYYMMDDHHmmss", () => {
settings.fileNamePrefix = '{{date:YYYYMMDDHHmmss}}-';
const prefix = file.fileNamePrefix();
assert.equal(prefix, '20201225111752-');
expect(prefix).toBe('20201225111752-');
});

it("Correct prefix for word dates YYYY-MMMM-Do_dddd", () => {
settings.fileNamePrefix = '{{date:YYYY-MMMM-Do_dddd}}-';
const prefix = file.fileNamePrefix();
assert.equal(prefix, '2020-December-25th_Friday-');
expect(prefix).toBe('2020-December-25th_Friday-');
});

it("Correct prefix for with text and date", () => {
settings.fileNamePrefix = 'ZK_{{date:YYYYMMDDHHmm}}-';
const prefix = file.fileNamePrefix();
assert.equal(prefix, 'ZK_202012251117-');
expect(prefix).toBe('ZK_202012251117-');
});

it("No date in prefix", () => {
settings.fileNamePrefix = 'Inbox-Note-';
const prefix = file.fileNamePrefix();
assert.equal(prefix, 'Inbox-Note-');
expect(prefix).toBe('Inbox-Note-');
});

it("No prefix", () => {
settings.fileNamePrefix = '';
const prefix = file.fileNamePrefix();
assert.equal(prefix, '');
expect(prefix).toBe('');
});

afterAll(() => {
Expand All @@ -70,38 +70,38 @@ describe("File Name Sanitisation", () => {
it("No sanitisation should be required", () => {
fileName = '-- This should be allowed & (e.g. £$\'=)';
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, fileName);
expect(sanitised).toBe(fileName);
});

it("Expected internal link sanitisation", () => {
fileName = '[[Internal Link]] to something';
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, 'Internal Link to something');
expect(sanitised).toBe('Internal Link to something');
});

it("Expected external link sanitisation", () => {
fileName = '[Obsidian](https://en.wikipedia.org/wiki/Obsidian)';
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, 'Obsidian(httpsen.wikipedia.orgwikiObsidian)');
expect(sanitised).toBe('Obsidian(httpsen.wikipedia.orgwikiObsidian)');
});

it("Heading sanitisation", () => {
fileName = '## A Heading Goes Here';
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, 'A Heading Goes Here');
expect(sanitised).toBe('A Heading Goes Here');
});

it("Illegal file path character sanitisation (*\"\/<>:|?", () => {
fileName = '**This has**\\/ "a lot" of <illegal>: |characters??|' ;
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, 'This has a lot of illegal characters');
expect(sanitised).toBe('This has a lot of illegal characters');
});

it("Should include prefix", () => {
fileName = '## A Heading Goes Here';
settings.fileNamePrefix = 'ZK-{{date:YYYY-MMM-DD-HHmm}}-';
const sanitised = file.sanitisedFileName(fileName);
assert.equal(sanitised, 'ZK-2020-Dec-25-1117-A Heading Goes Here');
expect(sanitised).toBe('ZK-2020-Dec-25-1117-A Heading Goes Here');
});

it("Idempotent sanitisation with no duplicate prefixes", () => {
Expand All @@ -110,7 +110,7 @@ describe("File Name Sanitisation", () => {
let sanitised = file.sanitisedFileName(fileName);
sanitised = file.sanitisedFileName(sanitised);
sanitised = file.sanitisedFileName(sanitised);
assert.equal(sanitised, 'ZK-2020-Dec-25-1117-A Heading Goes Here');
expect(sanitised).toBe('ZK-2020-Dec-25-1117-A Heading Goes Here');
});


Expand Down
24 changes: 14 additions & 10 deletions tests/moment-date-regex.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assert } from 'chai';
/**
* @jest-environment jsdom
*/
import {describe, expect, beforeAll, afterAll} from '@jest/globals';
import moment from 'moment';
import { mockDate } from './mocks/date'
import MomentDateRegex from '../src/moment-date-regex';
const date = new Date(2020, 9, 31, 14, 25, 15);
Expand All @@ -15,42 +19,42 @@ describe("Date formatting", () => {
const input = 'Zettels/{{date}}';
const expectedOuput = 'Zettels/202010311425';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("YYYYMMDD format", () => {
const input = 'Zettels/{{date:YYYYMMDD}}';
const expectedOuput = 'Zettels/20201031';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Multiple dates", () => {
const input = 'Zettels/{{date:YYYY}}/{{date:MMM}}/{{date:DD_ddd}}';
const expectedOuput = 'Zettels/2020/Oct/31_Sat';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Date path prefixing", () => {
const input = '{{date:YYYY}}/{{date:MM}}/My Notes';
const expectedOuput = '2020/10/My Notes';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Text between date targets", () => {
const input = '{{date:YYYY}}/Zettels/{{date:MMMM}}';
const expectedOuput = '2020/Zettels/October';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Date file name prefixing", () => {
const input = '{{date:YYYYMMDDHHmm}}-My New Note';
const expectedOuput = '202010311425-My New Note';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

afterAll(() => {
Expand All @@ -64,20 +68,20 @@ describe("Non-date input", () => {
const input = 'Inbox/New';
const expectedOuput = 'Inbox/New';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Input with date format without date target", () => {
const input = 'Inbox/YYYY';
const expectedOuput = 'Inbox/YYYY';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});

it("Input with date format and partial date target", () => {
const input = 'Inbox/{{date:YYYY';
const expectedOuput = 'Inbox/{{date:YYYY';

assert.equal(momentRegex.replace(input), expectedOuput);
expect(momentRegex.replace(input)).toBe(expectedOuput);
});
});
Loading

0 comments on commit 9f8661d

Please sign in to comment.