Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Mar 6, 2021
1 parent 6f93289 commit 57a0a44
Show file tree
Hide file tree
Showing 20 changed files with 3,957 additions and 69 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

[*.{js,jsx,json,ts,tsx,yml}]
indent_size = 2
indent_style = space
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
root: true,
env: {
browser: true,
es2021: true,
es6: true,
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jest/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'jest'],
rules: {
'no-console': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
},
};
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: 'yarn'
directory: '/'
schedule:
interval: 'daily'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ pnpm-debug.log*
*.njsproj
*.sln
*.sw?

coverage
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
6 changes: 6 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# shellcheck source=./_/husky.sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
9 changes: 9 additions & 0 deletions .husky/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
command_exists () {
command -v "$1" >/dev/null 2>&1
}

# Workaround for Windows 10, Git Bash and Yarn
if command_exists winpty && test -t 1; then
exec < /dev/tty
fi
6 changes: 6 additions & 0 deletions .husky/lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
'*.{js,jsx,ts,tsx}': ['eslint --fix', 'prettier --write'],
'{!(package)*.json,*.code-snippets,.!(browserslist)*rc}': ['prettier --write--parser json'],
'package.json': ['prettier --write'],
'*.md': ['prettier --write'],
};
10 changes: 10 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"

[ -n "$CI" ] && exit 0

# Format and submit code according to lintstagedrc.js configuration
npm run lint:lint-staged

npm run lint:pretty
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/types/'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|js)x?$',
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}', '!src/**/*.d.ts'],
};
24 changes: 21 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
"build": "tsup src/index.ts --dts --format cjs,esm",
"prepublishOnly": "yarn build",
"log": "conventional-changelog -p angular -i CHANGELOG.md -s",
"example:dev": "npm -C example run serve",
"example:build": "npm -C example run build"
"lint:lint-staged": "lint-staged -c ./.husky/lintstagedrc.js",
"lint:pretty": "pretty-quick --staged",
"lint:eslint": "eslint \"src/**/*.{ts,tsx}\" --fix",
"test": "jest --coverage",
"test:watch": "jest --watch",
"install:husky": "is-ci || husky install",
"postinstall": "npm run install:husky"
},
"keywords": [
"vite",
Expand Down Expand Up @@ -46,15 +51,28 @@
"vite": ">=2.0.0"
},
"devDependencies": {
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"@types/debug": "^4.1.5",
"@types/etag": "^1.8.0",
"@types/fs-extra": "^9.0.8",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.31",
"@types/svgo": "^1.3.4",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"commitizen": "^4.2.3",
"conventional-changelog-cli": "^2.1.1",
"ini": "^2.0.0",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-jest": "^24.1.5",
"husky": "^5.1.3",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.3",
"tsup": "^4.6.1",
"typescript": "^4.2.3",
"vite": "^2.0.5"
Expand Down
6 changes: 6 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ module.exports = {
tabWidth: 2,
},
},
{
files: '*.ts',
options: {
parser: 'typescript',
},
},
],
};
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Options as SvgoOptions } from 'svgo';

import fg from 'fast-glob';
import getEtag from 'etag';

// @ts-ignore
import { optimize } from 'svgo';
import fs from 'fs-extra';
Expand Down Expand Up @@ -49,6 +50,10 @@ export default (opt: ViteSvgIconsPlugin): Plugin => {
};

let { svgoOptions } = options;
const { symbolId } = options;
if (!symbolId.includes('[name]')) {
throw new Error('SymbolId must contain [name] string!');
}

