Skip to content

Commit

Permalink
test(core): ensure proper rendering of highlights in custom templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bandantonio committed Jul 18, 2024
1 parent dcdce55 commit 0ed9ccc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 66 deletions.
4 changes: 2 additions & 2 deletions test/mocks/aggregatedDetailsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const annotationsToAggregate = [{
"ZANNOTATIONASSETID": "THBFYNJKTGFTTVCGSAE5",
"ZFUTUREPROOFING5": "Another aggregated Introduction",
"ZANNOTATIONREPRESENTATIVETEXT": "This is a contextual text for the aggregated highlight from the Apple iPhone User Guide\n\ncontaining a new line to test the preservation of indentation",
"ZANNOTATIONSELECTEDTEXT": "aggregated highlight from the Apple iPhone User Guide\n\ncontaining a new line to test the preservation of indentation",
"ZANNOTATIONSELECTEDTEXT": "aggregated highlight from the Apple iPhone User Guide\n\ncontaining a new line to test the preservation of indentation\n\nand another new line\n\nto check one more time",
"ZANNOTATIONLOCATION": "aggregated-highlight-link-from-the-apple-iphone-user-guide",
"ZANNOTATIONNOTE": "Test note for the aggregated highlight from the Apple iPhone User Guide\n\nalong with a new line to test the preservation of indentation",
"ZANNOTATIONCREATIONDATE": 731876693.002279,
Expand Down Expand Up @@ -52,7 +52,7 @@ export const aggregatedHighlights = [{
}, {
"chapter": "Another aggregated Introduction",
"contextualText": "This is a contextual text for the aggregated highlight from the Apple iPhone User Guide\ncontaining a new line to test the preservation of indentation",
"highlight": "aggregated highlight from the Apple iPhone User Guide\ncontaining a new line to test the preservation of indentation",
"highlight": "aggregated highlight from the Apple iPhone User Guide\ncontaining a new line to test the preservation of indentation\nand another new line\nto check one more time",
"note": "Test note for the aggregated highlight from the Apple iPhone User Guide\nalong with a new line to test the preservation of indentation",
"highlightLocation": "aggregated-highlight-link-from-the-apple-iphone-user-guide",
"highlightStyle": 3,
Expand Down
18 changes: 0 additions & 18 deletions test/mocks/detailsData.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/mocks/rawTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Number of annotations:: {{annotations.length}}
{{/each}}
`;

export const rawCustomTemplateWrappedTextBlockMock = `Title:: 📕 {{{bookTitle}}}
export const rawCustomTemplateMockWithWrappedTextBlockContainingNewlines = `Title:: 📕 {{{bookTitle}}}
Author:: {{{bookAuthor}}}
Link:: [Apple Books Link](ibooks://assetid/{{bookId}})
Expand Down
27 changes: 18 additions & 9 deletions test/mocks/renderedTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Number of annotations:: 2
containing a new line to test the preservation of indentation
- 🎯 Highlight:: aggregated highlight from the Apple iPhone User Guide
containing a new line to test the preservation of indentation
and another new line
to check one more time
- 📝 Note:: Test note for the aggregated highlight from the Apple iPhone User Guide
along with a new line to test the preservation of indentation
- 📙 Highlight Link:: [Apple Books Highlight Link](ibooks://assetid/THBFYNJKTGFTTVCGSAE5#aggregated-highlight-link-from-the-apple-iphone-user-guide)
Expand Down Expand Up @@ -56,7 +58,9 @@ Number of annotations:: 2
- 🔖 Context:: This is a contextual text for the aggregated highlight from the Apple iPhone User Guide
containing a new line to test the preservation of indentation
- 🎯 Highlight:: <mark style="background:rgb(249,213,108); color:#000; padding:2px;">aggregated highlight from the Apple iPhone User Guide
containing a new line to test the preservation of indentation</mark>
containing a new line to test the preservation of indentation
and another new line
to check one more time</mark>
- 📝 Note:: Test note for the aggregated highlight from the Apple iPhone User Guide
along with a new line to test the preservation of indentation
- 📙 Highlight Link:: [Apple Books Highlight Link](ibooks://assetid/THBFYNJKTGFTTVCGSAE5#aggregated-highlight-link-from-the-apple-iphone-user-guide)
Expand All @@ -65,20 +69,25 @@ along with a new line to test the preservation of indentation
`;

export const renderedCustomTemplateWrappedTextBlockMock = `Title:: 📕 Designing Data-Intensive Applications
Author:: Kleppmann, Martin
Link:: [Apple Books Link](ibooks://assetid/28AEDF62F12B289C88BD6659BD6E50CC)
export const renderedCustomTemplateMockWithWrappedTextBlockContainingNewlines = `Title:: 📕 Apple iPhone - User Guide - Instructions - with - restricted - symbols - in - title
Author:: Apple Inc.
Link:: [Apple Books Link](ibooks://assetid/THBFYNJKTGFTTVCGSAE5)
## Annotations
Number of annotations:: 1
Number of annotations:: 2
----
> [!QUOTE]
> Chapter 1 introduces the terminology and approach
that we're going to use throughout this book. It examines what we actually mean by
words like reliability, scalability, and maintainability, and how
we can try to achieve these goals.
> aggregated highlight from the Apple iPhone User Guide
----
> [!QUOTE]
> aggregated highlight from the Apple iPhone User Guide
containing a new line to test the preservation of indentation
and another new line
to check one more time
`;
31 changes: 0 additions & 31 deletions test/preserveNewlineIndentation.spec.ts

This file was deleted.

10 changes: 8 additions & 2 deletions test/renderHighlightsTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Handlebars from 'handlebars';
import { describe, expect, test, vi } from 'vitest';
import { renderHighlightsTemplate } from '../src/methods/renderHighlightsTemplate';
import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { rawCustomTemplateMock } from './mocks/rawTemplates';
import { defaultTemplateMock, renderedCustomTemplateMock } from './mocks/renderedTemplate';
import { rawCustomTemplateMock, rawCustomTemplateMockWithWrappedTextBlockContainingNewlines } from './mocks/rawTemplates';
import { defaultTemplateMock, renderedCustomTemplateMock, renderedCustomTemplateMockWithWrappedTextBlockContainingNewlines } from './mocks/renderedTemplate';
import defaultTemplate from '../src/template';
import { ICombinedBooksAndHighlights } from 'src/types';

Expand All @@ -31,6 +31,12 @@ describe('renderHighlightsTemplate', () => {

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

test('Should render a custom template with the provided data and preserve newlines in wrapped text blocks', async () => {
const renderedTemplate = await renderHighlightsTemplate(aggregatedHighlights[0] as ICombinedBooksAndHighlights, rawCustomTemplateMockWithWrappedTextBlockContainingNewlines);

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

describe('Custom Handlebars helpers', () => {
Expand Down
69 changes: 66 additions & 3 deletions test/saveHighlightsToVault.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import SaveHighlights from '../src/methods/saveHighlightsToVault';
import { AppleBooksHighlightsImportPluginSettings } from '../src/settings';
import { rawCustomTemplateMock, rawCustomTemplateMockWithWrappedTextBlockContainingNewlines } from './mocks/rawTemplates';
import { aggregatedHighlights } from './mocks/aggregatedDetailsData';
import { defaultTemplateMock } from './mocks/renderedTemplate';
import {
defaultTemplateMock,
renderedCustomTemplateMock,
renderedCustomTemplateMockWithWrappedTextBlockContainingNewlines,
} from './mocks/renderedTemplate';
import { ICombinedBooksAndHighlights } from '../src/types'

const mockVault = {
Expand Down Expand Up @@ -39,7 +47,11 @@ afterEach(() => {
const settings = new AppleBooksHighlightsImportPluginSettings();

describe('Save highlights to vault', () => {
test('Should save highlights to vault', async () => {
dayjs.extend(utc);
dayjs.extend(timezone);
const tzSpy = vi.spyOn(dayjs.tz, 'guess');

test('Should save highlights to vault using the default template', async () => {
// eslint-disable-next-line
const saveHighlights = new SaveHighlights({ vault: mockVault } as any, settings);
const spyGetAbstractFileByPath = vi.spyOn(mockVault, 'getAbstractFileByPath').mockReturnValue('ibooks-highlights');
Expand All @@ -62,7 +74,58 @@ describe('Save highlights to vault', () => {
);
});

test('Should skip saving highlights to vault highlights are not found', async () => {
test('Should save highlights to vault using the custom colored template', async () => {
tzSpy.mockImplementation(() => 'America/New_York');

settings.template = rawCustomTemplateMock;

// 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 as ICombinedBooksAndHighlights[]);

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`,
renderedCustomTemplateMock
);
});

test('Should save highlights to vault using the custom template with wrapped text block', async () => {
settings.template = rawCustomTemplateMockWithWrappedTextBlockContainingNewlines;
// 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 as ICombinedBooksAndHighlights[]);

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`,
renderedCustomTemplateMockWithWrappedTextBlockContainingNewlines
);
});

test('Should skip saving highlights to vault if 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('');
Expand Down

0 comments on commit 0ed9ccc

Please sign in to comment.