generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for basic functionality
- Loading branch information
1 parent
65a4984
commit eda8f3b
Showing
8 changed files
with
187 additions
and
27 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
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
Binary file not shown.
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,30 @@ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
describe('Plugin documentation', () => { | ||
test('Check that README.md exists', () => { | ||
expect(path.join(process.cwd(), 'README.md')).toBeDefined(); | ||
}); | ||
|
||
test('Check that Template variables contains complete list of variables', async () => { | ||
const readme = await fs.readFile(path.join(process.cwd(), 'README.md'), 'utf-8'); | ||
|
||
expect(readme).toContain('{{{bookTitle}}}'); | ||
expect(readme).toContain('{{bookId}}'); | ||
expect(readme).toContain('{{{bookAuthor}}}'); | ||
expect(readme).toContain('{{{bookGenre}}}'); | ||
expect(readme).toContain('{{bookLanguage}}'); | ||
expect(readme).toContain('{{bookLastOpenedDate}}'); | ||
expect(readme).toContain('{{bookCoverUrl}}'); | ||
expect(readme).toContain('{{annotations}}'); | ||
expect(readme).toContain('{{annotations.length}}'); | ||
expect(readme).toContain('{{{chapter}}}'); | ||
expect(readme).toContain('{{{contextualText}}}'); | ||
expect(readme).toContain('{{{highlight}}}'); | ||
expect(readme).toContain('{{{note}}}'); | ||
expect(readme).toContain('{{highlightStyle}}'); | ||
expect(readme).toContain('{{highlightCreationDate}}'); | ||
expect(readme).toContain('{{highlightModificationDate}}'); | ||
}); | ||
}); |
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
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,112 @@ | ||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import SaveHighlights from '../src/methods/saveHighlightsToVault'; | ||
import { AppleBooksHighlightsImportPluginSettings } from '../src/settings'; | ||
import { aggregatedHighlights } from './mocks/aggregatedDetailsData'; | ||
import { defaultTemplateMock } from './mocks/renderedTemplate'; | ||
|
||
const mockVault = { | ||
getAbstractFileByPath: vi.fn(), | ||
// eslint-disable-next-line | ||
createFolder: vi.fn().mockImplementation(async (folderName: string) => { | ||
return; | ||
}), | ||
// eslint-disable-next-line | ||
create: vi.fn().mockImplementation(async (filePath: string, data: string) => { | ||
return; | ||
}), | ||
// eslint-disable-next-line | ||
delete: vi.fn().mockImplementation(async (folderPath: string, force: boolean) => { | ||
return; | ||
}), | ||
adapter: { | ||
list: vi.fn(), | ||
// eslint-disable-next-line | ||
copy: vi.fn().mockImplementation(async (source: string, destination: string) => { | ||
return; | ||
}), | ||
} | ||
}; | ||
|
||
beforeEach(() => { | ||
Date.now = vi.fn().mockImplementation(() => 1704060001); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
const settings = new AppleBooksHighlightsImportPluginSettings(); | ||
|
||
describe('Save highlights to vault', () => { | ||
test('Should save highlights to vault', async () => { | ||
// eslint-disable-next-line | ||
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, settings); | ||
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('ibooks-highlights'); | ||
|
||
await saveHighlights.saveHighlightsToVault(aggregatedHighlights); | ||
|
||
expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1); | ||
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith('ibooks-highlights'); | ||
|
||
expect(mockVault.delete).toHaveBeenCalledTimes(1); | ||
expect(mockVault.delete).toHaveBeenCalledWith('ibooks-highlights', true); | ||
|
||
expect(mockVault.createFolder).toHaveBeenCalledTimes(1); | ||
expect(mockVault.createFolder).toHaveBeenCalledWith('ibooks-highlights'); | ||
|
||
expect(mockVault.create).toHaveBeenCalledTimes(1); | ||
expect(mockVault.create).toHaveBeenCalledWith( | ||
`ibooks-highlights/Apple iPhone - User Guide - Instructions - with - restricted - symbols - in - title.md`, | ||
defaultTemplateMock | ||
); | ||
}); | ||
|
||
test('Should skip saving highlights to vault highlights are not found', async () => { | ||
// eslint-disable-next-line | ||
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, { ...settings, highlightsFolder: '' }); | ||
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue(''); | ||
|
||
await saveHighlights.saveHighlightsToVault(aggregatedHighlights); | ||
|
||
expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1); | ||
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith(''); | ||
|
||
expect(mockVault.delete).toHaveBeenCalledTimes(0); | ||
|
||
expect(mockVault.createFolder).toHaveBeenCalledTimes(1); | ||
expect(mockVault.createFolder).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
test('Should backup highlights if backup option is enabled', async () => { | ||
// eslint-disable-next-line | ||
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, { ...settings, backup: true }); | ||
|
||
vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('ibooks-highlights'); | ||
// eslint-disable-next-line | ||
const spyList = vi.spyOn(mockVault.adapter, 'list').mockImplementation(async (folderPath: string) => { | ||
return { | ||
files: [ | ||
'ibooks-highlights/Hello-world.md', | ||
'ibooks-highlights/Goodbye-world.md', | ||
], | ||
}; | ||
}); | ||
|
||
await saveHighlights.saveHighlightsToVault(aggregatedHighlights); | ||
|
||
expect(spyList).toHaveBeenCalledTimes(1); | ||
expect(spyList).toReturnWith({ | ||
files: [ | ||
'ibooks-highlights/Hello-world.md', | ||
'ibooks-highlights/Goodbye-world.md', | ||
], | ||
}); | ||
|
||
expect(mockVault.createFolder).toHaveBeenCalledTimes(2); | ||
expect(mockVault.createFolder).toHaveBeenNthCalledWith(1, `ibooks-highlights-bk-1704060001`); | ||
expect(mockVault.createFolder).toHaveBeenNthCalledWith(2, 'ibooks-highlights'); | ||
|
||
expect(mockVault.adapter.copy).toHaveBeenNthCalledWith(1, 'ibooks-highlights/Hello-world.md', 'ibooks-highlights-bk-1704060001/Hello-world.md'); | ||
expect(mockVault.adapter.copy).toHaveBeenNthCalledWith(2, 'ibooks-highlights/Goodbye-world.md', 'ibooks-highlights-bk-1704060001/Goodbye-world.md'); | ||
}); | ||
}); |
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,22 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { AppleBooksHighlightsImportPluginSettings } from '../src/settings'; | ||
import defaultTemplate from '../src/template'; | ||
const settings = new AppleBooksHighlightsImportPluginSettings(); | ||
|
||
describe('Plugin default settings', () => { | ||
test("Highlights folder", () => { | ||
expect(settings.highlightsFolder).toEqual('ibooks-highlights'); | ||
}); | ||
|
||
test("Import highlights on start", () => { | ||
expect(settings.importOnStart).toBeFalsy(); | ||
}); | ||
|
||
test("Backup highlights", () => { | ||
expect(settings.backup).toBeFalsy(); | ||
}); | ||
|
||
test('Template', () => { | ||
expect(settings.template).toEqual(defaultTemplate); | ||
}); | ||
}) |