add some tests

This commit is contained in:
Gauthier Roebroeck 2025-06-11 12:26:18 +08:00
parent 672518683c
commit 28ef33ee36
5 changed files with 1016 additions and 24 deletions

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@
"build": "run-s i18n:compile \"build-only {@}\" --",
"preview": "vite preview",
"build-only": "vite build",
"test": "vitest",
"type-check": "vue-tsc --build --force",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
@ -45,6 +46,7 @@
"eslint-config-prettier": "^10.1.5",
"eslint-plugin-formatjs": "^5.3.1",
"eslint-plugin-vue": "^10.1.0",
"jsdom": "^26.1.0",
"npm-run-all2": "^8.0.4",
"openapi-typescript": "^7.8.0",
"prettier": "^3.5.3",
@ -60,6 +62,7 @@
"vite-plugin-dir2json": "^1.3.0",
"vite-plugin-vue-layouts-next": "^0.1.3",
"vite-plugin-vuetify": "^2.0.3",
"vitest": "^3.2.3",
"vue-router": "^4.4.0",
"vue-tsc": "^2.1.10"
}

View file

@ -0,0 +1,44 @@
// @vitest-environment jsdom
import { beforeAll, expect, test, vi } from 'vitest'
import { loadLocale, defaultLocale, setLocale, getLocale, availableLocales } from './locale-helper'
beforeAll(() => {
// mock the available locales, as locales are checked against what's available
vi.mock('../i18n?dir2json&ext=.json&1', () => {
return {
default: {
en: {
sample: 'sample',
},
fr: {
sample: 'échantillon',
},
} as Record<string, Record<string, string>>,
}
})
})
test('given available locales when getting available locales then they are returned', () => {
expect(Object.keys(availableLocales)).toStrictEqual(['en', 'fr'])
})
test('when trying to load unknown locale then default locale is loaded', () => {
const localeDefault = loadLocale(defaultLocale)
const localeUnknown = loadLocale('unknown')
expect(localeUnknown).toBe(localeDefault)
})
test('when setting locale then it is persisted', () => {
const spy = vi.fn(() => {})
vi.spyOn(window, 'location', 'get').mockReturnValue({
reload: spy,
} as unknown as Location)
setLocale('fr')
const newLocale = getLocale()
expect(newLocale).toBe('fr')
expect(spy).toHaveBeenCalledOnce()
})

View file

@ -42,7 +42,7 @@ export const availableLocales = loadAvailableLocales()
* Gets the saved locale from localStorage.
* If the locale is not valid, defaults to 'en'.
*/
function getLocale(): string {
export function getLocale(): string {
const storageLocale = localStorage.getItem('userLocale') ?? defaultLocale
return storageLocale in availableLocales ? storageLocale : defaultLocale
}

View file

@ -1,3 +1,5 @@
/// <reference types="vitest/config" />
// Plugins
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
@ -81,4 +83,7 @@ export default defineConfig({
},
},
},
test: {
restoreMocks: true,
},
})