From 3cc7af3bd921c4f203495f3c094c796594a03600 Mon Sep 17 00:00:00 2001 From: Balaj Marius Date: Tue, 29 Oct 2024 15:04:29 +0200 Subject: [PATCH] chore: added tests --- .github/workflows/ci.yaml | 12 +- .../unit/__snapshots__/svg2jsx.test.ts.snap | 19 +++ __test__/unit/svg2jsx.test.ts | 111 ++++++++++++++++++ biome.json | 2 +- jest.config.ts | 5 +- package.json | 6 +- src/pages/api/index.ts | 2 +- yarn.lock | 5 - 8 files changed, 147 insertions(+), 15 deletions(-) create mode 100644 __test__/unit/__snapshots__/svg2jsx.test.ts.snap create mode 100644 __test__/unit/svg2jsx.test.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dba2c89..4354e02 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,10 +7,13 @@ jobs: ci: runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: - sparse-checkout: src + node-version: "20.18.0" + + - name: Dependencies + run: yarn install --frozen-lockfile - name: Setup Biome CLI uses: biomejs/setup-biome@v2 @@ -19,3 +22,6 @@ jobs: - name: Run Biome run: biome ci . + + - name: Run Tests + run: yarn test diff --git a/__test__/unit/__snapshots__/svg2jsx.test.ts.snap b/__test__/unit/__snapshots__/svg2jsx.test.ts.snap new file mode 100644 index 0000000..5eb1117 --- /dev/null +++ b/__test__/unit/__snapshots__/svg2jsx.test.ts.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`svg2jsx tests clean attributes 1`] = `""`; + +exports[`svg2jsx tests convert attributes 1`] = `""`; + +exports[`svg2jsx tests convert inline styles to style objects 1`] = `""`; + +exports[`svg2jsx tests convert nested nodes 1`] = `""`; + +exports[`svg2jsx tests convert text elements 1`] = `"svg2jsx"`; + +exports[`svg2jsx tests keep attribute values 1`] = `""`; + +exports[`svg2jsx tests keep dimensions attributes 1`] = `""`; + +exports[`svg2jsx tests quote string style values 1`] = `""`; + +exports[`svg2jsx tests remove unnecessary nodes 1`] = `""`; diff --git a/__test__/unit/svg2jsx.test.ts b/__test__/unit/svg2jsx.test.ts new file mode 100644 index 0000000..482cdf4 --- /dev/null +++ b/__test__/unit/svg2jsx.test.ts @@ -0,0 +1,111 @@ +import { describe, expect, test } from "@jest/globals"; + +import { svg2jsx } from "../../src/pages/api"; + +describe("svg2jsx tests", () => { + test("clean attributes", () => { + const jsx = svg2jsx('', {}); + + expect(jsx).toEqual(expect.not.stringMatching(/strok/)); + expect(jsx).toMatchSnapshot(); + }); + + test("remove unnecessary nodes", () => { + const jsx = svg2jsx("", {}); + + expect(jsx).toEqual(expect.stringMatching(/<\/svg>/)); + expect(jsx).toMatchSnapshot(); + }); + + test("convert attributes", () => { + const jsx = svg2jsx('', {}); + + expect(jsx).toEqual(expect.stringMatching(/className=/)); + expect(jsx).toEqual(expect.not.stringMatching(/class=/)); + expect(jsx).toEqual(expect.stringMatching(/fillRule=/)); + expect(jsx).toEqual(expect.not.stringMatching(/fill-rule=/)); + expect(jsx).toEqual(expect.stringMatching(/data-name=/)); + expect(jsx).toEqual(expect.not.stringMatching(/dataName=/)); + expect(jsx).toMatchSnapshot(); + }); + + test("convert inline styles to style objects", () => { + const jsx = svg2jsx('', {}); + + expect(jsx).toEqual(expect.stringMatching(/marginLeft: "20px"/)); + expect(jsx).toEqual(expect.stringMatching(/paddingTop: "20px"/)); + expect(jsx).toMatchSnapshot(); + }); + + test("quote string style values", () => { + const jsx = svg2jsx( + ` + + `, + {} + ); + + expect(jsx).toEqual(expect.stringMatching(/marginLeft: "auto"/)); + expect(jsx).toMatchSnapshot(); + }); + + test("convert nested nodes", () => { + const jsx = svg2jsx( + ` + + + + + + + `, + {} + ); + + expect(jsx).toEqual(expect.not.stringMatching(/>\s*>/gm)); + expect(jsx).toEqual(expect.stringMatching(/ { + const jsx = svg2jsx( + ` + svg2jsx + `, + {} + ); + + expect(jsx.match(/svg2jsx<\/text>/g)).toHaveLength(1); + expect(jsx).toMatchSnapshot(); + }); + + test("keep dimensions attributes", () => { + const jsx = svg2jsx( + ` + + `, + {} + ); + + expect(jsx.match(/width="125"/g)).toHaveLength(1); + expect(jsx.match(/height="125"/g)).toHaveLength(1); + expect(jsx).toMatchSnapshot(); + }); + + test("keep attribute values", () => { + const jsx = svg2jsx( + ` + + + `, + {} + ); + + expect(jsx.match(/id="svg-test"/g)).toHaveLength(1); + expect(jsx.match(/href="#svg-test"/g)).toHaveLength(1); + expect(jsx).toMatchSnapshot(); + }); +}); diff --git a/biome.json b/biome.json index cedd8a5..9976453 100644 --- a/biome.json +++ b/biome.json @@ -7,7 +7,7 @@ }, "files": { "ignoreUnknown": false, - "ignore": [] + "ignore": ["__test__/unit/**/*.test.ts"] }, "formatter": { "enabled": true, diff --git a/jest.config.ts b/jest.config.ts index 38e7ecf..45841ac 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -1,4 +1,4 @@ -import type { Config } from "jest"; +import { type Config } from "jest"; import jest from "next/jest.js"; @@ -8,10 +8,11 @@ const createJestConfig = jest({ const config: Config = { coverageProvider: "v8", - testEnvironment: "jsdom", + testEnvironment: "node", moduleNameMapper: { "^@/(.*)$": "/$1", }, + testPathIgnorePatterns: ["/node_modules/", "/dist/"], }; export default createJestConfig(config); diff --git a/package.json b/package.json index f419bb0..f63b2ab 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "svg2jsx", - "version": "0.1.0", + "version": "3.0.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", - "test": "jest --config jest.config.ts", + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --config jest.config.ts", "lint": "biome ci ./src/**/*", "format": "biome format ./src/**/* --write", "prepare": "husky" @@ -57,6 +57,6 @@ "typescript": "^5.6.3" }, "engines": { - "node": ">=19.0.0" + "node": ">=18.20.0" } } diff --git a/src/pages/api/index.ts b/src/pages/api/index.ts index b687883..cd5c673 100644 --- a/src/pages/api/index.ts +++ b/src/pages/api/index.ts @@ -16,7 +16,7 @@ export type Data = { }; export const svg2jsx = flow( - (svg: string, options: Options) => clean(svg, options.cleanupIds), + (svg: string, options: Options) => clean(svg, options?.cleanupIds), (svg: string) => parse(svg), (node: Node) => transform(node), (node: Node) => stringify(node), diff --git a/yarn.lock b/yarn.lock index 004c338..1717237 100644 --- a/yarn.lock +++ b/yarn.lock @@ -368,11 +368,6 @@ dependencies: "@jridgewell/trace-mapping" "0.3.9" -"@custom-react-hooks/use-clipboard@^1.5.1": - version "1.5.1" - resolved "https://registry.yarnpkg.com/@custom-react-hooks/use-clipboard/-/use-clipboard-1.5.1.tgz#afa2b013ab4b73ec1062af2d490f98d979c9b828" - integrity sha512-rXwbVk8GtqZHTJsEWl1ubkE/uEiZR1OpvErdCpvctKVbwwAulhYwfvrU9CvoVywcqbVoj8j+gHYcYXNCyUh4Xw== - "@emnapi/runtime@^1.2.0": version "1.3.1" resolved "https://registry.yarnpkg.com/@emnapi/runtime/-/runtime-1.3.1.tgz#0fcaa575afc31f455fd33534c19381cfce6c6f60"