Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdvlpr committed Dec 27, 2024
1 parent e52b3d3 commit 2f10626
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
10 changes: 10 additions & 0 deletions docs/utils/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest';

import { fetchLatestVersion } from '../api';

describe('fetchLatestVersion', () => {
it('should fetch the latest version', async () => {
const version = await fetchLatestVersion();
expect(version).toBe('v1.2.3');
});
});
15 changes: 15 additions & 0 deletions docs/utils/__tests__/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';

import { camelToKebabCase, kebabToCamelCase } from '../general';

describe('camelToKebabCase', () => {
it('should convert camelCase to kebab-case', () => {
expect(camelToKebabCase('camelCaseString')).toBe('camel-case-string');
});
});

describe('kebabToCamelCase', () => {
it('should convert kebab-case to camelCase', () => {
expect(kebabToCamelCase('kebab-case-string')).toBe('kebabCaseString');
});
});
2 changes: 2 additions & 0 deletions docs/utils/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,6 @@ export const mapThemeConfig = (
],
sidebarMenuLabel: msg.sidebarMenuLabel,
siteTitle: msg.title,
// @ts-expect-error: `skipToContentLabel` is missing in `DefaultTheme`.
skipToContentLabel: msg.skipToContentLabel,
});
60 changes: 60 additions & 0 deletions test/vitest/__tests__/locales.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import docsMessages from 'app/docs/locales';
import { readdir, readFile } from 'fs-extra';
import { enabled, locales as localeOptions } from 'src/constants/locales';
import appMessages from 'src/i18n';
import { camelToKebabCase } from 'src/utils/general';
import { resolve } from 'upath';
import { describe, expect, it } from 'vitest';

describe('Locales', () => {
it('should be defined', async () => {
const locales = localeOptions.map((l) => l.value).sort();
const localesKebab = locales.map(camelToKebabCase).sort();

const srcMessages = Object.keys(appMessages);
const docsLocaleMessages = Object.keys(docsMessages).sort();

const docsLocaleFiles = (
await readdir(resolve(__dirname, '../../../docs/locales'))
)
.filter((f) => f.endsWith('.json'))
.map((f) => f.replace('.json', ''))
.sort();

const docsSrcFolders = (
await readdir(resolve(__dirname, '../../../docs/src'))
)
.filter((f) => f !== 'assets' && f !== 'public')
.sort();

const srcLocaleFiles = (
await readdir(resolve(__dirname, '../../../src/i18n'))
)
.filter((f) => f.endsWith('.json'))
.map((f) => f.replace('.json', ''))
.sort();

expect(locales).toEqual(docsLocaleMessages);
expect(localesKebab).toEqual(docsLocaleFiles);
expect(localesKebab).toEqual(docsSrcFolders);
expect(localesKebab).toEqual(srcLocaleFiles);
expect(enabled).toEqual(srcMessages);
});

it('should not have unused keys for docs', async () => {
const localeMapper = await readFile(
resolve(__dirname, '../../../docs/utils/locales.ts'),
'utf-8',
);

const config = await readFile(
resolve(__dirname, '../../../docs/.vitepress/config.mts'),
'utf-8',
);

const docsKeys = Object.keys(docsMessages.en);
docsKeys.forEach((key) => {
expect(localeMapper + config).toContain(key);
});
});
});
18 changes: 8 additions & 10 deletions test/vitest/setup-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pkg from 'app/package.json';
import { vol } from 'memfs';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
Expand All @@ -21,20 +20,19 @@ export const restHandlers = [
return HttpResponse.json(jwYeartext);
}),
http.get(
`${pkg.repository.url
.replace('.git', '')
.replace('github.com', 'api.github.com/repos')}/releases`,
`${process.env.repository?.replace(
'github.com',
'api.github.com/repos',
)}/releases`,
() => {
return HttpResponse.json(releases);
},
),
http.get(
`${pkg.repository.url
.replace('.git', '')
.replace(
'github',
'raw.githubusercontent',
)}/refs/heads/master/announcements.json`,
`${process.env.repository?.replace(
'github',
'raw.githubusercontent',
)}/refs/heads/master/announcements.json`,
() => {
return HttpResponse.json(announcements);
},
Expand Down
1 change: 1 addition & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default defineConfig({
include: [
// Matches vitest tests in any subfolder of 'src' or into 'test/vitest/__tests__'
// Matches all files with extension 'js', 'jsx', 'ts' and 'tsx'
'docs/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
'src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
'test/vitest/__tests__/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
],
Expand Down

0 comments on commit 2f10626

Please sign in to comment.