-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor logic for docs versions dropdown and add tests (#2576)
- Loading branch information
1 parent
c185ddf
commit 0e4a9d0
Showing
5 changed files
with
426 additions
and
10 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,61 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { isReleaseNotesPage } from 'lib/docs/is-release-notes-page' | ||
|
||
describe('isReleaseNotesPage', () => { | ||
it('returns true for valid release notes page paths', () => { | ||
const validPaths = [ | ||
'v202409-2/releases/2024/v202407-1', | ||
'releases/2022/v220601-1', | ||
'releases/2021/v210601-2', | ||
'release-notes/1.2.3', | ||
'release-notes/2.0.x', | ||
'/release-notes/v2.0.x', | ||
'/boundary/docs/release-notes/v0_15_0', | ||
'/vault/docs/release-notes/1.13.0', | ||
] | ||
|
||
validPaths.forEach((path) => { | ||
expect(isReleaseNotesPage(path)).toBe(true) | ||
}) | ||
}) | ||
|
||
it('returns false for invalid release notes page paths', () => { | ||
const invalidPaths = [ | ||
'releases/2022/v220601', | ||
'releases/2021/v210601', | ||
'release-notes/1.2', | ||
'release-notes/2.0', | ||
'release-notes/2.x', | ||
'releases/2022/v220601-', | ||
'releases/2021/v210601-', | ||
'/release-notes/1.2.', | ||
'/release-notes/2.0.', | ||
'/release-notes/2.x.', | ||
'/releases/2022/v220601-1234-5678', | ||
'/releases/2021/v210601-5678-1234', | ||
'/release-notes/1.2.3.4', | ||
'/release-notes/2.0.x.y', | ||
] | ||
invalidPaths.forEach((path) => { | ||
expect(isReleaseNotesPage(path)).toBe(false) | ||
}) | ||
}) | ||
|
||
it('returns false for non-release notes page paths', () => { | ||
const nonReleaseNotesPaths = [ | ||
'/releases', | ||
'/getting-started', | ||
'/enterprise/v202401-1/migrate', | ||
'/enterprise/v202401-1/releases', | ||
'/waypoint/reference/config', | ||
'/vault/install', | ||
] | ||
nonReleaseNotesPaths.forEach((path) => { | ||
expect(isReleaseNotesPage(path)).toBe(false) | ||
}) | ||
}) | ||
}) |
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,25 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
/** | ||
* Determines if a given path corresponds to a release notes page. | ||
* | ||
* This function uses a regular expression to check if the provided path matches | ||
* the expected patterns for release notes pages. The patterns include: | ||
* - `vYYYYMM-NN/releases/YYYY/vYYYYMM-NN` | ||
* - `releases/YYYY/vYYYYMM-NN` | ||
* - `/releases/YYYY/vYYYYMM-NN` | ||
* - `release-notes/vX.X.X` or `/release-notes/X.X.X` | ||
* | ||
* @param path - The path to be checked. | ||
* @returns `true` if the path matches the release notes pattern, otherwise `false`. | ||
*/ | ||
export const isReleaseNotesPage = (path: string): boolean => { | ||
const regexPatterns = [ | ||
/(\/?releases\/\d{4}\/(v\d{6}-\d{1}))$/i, | ||
/\/?release-notes\/(v\d+[.|_]|(\d+[.|_]))\d+[.|_]([0-9]|x)$/i, | ||
] | ||
return regexPatterns.some((pattern) => pattern.test(path)) | ||
} |
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
170 changes: 170 additions & 0 deletions
170
src/views/docs-view/utils/__tests__/fetch-valid-versions.test.ts
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,170 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { describe, it, expect, vi } from 'vitest' | ||
import { isReleaseNotesPage } from 'lib/docs/is-release-notes-page' | ||
import { getValidVersions } from '../get-valid-versions' | ||
import { VersionSelectItem } from '../../loaders/remote-content' | ||
import { fetchValidVersions } from 'views/docs-view/server' | ||
|
||
vi.mock('lib/docs/is-release-notes-page') | ||
vi.mock('../get-valid-versions') | ||
|
||
describe('fetchValidVersions', () => { | ||
const versions: VersionSelectItem[] = [ | ||
{ | ||
version: 'v1.0.0', | ||
name: 'v1.0.0', | ||
label: 'v1.0.0', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
{ | ||
version: 'v2.0.0', | ||
name: '', | ||
label: '', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
] | ||
|
||
const mockIsReleaseNotesPage = (returnValue: boolean) => { | ||
vi.mocked(isReleaseNotesPage).mockReturnValue(returnValue) | ||
} | ||
|
||
const mockGetValidVersions = (returnValue: VersionSelectItem[]) => { | ||
vi.mocked(getValidVersions).mockResolvedValue(returnValue) | ||
} | ||
|
||
const runTest = async ( | ||
pathParts: string[], | ||
versionPathPart: string, | ||
basePathForLoader: string, | ||
productSlugForLoader: string, | ||
expectedPath: string, | ||
expectedVersions: VersionSelectItem[] | ||
) => { | ||
const result = await fetchValidVersions( | ||
pathParts, | ||
versionPathPart, | ||
basePathForLoader, | ||
versions, | ||
productSlugForLoader | ||
) | ||
|
||
expect(isReleaseNotesPage).toHaveBeenCalledWith(pathParts.join('/')) | ||
expect(getValidVersions).toHaveBeenCalledWith( | ||
versions, | ||
expectedPath, | ||
productSlugForLoader | ||
) | ||
expect(result).toEqual(expectedVersions) | ||
} | ||
|
||
it('should filter versions correctly for non-release notes pages', async () => { | ||
const pathParts = ['docs', 'v1.0.0', 'guide'] | ||
const versionPathPart = 'v1.0.0' | ||
const basePathForLoader = '/base/path' | ||
const productSlugForLoader = 'product-slug' | ||
|
||
mockIsReleaseNotesPage(false) | ||
mockGetValidVersions([ | ||
{ | ||
version: 'v1.0.0', | ||
name: 'v1.0.0', | ||
label: 'v1.0.0', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
]) | ||
|
||
await runTest( | ||
pathParts, | ||
versionPathPart, | ||
basePathForLoader, | ||
productSlugForLoader, | ||
'doc#/base/path/docs/guide', | ||
[ | ||
{ | ||
version: 'v1.0.0', | ||
name: 'v1.0.0', | ||
label: 'v1.0.0', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
] | ||
) | ||
}) | ||
|
||
it('should filter versions correctly for release notes pages', async () => { | ||
const pathParts = ['v202409-2', 'releases', '2024', 'v202409-1'] | ||
const versionPathPart = 'v202409-2' | ||
const basePathForLoader = 'enterprise' | ||
const productSlugForLoader = 'ptfe-releases' | ||
const releaseNotesVersions: VersionSelectItem[] = [ | ||
{ | ||
name: 'latest', | ||
label: 'v202409-2 (latest)', | ||
isLatest: true, | ||
releaseStage: 'stable', | ||
version: 'v202409-2', | ||
}, | ||
{ | ||
name: 'v202409-1', | ||
label: 'v202409-1', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
version: 'v202409-1', | ||
}, | ||
] | ||
|
||
mockIsReleaseNotesPage(true) | ||
mockGetValidVersions(releaseNotesVersions) | ||
|
||
await runTest( | ||
pathParts, | ||
versionPathPart, | ||
basePathForLoader, | ||
productSlugForLoader, | ||
'doc#enterprise/releases/2024/v202409-1', | ||
releaseNotesVersions | ||
) | ||
}) | ||
|
||
it('should handle paths without versions correctly', async () => { | ||
const pathParts = ['docs', 'guide'] | ||
const versionPathPart = 'v1.0.0' | ||
const basePathForLoader = '/base/path' | ||
const productSlugForLoader = 'product-slug' | ||
|
||
mockIsReleaseNotesPage(false) | ||
mockGetValidVersions([ | ||
{ | ||
version: 'v1.0.0', | ||
name: 'v1.0.0', | ||
label: 'v1.0.0', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
]) | ||
|
||
await runTest( | ||
pathParts, | ||
versionPathPart, | ||
basePathForLoader, | ||
productSlugForLoader, | ||
'doc#/base/path/docs/guide', | ||
[ | ||
{ | ||
version: 'v1.0.0', | ||
name: 'v1.0.0', | ||
label: 'v1.0.0', | ||
isLatest: false, | ||
releaseStage: 'stable', | ||
}, | ||
] | ||
) | ||
}) | ||
}) |
Oops, something went wrong.