if (svgoOptions) {
svgoOptions = typeof svgoOptions === 'boolean' ? {} : svgoOptions;
Expand Down Expand Up @@ -77,7 +82,7 @@ export default (opt: ViteSvgIconsPlugin): Plugin => {
},
configureServer: ({ middlewares }) => {
middlewares.use(async (req, res, next) => {
let url = req.url!;
const url = req.url!;
if (normalizePath(url) === `/@id/${SVG_ICONS_NAME}`) {
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Cache-Control', 'no-cache');
Expand All @@ -93,7 +98,7 @@ export default (opt: ViteSvgIconsPlugin): Plugin => {
};
};

async function createModuleCode(
export async function createModuleCode(
cache: Map<string, FileStats>,
svgoOptions: SvgoOptions,
options: ViteSvgIconsPlugin
Expand All @@ -120,7 +125,7 @@ async function createModuleCode(
* @param cache
* @param options
*/
async function compilerIcons(
export async function compilerIcons(
cache: Map<string, FileStats>,
svgOptions: SvgoOptions,
options: ViteSvgIconsPlugin
Expand Down Expand Up @@ -168,7 +173,7 @@ async function compilerIcons(
return insertHtml;
}

async function compilerIcon(
export async function compilerIcon(
file: string,
symbolId: string,
svgOptions: SvgoOptions
Expand All @@ -189,7 +194,7 @@ async function compilerIcon(
return svgSymbol.render();
}

function createSymbolId(name: string, options: ViteSvgIconsPlugin) {
export function createSymbolId(name: string, options: ViteSvgIconsPlugin) {
const { symbolId } = options;

if (!symbolId) {
Expand All @@ -211,7 +216,7 @@ function createSymbolId(name: string, options: ViteSvgIconsPlugin) {
return id.replace(path.extname(id), '');
}

function discreteDir(name: string) {
export function discreteDir(name: string) {
if (!normalizePath(name).includes('/')) {
return {
fileName: name,
Expand Down
23 changes: 23 additions & 0 deletions tests/createSymbolId.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createSymbolId } from '../src/index';

const TEST_SYMBOL_ID = 'icon-[dir]-[name]';
// const TEST_SYMBOL_ID = 'icon-[name]';
test('createSymbolId test', () => {
const options = { symbolId: TEST_SYMBOL_ID } as any;

const normalId = createSymbolId('file.svg', options);
const dirId = createSymbolId('dir/file.svg', options);
const folderId = createSymbolId('folder/dir/file.svg', options);
const specialId = createSymbolId('folder/dir/.file.svg', options);

expect(normalId).toBe('icon-file');
expect(dirId).toBe('icon-dir-file');
expect(folderId).toBe('icon-folder-dir-file');
expect(specialId).toBe('icon-folder-dir-.file');
});

test('createSymbolId Not dir', () => {
const id = createSymbolId('dir/file.svg', { symbolId: 'icon-[name]' } as any);

expect(id).toBe('icon-dir/file');
});
28 changes: 28 additions & 0 deletions tests/discreteDir.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { discreteDir } from '../src/index';
test('discreteDir Not included /', () => {
const { fileName, dirName } = discreteDir('file.svg');

expect(fileName).toBe('file.svg');
expect(dirName).toBe('');
});

test('discreteDir Not included / and include .', () => {
const { fileName, dirName } = discreteDir('file.name.svg');

expect(fileName).toBe('file.name.svg');
expect(dirName).toBe('');
});

test('discreteDir Included /', () => {
const { fileName, dirName } = discreteDir('dir/file.svg');

expect(fileName).toBe('file.svg');
expect(dirName).toBe('dir');
});

test('discreteDir Included multiple /', () => {
const { fileName, dirName } = discreteDir('folder/dir/file.svg');

expect(fileName).toBe('file.svg');
expect(dirName).toBe('folder-dir');
});
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"noUnusedLocals": true,
"esModuleInterop": true,
"outDir": "dist",
"lib": ["dom", "esnext"],
"types": ["vite/client"],
"lib": ["dom", "esnext", "es2015", "es2017", "es2018", "es2019"],
"types": ["vite/client", "jest", "node"],
"sourceMap": false,
"noEmitOnError": true
},
"include": ["./src"],
"exclude": ["**/dist", "**/node_modules", "**/test"]
"include": ["src/**/*", "tests/**/*"],
"exclude": ["**/dist", "**/node_modules"]
}
Loading

0 comments on commit 57a0a44

Please sign in to comment.