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 main functions. Decouple functions for better tes…
…tability
- Loading branch information
1 parent
a4adc48
commit 2e1d747
Showing
17 changed files
with
345 additions
and
129 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
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,45 @@ | ||
import { IBook, IBookAnnotation, ICombinedBooksAndHighlights } from '../types'; | ||
import { getBooks } from './getBooks'; | ||
import { getAnnotations } from './getAnnotations'; | ||
|
||
export const aggregateBookAndHighlightDetails = async (): Promise<ICombinedBooksAndHighlights[]> => { | ||
const books = await getBooks(); | ||
const annotations = await getAnnotations(); | ||
|
||
const resultingHighlights: ICombinedBooksAndHighlights[] = books.reduce((highlights: ICombinedBooksAndHighlights[], book: IBook) => { | ||
const bookRelatedAnnotations: IBookAnnotation[] = annotations.filter((annotation: IBookAnnotation) => annotation.ZANNOTATIONASSETID === book.ZASSETID); | ||
|
||
if (bookRelatedAnnotations.length > 0) { | ||
// Obsidian forbids adding certain characters to the title of a note, so they must be replaced with a dash (-) | ||
// | # ^ [] \ / : | ||
// after the replacement, two or more spaces are replaced with a single one | ||
// eslint-disable-next-line | ||
const normalizedBookTitle = book.ZTITLE.replace(/[\|\#\^\[\]\\\/\:]+/g, ' -').replace(/\s{2,}/g, ' '); | ||
|
||
highlights.push({ | ||
bookTitle: normalizedBookTitle, | ||
bookId: book.ZASSETID, | ||
bookAuthor: book.ZAUTHOR, | ||
bookGenre: book.ZGENRE, | ||
bookLanguage: book.ZLANGUAGE, | ||
bookLastOpenedDate: book.ZLASTOPENDATE, | ||
bookCoverUrl: book.ZCOVERURL, | ||
annotations: bookRelatedAnnotations.map(annotation => { | ||
return { | ||
chapter: annotation.ZFUTUREPROOFING5, | ||
contextualText: annotation.ZANNOTATIONREPRESENTATIVETEXT, | ||
highlight: annotation.ZANNOTATIONSELECTEDTEXT, | ||
note: annotation.ZANNOTATIONNOTE, | ||
highlightStyle: annotation.ZANNOTATIONSTYLE, | ||
highlightCreationDate: annotation.ZANNOTATIONCREATIONDATE, | ||
highlightModificationDate: annotation.ZANNOTATIONMODIFICATIONDATE | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
return highlights; | ||
}, []); | ||
|
||
return resultingHighlights; | ||
}; |
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,13 @@ | ||
import { IBookAnnotation } from '../types'; | ||
import { annotationsRequest } from '../db'; | ||
import { HIGHLIGHTS_DB_PATH, HIGHLIGHTS_LIBRARY_COLUMNS, HIGHLIGHTS_LIBRARY_NAME } from '../db/constants'; | ||
|
||
|
||
export const getAnnotations = async(): Promise<IBookAnnotation[]> => { | ||
const annotationDetails = await annotationsRequest( | ||
HIGHLIGHTS_DB_PATH, | ||
`SELECT ${HIGHLIGHTS_LIBRARY_COLUMNS.join(', ')} FROM ${HIGHLIGHTS_LIBRARY_NAME} WHERE ZANNOTATIONDELETED IS 0 AND ZANNOTATIONSELECTEDTEXT IS NOT NULL` | ||
) as IBookAnnotation[]; | ||
|
||
return annotationDetails; | ||
} |
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,12 @@ | ||
import { IBook } from '../types'; | ||
import { dbRequest } from '../db'; | ||
import { BOOKS_DB_PATH, BOOKS_LIBRARY_COLUMNS, BOOKS_LIBRARY_NAME } from '../db/constants'; | ||
|
||
export const getBooks = async (): Promise<IBook[]> => { | ||
const bookDetails = await dbRequest( | ||
BOOKS_DB_PATH, | ||
`SELECT ${BOOKS_LIBRARY_COLUMNS.join(', ')} FROM ${BOOKS_LIBRARY_NAME} WHERE ZPURCHASEDATE IS NOT NULL` | ||
) as IBook[]; | ||
|
||
return bookDetails; | ||
} |
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,20 @@ | ||
import dayjs from 'dayjs'; | ||
import * as Handlebars from "handlebars"; | ||
import { ICombinedBooksAndHighlights } from '../types'; | ||
export const renderHighlightsTemplate = async (highlight: ICombinedBooksAndHighlights, template: string) => { | ||
// TODO: Consider moving to a separate file if there are several helpers to be added | ||
Handlebars.registerHelper("eq", function(a, b) { | ||
if (a == b) { | ||
return this; | ||
} | ||
}); | ||
|
||
Handlebars.registerHelper("dateFormat", (date, format) => { | ||
return dayjs("2001-01-01").add(date, "s").format(format); | ||
}); | ||
|
||
const compiledTemplate = Handlebars.compile(template); | ||
const renderedTemplate = compiledTemplate(highlight); | ||
|
||
return renderedTemplate; | ||
} |
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
File renamed without changes.
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,25 @@ | ||
import { describe, expect, test, vi } from 'vitest'; | ||
import { aggregateBookAndHighlightDetails } from '../src/methods/aggregateDetails'; | ||
import * as db from '../src/db'; | ||
import { booksToAggregate, annotationsToAggregate, aggregatedHighlights } from './mocks/aggregatedDetailsData'; | ||
import { IBook, IBookAnnotation } from '../src/types'; | ||
|
||
describe('aggregateBookAndHighlightDetails', () => { | ||
test('Should return an array of aggregated highlights when a book has highlights', async () => { | ||
vi.spyOn(db, 'dbRequest').mockResolvedValue(booksToAggregate as IBook[]); | ||
vi.spyOn(db, 'annotationsRequest').mockResolvedValue(annotationsToAggregate as IBookAnnotation[]); | ||
|
||
const books = await aggregateBookAndHighlightDetails(); | ||
|
||
expect(books).toEqual(aggregatedHighlights); | ||
}); | ||
|
||
test('Should return an empty array when a book has no highlights', async () => { | ||
vi.spyOn(db, 'dbRequest').mockResolvedValue(booksToAggregate as IBook[]); | ||
vi.spyOn(db, 'annotationsRequest').mockResolvedValue([]); | ||
|
||
const books = await aggregateBookAndHighlightDetails(); | ||
|
||
expect(books).toEqual([]); | ||
}); | ||
}); |
Oops, something went wrong.