Skip to content

Commit

Permalink
test: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 11, 2024
1 parent 5f156ae commit e597cab
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 173 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/app/prepare/prepareRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const HMR_CODE = `
if (import.meta.webpackHot) {
import.meta.webpackHot.accept()
__VUE_HMR_RUNTIME__.updateRoutes?.(${ROUTES_VAR_NAME})
__VUE_HMR_RUNTIME__.updateRedirects?.(${REDIRECTS_VAR_NAME}})
__VUE_HMR_RUNTIME__.updateRedirects?.(${REDIRECTS_VAR_NAME})
}
if (import.meta.hot) {
Expand Down
242 changes: 120 additions & 122 deletions packages/core/src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,131 @@ import type { PageBase, PageData, PageFrontmatter } from '@vuepress/shared'
/**
* Vuepress Page
*/
export type Page<
export interface Page<
ExtraPageData extends Record<string, unknown> = Record<string, unknown>,
ExtraPageFrontmatter extends Record<string, unknown> = Record<
string,
unknown
>,
ExtraPageFields extends Record<string, unknown> = Record<string, unknown>,
> = ExtraPageFields &
PageBase<ExtraPageFrontmatter> & {
/**
* Data of the page, which will be available in client code
*/
data: PageData<ExtraPageData, ExtraPageFrontmatter>

/**
* Raw Content of the page
*/
content: string

/**
* Rendered content of the page
*/
contentRendered: string

/**
* Date of the page, in 'yyyy-MM-dd' format
*
* @example '2020-09-09'
*/
date: string

/**
* Dependencies of the page
*/
deps: string[]

/**
* Links of the page
*/
links: MarkdownLink[]

/**
* Markdown env object of the page
*/
markdownEnv: Record<string, unknown>

/**
* Path of the page that inferred from file path
*
* If the page does not come from a file, it would be `null`
*
* @example '/guide/index.html'
*/
pathInferred: string | null

/**
* Locale path prefix of the page
*
* @example '/getting-started.html' -> '/'
* @example '/en/getting-started.html' -> '/en/'
* @example '/zh/getting-started.html' -> '/zh/'
*/
pathLocale: string

/**
* Permalink of the page
*
* If the page does not have a permalink, it would be `null`
*/
permalink: string | null

/**
* Custom data to be attached to route record
*/
routeMeta: Record<string, unknown>

/**
* Extracted sfc blocks of the page
*/
sfcBlocks: MarkdownSfcBlocks

/**
* Slug of the page
*/
slug: string

/**
* Source file path
*
* If the page does not come from a file, it would be `null`
*/
filePath: string | null

/**
* Source file path relative to source directory
*
* If the page does not come from a file, it would be `null`
*/
filePathRelative: string | null

/**
* Chunk file path
*/
chunkFilePath: string

/**
* Chunk file path relative to temp directory
*/
chunkFilePathRelative: string

/**
* Chunk name
*
* This will only take effect in webpack
*/
chunkName: string

/**
* Rendered html file path
*/
htmlFilePath: string

/**
* Rendered html file path relative to dest directory
*/
htmlFilePathRelative: string
}
> extends PageBase<ExtraPageFrontmatter> {
/**
* Data of the page, which will be available in client code
*/
data: PageData<ExtraPageData, ExtraPageFrontmatter>

/**
* Raw Content of the page
*/
content: string

/**
* Rendered content of the page
*/
contentRendered: string

/**
* Date of the page, in 'yyyy-MM-dd' format
*
* @example '2020-09-09'
*/
date: string

/**
* Dependencies of the page
*/
deps: string[]

/**
* Links of the page
*/
links: MarkdownLink[]

/**
* Markdown env object of the page
*/
markdownEnv: Record<string, unknown>

/**
* Path of the page that inferred from file path
*
* If the page does not come from a file, it would be `null`
*
* @example '/guide/index.html'
*/
pathInferred: string | null

/**
* Locale path prefix of the page
*
* @example '/getting-started.html' -> '/'
* @example '/en/getting-started.html' -> '/en/'
* @example '/zh/getting-started.html' -> '/zh/'
*/
pathLocale: string

/**
* Permalink of the page
*
* If the page does not have a permalink, it would be `null`
*/
permalink: string | null

/**
* Custom data to be attached to route record
*/
routeMeta: Record<string, unknown>

/**
* Extracted sfc blocks of the page
*/
sfcBlocks: MarkdownSfcBlocks

/**
* Slug of the page
*/
slug: string

/**
* Source file path
*
* If the page does not come from a file, it would be `null`
*/
filePath: string | null

/**
* Source file path relative to source directory
*
* If the page does not come from a file, it would be `null`
*/
filePathRelative: string | null

/**
* Chunk file path
*/
chunkFilePath: string

/**
* Chunk file path relative to temp directory
*/
chunkFilePathRelative: string

/**
* Chunk name
*
* This will only take effect in webpack
*/
chunkName: string

/**
* Rendered html file path
*/
htmlFilePath: string

/**
* Rendered html file path relative to dest directory
*/
htmlFilePathRelative: string
}

/**
* Options to create vuepress page
Expand Down
12 changes: 3 additions & 9 deletions packages/core/tests/page/createPage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,13 @@ describe('should work without plugins', () => {
expect(page.filePathRelative).toBeNull()
expect(page.htmlFilePath).toBe(app.dir.dest(`index.html`))
expect(page.htmlFilePathRelative).toBe(`index.html`)
expect(page.componentFilePath).toBe(
app.dir.temp(`pages/${page.htmlFilePathRelative}.vue`),
)
expect(page.componentFilePathRelative).toBe(
`pages/${page.htmlFilePathRelative}.vue`,
)
expect(page.chunkFilePath).toBe(
app.dir.temp(`pages/${page.htmlFilePathRelative}.js`),
app.dir.temp(`pages/${page.htmlFilePathRelative}.vue`),
)
expect(page.chunkFilePathRelative).toBe(
`pages/${page.htmlFilePathRelative}.js`,
`pages/${page.htmlFilePathRelative}.vue`,
)
expect(page.chunkName).toBeTruthy()
expect(page.chunkName).toBe(`index.html`)
})
})

Expand Down
23 changes: 20 additions & 3 deletions packages/core/tests/page/resolvePageChunkInfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,32 @@ const app = createBaseApp({
bundler: {} as Bundler,
})

it('should resolve page chunk info correctly', () => {
it('should resolve page chunk info correctly without source file path', () => {
const resolved = resolvePageChunkInfo({
app,
filePath: null,
filePathRelative: null,
htmlFilePathRelative: 'foo.html',
})

expect(resolved).toEqual({
chunkFilePath: app.dir.temp('pages/foo.html.js'),
chunkFilePathRelative: 'pages/foo.html.js',
chunkFilePath: app.dir.temp('pages/foo.html.vue'),
chunkFilePathRelative: 'pages/foo.html.vue',
chunkName: sanitizeFileName('foo.html'),
})
})

it('should resolve page chunk info correctly with source file path', () => {
const resolved = resolvePageChunkInfo({
app,
filePath: app.dir.source('foo.md'),
filePathRelative: 'foo.md',
htmlFilePathRelative: 'foo.html',
})

expect(resolved).toEqual({
chunkFilePath: app.dir.source('foo.md'),
chunkFilePathRelative: 'foo.md',
chunkName: sanitizeFileName('foo.html'),
})
})
38 changes: 0 additions & 38 deletions packages/core/tests/page/resolvePageComponentInfo.spec.ts

This file was deleted.

0 comments on commit e597cab

Please sign in to comment.