-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(platform): cache content attributes if they have not changed (#561)
- Loading branch information
Showing
2 changed files
with
77 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, expect } from 'vitest'; | ||
import * as fs from 'fs'; | ||
|
||
vi.mock('fs'); | ||
|
||
import { contentPlugin } from './content-plugin'; | ||
|
||
describe('content plugin', () => { | ||
const [plugin] = contentPlugin(); | ||
const transform = (code: string, id: string): any => { | ||
// Use `any` because not of the signatures are callable and it also expects | ||
// to pass a valid `this` type. | ||
const pluginTransform: any = plugin.transform; | ||
return pluginTransform(code, id); | ||
}; | ||
|
||
it('should skip transforming code if there is no `analog-content-list` at the end', async () => { | ||
// Arrange | ||
const code = 'Some_code'; | ||
const id = '/src/content/post.md'; | ||
// Act & Assert | ||
expect(await transform(code, id)).toEqual(undefined); | ||
}); | ||
|
||
it('should cache parsed attributes if the code is the same', async () => { | ||
// Arrange | ||
const code = | ||
'---\n' + | ||
'title: My First Post\n' + | ||
'slug: 2022-12-27-my-first-post\n' + | ||
'description: My First Post Description\n' + | ||
'---\n' + | ||
'\n' + | ||
'Hello World\n'; | ||
const id = '/src/content/post.md?analog-content-list=true'; | ||
const readFileSyncSpy = vi.spyOn(fs, 'readFileSync').mockReturnValue(code); | ||
const result = | ||
'export default {"title":"My First Post","slug":"2022-12-27-my-first-post","description":"My First Post Description"}'; | ||
// Act & Assert | ||
expect(await transform(code, id)).toEqual(result); | ||
expect(await transform(code, id)).toEqual(result); | ||
// Ensure the `readFileSync` has been called only once. | ||
expect(readFileSyncSpy).toBeCalledTimes(1); | ||
}); | ||
}); |
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