Skip to content

Commit

Permalink
fix: all suggestions from (#28)
Browse files Browse the repository at this point in the history
* remove unused field from db query response
* make highlight consistent with rest of content
* update test to use the proper highlight template
  • Loading branch information
absorpheus committed Jul 17, 2024
1 parent efb79fb commit 4c073ec
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface IBookAnnotation {
ZANNOTATIONCREATIONDATE: number;
ZANNOTATIONMODIFICATIONDATE: number;
ZANNOTATIONSTYLE: 0 | 1 | 2 | 3 | 4 | 5;
ZANNOTATIONDELETED: 0 | 1;
}

export interface IHighlight {
Expand Down
20 changes: 0 additions & 20 deletions test/mocks/aggregateWrappedTextBlockData.ts

This file was deleted.

8 changes: 3 additions & 5 deletions test/mocks/aggregatedDetailsData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ICombinedBooksAndHighlights, IBookAnnotation, IBook } from "src/types";

export const booksToAggregate: IBook[] = [{
export const booksToAggregate = [{
"ZASSETID": "THBFYNJKTGFTTVCGSAE5",
"ZTITLE": "Apple iPhone: User Guide | Instructions ^ with # restricted [ symbols ] in \ / title", // eslint-disable-line
"ZAUTHOR": "Apple Inc.",
Expand All @@ -10,7 +8,7 @@ export const booksToAggregate: IBook[] = [{
"ZCOVERURL": ''
}];

export const annotationsToAggregate: IBookAnnotation[] = [{
export const annotationsToAggregate = [{
"ZANNOTATIONASSETID": "THBFYNJKTGFTTVCGSAE5",
"ZFUTUREPROOFING5": "Aggregated Introduction",
"ZANNOTATIONREPRESENTATIVETEXT": "This is a contextual text for the aggregated hightlight from the Apple iPhone User Guide",
Expand All @@ -32,7 +30,7 @@ export const annotationsToAggregate: IBookAnnotation[] = [{
"ZANNOTATIONDELETED": 0
}];

export const aggregatedHighlights: ICombinedBooksAndHighlights[] = [{
export const aggregatedHighlights = [{
"bookTitle": "Apple iPhone - User Guide - Instructions - with - restricted - symbols - in - title",
"bookId": "THBFYNJKTGFTTVCGSAE5",
"bookAuthor": "Apple Inc.",
Expand Down
18 changes: 18 additions & 0 deletions test/mocks/detailsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const highlights = [{
"bookTitle": "Designing Data-Intensive Applications",
"bookId": "28AEDF62F12B289C88BD6659BD6E50CC",
"bookAuthor": "Kleppmann, Martin",
"bookGenre": "Technology",
"bookLanguage": "EN",
"bookLastOpenedDate": 731876693.002279,
"bookCoverUrl": '',
"annotations": [{
"chapter": '',
"contextualText": `Chapter 1 introduces the terminology and approach\nthat we're going to use throughout this book. It examines what we actually mean by\nwords like reliability, scalability, and maintainability, and how\nwe can try to achieve these goals.`,
"highlight": `Chapter 1 introduces the terminology and approach\nthat we're going to use throughout this book. It examines what we actually mean by\nwords like reliability, scalability, and maintainability, and how\nwe can try to achieve these goals.`,
"note": `Test note for the hightlight from Designing Data-Intensive Applications`,
"highlightStyle": 3,
"highlightCreationDate": 731876693.002279,
"highlightModificationDate": 731876693.002279
}]
}]
10 changes: 9 additions & 1 deletion test/mocks/rawTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ Number of annotations:: {{annotations.length}}
{{/each}}
`;

export const wrappedTextBlockTemplateMock = `{{#each annotations}}
export const rawCustomTemplateWrappedTextBlockMock = `Title:: 📕 {{{bookTitle}}}
Author:: {{{bookAuthor}}}
Link:: [Apple Books Link](ibooks://assetid/{{bookId}})
## Annotations
Number of annotations:: {{annotations.length}}
{{#each annotations}}
----
> [!QUOTE]
Expand Down
10 changes: 9 additions & 1 deletion test/mocks/renderedTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ along with a new line to test the preservation of indentation
`;

export const renderedWrappedTextBlockTemplateMock = `----
export const renderedCustomTemplateWrappedTextBlockMock = `Title:: 📕 Designing Data-Intensive Applications
Author:: Kleppmann, Martin
Link:: [Apple Books Link](ibooks://assetid/28AEDF62F12B289C88BD6659BD6E50CC)
## Annotations
Number of annotations:: 1
----
> [!QUOTE]
> Chapter 1 introduces the terminology and approach
Expand Down
13 changes: 7 additions & 6 deletions test/preserveNewlineIndentation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, expect, test } from 'vitest'
import { preserveNewlineIndentation } from 'src/utils'
import { renderHighlightsTemplate } from 'src/methods/renderHighlightsTemplate'
import { wrappedTextBlockTemplateMock } from './mocks/rawTemplates'
import { renderedWrappedTextBlockTemplateMock } from './mocks/renderedTemplate'
import { aggregateWrappedTextBlockData } from './mocks/aggregateWrappedTextBlockData'
import { rawCustomTemplateWrappedTextBlockMock } from './mocks/rawTemplates'
import { renderedCustomTemplateWrappedTextBlockMock } from './mocks/renderedTemplate'
import { highlights } from './mocks/detailsData'
import { ICombinedBooksAndHighlights } from 'src/types'

describe('preserveNewlineIndentation', () => {
test('Should handle double new line characters to preserve proper indentation in text', () => {
Expand All @@ -22,9 +23,9 @@ describe('preserveNewlineIndentation', () => {
expect(actual).toEqual(expected)
})

test('Should render a custom template with the provided data', async () => {
const renderedTemplate = await renderHighlightsTemplate(aggregateWrappedTextBlockData, wrappedTextBlockTemplateMock);
test('Should render a custom template and preserve the proper indentation when a text block is wrapped', async () => {
const renderedTemplate = await renderHighlightsTemplate(highlights[0] as ICombinedBooksAndHighlights, rawCustomTemplateWrappedTextBlockMock);

expect(renderedTemplate).toEqual(renderedWrappedTextBlockTemplateMock);
expect(renderedTemplate).toEqual(renderedCustomTemplateWrappedTextBlockMock);
});
})
5 changes: 3 additions & 2 deletions test/renderHighlightsTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { rawCustomTemplateMock } from './mocks/rawTemplates';
import { defaultTemplateMock, renderedCustomTemplateMock } from './mocks/renderedTemplate';
import defaultTemplate from '../src/template';
import { ICombinedBooksAndHighlights } from 'src/types';

describe('renderHighlightsTemplate', () => {
const helpers = Handlebars.helpers;
Expand All @@ -18,15 +19,15 @@ describe('renderHighlightsTemplate', () => {

describe('Template rendering', () => {
test('Should render a default template with the provided data', async () => {
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0], defaultTemplate);
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0] as ICombinedBooksAndHighlights, defaultTemplate);

expect(renderedTemplate).toEqual(defaultTemplateMock);
});

test('Should render a custom template with the provided data', async () => {
tzSpy.mockImplementation(() => 'America/New_York');

const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0], rawCustomTemplateMock);
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0] as ICombinedBooksAndHighlights, rawCustomTemplateMock);

expect(renderedTemplate).toEqual(renderedCustomTemplateMock);
});
Expand Down
7 changes: 4 additions & 3 deletions test/saveHighlightsToVault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SaveHighlights from '../src/methods/saveHighlightsToVault';
import { AppleBooksHighlightsImportPluginSettings } from '../src/settings';
import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { defaultTemplateMock } from './mocks/renderedTemplate';
import { ICombinedBooksAndHighlights } from '../src/types'

const mockVault = {
getAbstractFileByPath: vi.fn(),
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Save highlights to vault', () => {
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, settings);
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('ibooks-highlights');

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1);
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith('ibooks-highlights');
Expand All @@ -66,7 +67,7 @@ describe('Save highlights to vault', () => {
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, { ...settings, highlightsFolder: '' });
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('');

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyGetAbstractFileByPath).toHaveBeenCalledTimes(1);
expect(spyGetAbstractFileByPath).toHaveBeenCalledWith('');
Expand All @@ -92,7 +93,7 @@ describe('Save highlights to vault', () => {
};
});

await saveHighlights.saveHighlightsToVault(aggregatedHighlights);
await saveHighlights.saveHighlightsToVault(aggregatedHighlights as ICombinedBooksAndHighlights[]);

expect(spyList).toHaveBeenCalledTimes(1);
expect(spyList).toReturnWith({
Expand Down

0 comments on commit 4c073ec

Please sign in to comment.