From 9ec65119000e3873eab788423eda823f36401a3f Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Fri, 26 Aug 2022 17:53:30 +0800 Subject: [PATCH] feat(ut): Add test --- package-lock.json | 23 ++++++++++++++++++++- package.json | 1 + tools/font-icon/src/index.js | 28 +++++++++++++++++--------- tools/font-icon/test/font-icon.test.js | 18 +++++++++++++++++ 4 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 tools/font-icon/test/font-icon.test.js diff --git a/package-lock.json b/package-lock.json index 96c5164..9110a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ }, "devDependencies": { "@jsdevtools/npm-publish": "^1.4.3", + "@types/jest": "^28.1.8", "@types/node": "^17.0.26", "jest": "28.1.3", "lerna": "^5.3.0", @@ -2634,6 +2635,16 @@ "@types/istanbul-lib-report": "*" } }, + "node_modules/@types/jest": { + "version": "28.1.8", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-28.1.8.tgz", + "integrity": "sha512-8TJkV++s7B6XqnDrzR1m/TT0A0h948Pnl/097veySPN67VRAgQ4gZ7n2KfJo2rVq6njQjdxU3GCCyDvAeuHoiw==", + "dev": true, + "dependencies": { + "expect": "^28.0.0", + "pretty-format": "^28.0.0" + } + }, "node_modules/@types/json5": { "version": "0.0.29", "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", @@ -10632,7 +10643,7 @@ }, "tools/common": { "name": "momentum-constructor-common", - "version": "1.0.5", + "version": "1.0.6", "license": "ISC", "dependencies": { "momentum-abstract": "^1.0.5" @@ -12787,6 +12798,16 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "28.1.8", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-28.1.8.tgz", + "integrity": "sha512-8TJkV++s7B6XqnDrzR1m/TT0A0h948Pnl/097veySPN67VRAgQ4gZ7n2KfJo2rVq6njQjdxU3GCCyDvAeuHoiw==", + "dev": true, + "requires": { + "expect": "^28.0.0", + "pretty-format": "^28.0.0" + } + }, "@types/json5": { "version": "0.0.29", "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", diff --git a/package.json b/package.json index c8eba44..b534c90 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/momentum-design/momentum-constructor#readme", "devDependencies": { "@jsdevtools/npm-publish": "^1.4.3", + "@types/jest": "^28.1.8", "@types/node": "^17.0.26", "jest": "28.1.3", "lerna": "^5.3.0", diff --git a/tools/font-icon/src/index.js b/tools/font-icon/src/index.js index 43e6537..fda9ae8 100644 --- a/tools/font-icon/src/index.js +++ b/tools/font-icon/src/index.js @@ -44,7 +44,7 @@ function replaceLast(str, ch, cr) { return `${str.substring(0, lp)}${cr}${str.substring(lp + 1)}`; } -async function generateSVGPattern(svgPath, content, tokenRegExp) { +async function generateSVGPattern(svgPath, content, tokenRegExp, isProd) { if (!isProd) { return `${svgPath}/*.svg`; } @@ -71,8 +71,11 @@ async function generateIconsJson(fontName, fontPath, tokenPath) { return [`icon-${name}`, codePointHex] }) - const iconsJson = Object.fromEntries(glyphs); - const tokenFile = `${tokenPath}/icons.json` + return Object.fromEntries(glyphs); +} + +async function writeIconsJson(iconsJson, tokenPath) { + const tokenFile = path.resolve(tokenPath, 'icons.json') await fs.writeFile(tokenFile, JSON.stringify(iconsJson, null, 2), 'utf-8'); } @@ -81,22 +84,26 @@ async function loadSvgFont(file) { return await xml2js.parseStringPromise(svgFile); } -async function build(){ - let config; +async function build(_config, _isProd = isProd, _dry = false){ + let config = _config; try { - config = require(path.resolve(configFile)); + if (!config) { + config = require(path.resolve(configFile)); + } } catch(e) { console.error(`Please run 'npx ${NAME} --i', before build!`) return } const { svgPath, fontName, fontPath, content, tokenRegExp, tokenPath } = config; - const svgPattern = await generateSVGPattern(svgPath, content, tokenRegExp); + const svgPattern = await generateSVGPattern(svgPath, content, tokenRegExp, _isProd); await generateFonts(fontName, svgPattern, fontPath, {ts: Date.now(), fontHeight: 1000}); - await generateIconsJson(fontName, fontPath, tokenPath); + + const iconsJson = await generateIconsJson(fontName, fontPath); + return _dry ? iconsJson : await writeIconsJson(iconsJson, tokenPath); } -async function init() { +async function init(_dry = false) { const pattern = /tw-icon-([\w-]+)/g; const config = { svgPath: 'node_modules/momentum-abstract/icon', @@ -110,7 +117,8 @@ async function init() { }, tokenPath: '.', } - await fs.writeFile(configFile, `module.exports = ${JSON.stringify(config, null, 2)}`, 'utf-8'); + + return _dry ? config : await fs.writeFile(configFile, `module.exports = ${JSON.stringify(config, null, 2)}`, 'utf-8'); } module.exports = { diff --git a/tools/font-icon/test/font-icon.test.js b/tools/font-icon/test/font-icon.test.js new file mode 100644 index 0000000..abad736 --- /dev/null +++ b/tools/font-icon/test/font-icon.test.js @@ -0,0 +1,18 @@ +const fontIcon = require('../src') + +describe("Font icon: ",()=>{ + test('Init', async () => { + const config = await fontIcon.init(true) + expect(config).toBeDefined(); + }); + + // tw-icon-arrow-down-bold + // tw-icon-arrow-up-filled + test('Build', async () => { + const config = await fontIcon.init(true) + config.content = ['tools/**/font-icon.test.js'] + const iconsJson = await fontIcon.build(config, true, true); + expect(iconsJson).toHaveProperty('icon-arrow-down-bold') + expect(iconsJson).toHaveProperty('icon-arrow-up-filled') + }) +});