-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added toMatchMultipleSvgSnapshot (#2)
* toMatchMultipleSvgSnapshots and test
- Loading branch information
1 parent
bfbf180
commit 22e8380
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { expect, test, beforeAll, afterAll } from "bun:test" | ||
import * as fs from "node:fs" | ||
import * as path from "node:path" | ||
import "../index" | ||
|
||
const testSvgs = [ | ||
`<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> | ||
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> | ||
</svg>`, | ||
`<svg width="400" height="110" xmlns="http://www.w3.org/2000/svg"> | ||
<defs> | ||
<pattern id="patt1" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse"> | ||
<circle cx="10" cy="10" r="10" fill="red" /> | ||
</pattern> | ||
</defs> | ||
<rect width="200" height="100" x="0" y="0" stroke="black" fill="url(#patt1)" /> | ||
</svg> | ||
`, | ||
`<svg height="220" width="500" xmlns="http://www.w3.org/2000/svg"> | ||
<polygon points="100,10 150,190 50,190" style="fill:lime;stroke:purple;stroke-width:3" /> | ||
</svg>`, | ||
] | ||
|
||
const svgNames: string[] = [] | ||
for (let i = 0; i < testSvgs.length; i++) svgNames.push(`test${i + 1}`) | ||
|
||
const snapshotDir = path.join(__dirname, "__snapshots__") | ||
const snapshotPaths = svgNames.map((svgName) => | ||
path.join(snapshotDir, `${svgName}.snap.svg`), | ||
) | ||
|
||
beforeAll(() => { | ||
if (!fs.existsSync(snapshotDir)) { | ||
fs.mkdirSync(snapshotDir, { recursive: true }) | ||
} | ||
}) | ||
|
||
afterAll(() => { | ||
for (const snapshotPath of snapshotPaths) | ||
if (fs.existsSync(snapshotPath)) { | ||
fs.unlinkSync(snapshotPath) | ||
} | ||
if (fs.existsSync(snapshotDir)) { | ||
fs.rmdirSync(snapshotDir, { recursive: true }) | ||
} | ||
}) | ||
|
||
test("toMatchMultipleSvgSnapshots creates and matches snapshots", async () => { | ||
// First run: create snapshot | ||
await expect(testSvgs).toMatchMultipleSvgSnapshots(import.meta.path, svgNames) | ||
|
||
// Verify snapshot was created | ||
for (const snapshotPath of snapshotPaths) | ||
expect(fs.existsSync(snapshotPath)).toBe(true) | ||
|
||
// Second run: match existing snapshot | ||
await expect(testSvgs).toMatchMultipleSvgSnapshots(import.meta.path, svgNames) | ||
}) |