Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add locale unit test #388

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions __test__/locale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect } from 'vitest'
import { resolve } from 'node:path'
import { readdirSync } from 'node:fs'
import { Language } from '../utils/getLanguage'
import { includeAllKeys, excludeKeys } from './utils'

const locales = readdirSync(resolve(__dirname, '../locales')).filter((file) => {
return file.includes('.json')
})

describe('should match name regex', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
/**
*
* both can match normal locale or reusable locale
*
* @example normal locale: en-US
* @example reusable locale: zh-Hant
*/
const regex = /^[a-zA-Z]{2}(-[a-zA-Z]{2})*.json$|^[a-zA-Z]{2}(-[a-zA-z]{4})*.json$/

locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(locale).toMatch(regex)
})
})
})

describe('should include full keys', () => {
const structure = require('../schema/locale.json') as Language
locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(includeAllKeys(require(`../locales/${locale}`), structure)).toBeTruthy()
})
})
})

describe("should not include extra keys", () => {
const structure = require('../schema/locale.json') as Language
locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(excludeKeys(require(`../locales/${locale}`), structure)).toBeTruthy()
})
})
})
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 48 additions & 0 deletions __test__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether missed some keys
*/
export function includeAllKeys(obj: Object, schema: Object) {
for (let key in schema) {
if (!obj.hasOwnProperty(key)) {
console.log(`key '${key}' lost`)
return false
}
if (schema[key] !== null) {
if (typeof schema[key] === 'string') {
if (typeof obj[key] !== schema[key]) {
console.error(`the type of ${key} is incorrect`)
return false
}
} else if (typeof schema[key] === 'object') {
if (!includeAllKeys(obj[key], schema[key])) {
return false
}
}
}
}
return true
}

/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether include extra keys
*/
export function excludeKeys(obj: Object, schema: Object) {
for (let key in obj) {
if (!schema.hasOwnProperty(key)) {
console.error(`unexpected key: ${key}`)
return false
}
if (schema[key] !== null && typeof schema[key] === 'object') {
if (!excludeKeys(obj[key], schema[key])) {
return false
}
}
}
return true
}
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
68 changes: 68 additions & 0 deletions schema/locale.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"projectName": {
"message": "string"
},
"shouldOverwrite": {
"dirForPrompts": {
"current": "string",
"target": "string"
},
"message": "string"
},
"packageName": {
"message": "string",
"invalidMessage": "string"
},
"needsTypeScript": {
"message": "string"
},
"needsJsx": {
"message": "string"
},
"needsRouter": {
"message": "string"
},
"needsPinia": {
"message": "string"
},
"needsVitest": {
"message": "string"
},
"needsE2eTesting": {
"message": "string",
"hint": "string",
"selectOptions": {
"negative": {
"title": "string"
},
"cypress": {
"title": "string",
"desc": "string"
},
"nightwatch": {
"title": "string",
"desc": "string"
},
"playwright": {
"title": "string"
}
}
},
"needsEslint": {
"message": "string"
},
"needsPrettier": {
"message": "string"
},
"errors": {
"operationCancelled": "string"
},
"defaultToggleOptions": {
"active": "string",
"inactive": "string"
},
"infos": {
"scaffolding": "string",
"done": "string"
}
}
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion utils/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface LanguageItem {
}
}

interface Language {
export interface Language {
projectName: LanguageItem
shouldOverwrite: LanguageItem
packageName: LanguageItem
Expand Down