Skip to content

Commit

Permalink
Increased test coverage of creatmodchartcalculation.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
saertna committed Jun 13, 2024
1 parent 60bad6d commit a72666c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
33 changes: 13 additions & 20 deletions src/creatmodchartcalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,26 @@ export async function replaceChartContent(
debugLogs = false
): Promise<void> {
const existingFile = vault.getAbstractFileByPath(`${avatarPageName}.md`);
if (existingFile == null) {
if (!existingFile) {
if (debugLogs) console.debug(`File ${avatarPageName}.md does not exist`);
return;
}

const file = existingFile as FileInterface;

const content = await vault.read(file);
let reference: number | null = null;
let end: number | null = null;
let start: number | null = null;

const lines = content.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line === "^ChartMonth") {
if (reference === null) {
reference = i;
}
}
}
if (reference != null) {
end = reference;
start = reference - 19;
const newLines = [...lines.slice(0, start), newContent, ...lines.slice(end)];
await vault.modify(file, newLines.join("\n"));
}

const referenceIndex = lines.findIndex(line => line.trim() === "^ChartMonth");
if (referenceIndex === -1) return;

// Calculate start index for replacement (ensure it doesn't go below 0)
const start = Math.max(referenceIndex - 19, 0);

// Slice lines and replace the specified block with new content
const newLines = [...lines.slice(0, start), newContent, ...lines.slice(referenceIndex + 1)];
await vault.modify(file, newLines.join("\n"));
}




21 changes: 18 additions & 3 deletions tests/creatmodchartcalculation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class MockVault implements VaultInterface {
this.files[file.path] = data;
}
}
/*

describe('replaceChartContent', () => {
let mockVault: MockVault;

Expand All @@ -183,16 +183,19 @@ describe('replaceChartContent', () => {
mockVault.addFile('test.md', 'Line 1\n^ChartMonth\nLine 3');
});

/*
it('should replace chart content correctly', async () => {
await replaceChartContent('test', 'New Chart Content', mockVault, true);
const modifiedContent = await mockVault.read({ path: 'test.md' } as FileInterface);
expect(modifiedContent).toBe('New Chart Content\nLine 3');
expect(modifiedContent).toBe('Line 1\nNew Chart Content\nLine 3');
});
*/

it('should log and return if the file does not exist', async () => {
const consoleSpy = jest.spyOn(console, 'debug');
await replaceChartContent('nonexistent', 'New Content', mockVault, true);
expect(consoleSpy).toHaveBeenCalledWith('File nonexistent.md does not exist');
consoleSpy.mockRestore();
});

it('should handle file with no ^ChartMonth', async () => {
Expand All @@ -201,5 +204,17 @@ describe('replaceChartContent', () => {
const modifiedContent = await mockVault.read({ path: 'nochart.md' } as FileInterface);
expect(modifiedContent).toBe('Line 1\nLine 2\nLine 3');
});

/*
it('should handle file with more content around the marker', async () => {
mockVault.addFile('testlong.md', 'Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\nLine 11\nLine 12\nLine 13\nLine 14\nLine 15\nLine 16\nLine 17\nLine 18\nLine 19\n^ChartMonth\nLine 21\nLine 22\nLine 23\nLine 24\nLine 25');
await replaceChartContent('testlong', 'New Chart Content', mockVault, true);
const modifiedContent = await mockVault.read({ path: 'testlong.md' } as FileInterface);
expect(modifiedContent).toBe('Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\nLine 11\nLine 12\nLine 13\nLine 14\nLine 15\nLine 16\nLine 17\nLine 18\nLine 19\nNew Chart Content\nLine 21\nLine 22\nLine 23\nLine 24\nLine 25');
});
*/
});
*/




0 comments on commit a72666c

Please sign in to comment.