1
- import { expect , it } from "vitest" ;
2
- import { convert , resolveFixture , TestOptions } from "./test-helper.ts" ;
3
- import { SVGSFCConvertor } from "../index.ts" ;
4
- import { basename } from "path" ;
5
1
import { readFileSync } from "fs" ;
2
+ import { basename } from "path" ;
3
+ import { expect , it } from "vitest" ;
4
+ import { resolveFixture } from "./test-helper.ts" ;
5
+ import { SVGSFCConvertor , SVGSFCOptions } from "../index.ts" ;
6
6
7
7
const strokeSVG = readFileSync ( resolveFixture ( "stroke.svg" ) , "utf8" ) ;
8
8
9
- it ( "should change attributes" , async ( ) => {
10
- expect ( await convert ( "styles-0.svg?sfc" ) ) . toMatchSnapshot ( ) ;
9
+ function convert ( name : string , options ?: SVGSFCOptions ) {
10
+ const svg = readFileSync ( resolveFixture ( name ) , "utf8" ) ;
11
+ return new SVGSFCConvertor ( options ) . convert ( svg , name ) ;
12
+ }
13
+
14
+ it ( "should remove processing instructions" , ( ) => {
15
+ expect ( convert ( "instruction.svg" ) ) . toMatch ( / ^ < t e m p l a t e > < s v g / ) ;
11
16
} ) ;
12
17
13
- it ( "should change the stroke attribute" , async ( ) => {
14
- expect ( await convert ( "stroke .svg?sfc" ) ) . toMatchSnapshot ( ) ;
18
+ it ( "should remove processing instructions when minify" , ( ) => {
19
+ expect ( convert ( "instruction .svg" , { minify : true } ) ) . toMatch ( / ^ < t e m p l a t e > < s v g / ) ;
15
20
} ) ;
16
21
17
- it ( "should remove processing instructions in dev" , async ( ) => {
18
- expect ( await convert ( "instruction .svg?sfc " , { mode : "development" } ) ) . toMatchSnapshot ( ) ;
22
+ it ( "should minify attributes" , ( ) => {
23
+ expect ( convert ( "styles-0 .svg" , { minify : true } ) ) . toMatchSnapshot ( ) ;
19
24
} ) ;
20
25
21
- it ( "should remove processing instructions in prod" , async ( ) => {
22
- expect ( await convert ( "instruction .svg?sfc" ) ) . toMatchSnapshot ( ) ;
26
+ it ( "should change the stroke attribute" , ( ) => {
27
+ expect ( convert ( "stroke .svg" , { minify : true } ) ) . toMatchSnapshot ( ) ;
23
28
} ) ;
24
29
25
- it ( "should extract styles" , async ( ) => {
26
- const code = await convert ( "styles-0.svg?sfc" ) ;
27
- expect ( code . toString ( ) ) . toMatchSnapshot ( ) ;
30
+ it ( "should extract styles" , ( ) => {
31
+ expect ( convert ( "styles-0.svg" ) )
32
+ . toContain ( "<style scoped>#rect { fill: blue; }.st0 { width: 100px; }</style>" ) ;
28
33
} ) ;
29
34
30
- it ( "should not process SVG if svgo option is false" , async ( ) => {
31
- const config = { svgo : false } as const ;
32
- const code = await convert ( "stroke.svg?sfc" , { config } ) ;
35
+ it ( "should not process SVG if svgo option is false" , ( ) => {
36
+ const code = convert ( "stroke.svg" , { svgo : false } ) ;
33
37
expect ( code ) . toBe ( `<template>${ strokeSVG } </template>` ) ;
34
38
} ) ;
35
39
@@ -54,34 +58,31 @@ it("should support configure SVG plugins", () => {
54
58
} ) ;
55
59
56
60
it ( "should apply only extractCSS plugin" , ( ) => {
57
- const promise = convert ( "styles-0.svg?sfc" , {
58
- config : {
59
- svgo : { plugins : [ "extractCSS" ] } ,
60
- } ,
61
- } ) ;
62
- return expect ( promise ) . resolves . toMatchSnapshot ( ) ;
61
+ const config : SVGSFCOptions = {
62
+ minify : true ,
63
+ svgo : { plugins : [ "extractCSS" ] } ,
64
+ } ;
65
+ return expect ( convert ( "styles-0.svg" , config ) ) . toMatchSnapshot ( ) ;
63
66
} ) ;
64
67
65
- it ( "should change <svg>'s attributes with svgProps" , async ( ) => {
66
- const config : TestOptions = {
67
- config : {
68
- svgProps : {
69
- ":data-foo" : "1" , // Add new
70
- viewBox : "0 0 5 5" , // Replace
71
- } ,
68
+ it ( "should change <svg>'s attributes with svgProps" , ( ) => {
69
+ const config : SVGSFCOptions = {
70
+ minify : true ,
71
+ svgProps : {
72
+ ":data-foo" : "1" , // Add new
73
+ viewBox : "0 0 5 5" , // Replace
72
74
} ,
73
75
} ;
74
- expect ( await convert ( "styles-0.svg?sfc " , config ) ) . toMatchSnapshot ( ) ;
76
+ expect ( convert ( "styles-0.svg" , config ) ) . toMatchSnapshot ( ) ;
75
77
} ) ;
76
78
77
- it ( "should change <svg>'s attributes with custom function" , async ( ) => {
78
- const config : TestOptions = {
79
- config : {
80
- svgProps ( attrs , path , passes ) {
81
- attrs . path = basename ( path ) ;
82
- attrs . passes = passes ;
83
- } ,
79
+ it ( "should change <svg>'s attributes with custom function" , ( ) => {
80
+ const config : SVGSFCOptions = {
81
+ minify : true ,
82
+ svgProps ( attrs , path , passes ) {
83
+ attrs . passes = passes ;
84
+ attrs . path = basename ( path ) ;
84
85
} ,
85
86
} ;
86
- expect ( await convert ( "styles-0.svg?sfc " , config ) ) . toMatchSnapshot ( ) ;
87
+ expect ( convert ( "styles-0.svg" , config ) ) . toMatchSnapshot ( ) ;
87
88
} ) ;
0 commit comments