Skip to content

Commit b8c287c

Browse files
committed
add svgOptions checks + tests
1 parent 18dca11 commit b8c287c

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
</p>
44

55
<p align="center">
6-
<a href="https://www.npmjs.com/package/blobs">
7-
<img src="https://img.shields.io/npm/v/blobs.svg">
8-
</a>
9-
<a href="https://github.com/g-harel/blobs/actions?query=workflow%3Aon-push">
10-
<img src="https://img.shields.io/github/workflow/status/g-harel/blobs/on-push">
11-
</a>
6+
<a href="https://www.npmjs.com/package/blobs"><!--
7+
--><img src="https://img.shields.io/npm/v/blobs.svg"><!--
8+
--></a>
9+
<a href="https://github.com/g-harel/blobs/actions?query=workflow%3Aon-push"><!--
10+
--><img src="https://img.shields.io/github/workflow/status/g-harel/blobs/on-push"><!--
11+
--></a>
1212
</p>
1313

1414
<p align="center">
@@ -19,8 +19,11 @@
1919

2020
## Install
2121

22+
```bash
23+
$ npm install blobs
24+
```
25+
2226
```ts
23-
// $ npm install blobs
2427
import * as blobs2 from "blobs/v2";
2528
```
2629

index.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@
6666
if (!mobileElem) return;
6767
console.log(window.innerWidth);
6868

69-
mobileElem.innerHTML = blobs2.svg({
70-
seed: Math.random(),
71-
extraPoints: 4,
72-
randomness: 6,
73-
size: Math.min(600, window.innerWidth - 100),
74-
});
69+
mobileElem.innerHTML = blobs2.svg(
70+
{
71+
seed: Math.random(),
72+
extraPoints: 4,
73+
randomness: 6,
74+
size: Math.min(600, window.innerWidth - 100),
75+
},
76+
{
77+
stroke: "black",
78+
strokeWidth: -10,
79+
},
80+
);
7581
}
7682

7783
refresh();

internal/check.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ export const checkCanvasOptions = (canvasOptions: any) => {
4545

4646
export const checkSvgOptions = (svgOptions: any) => {
4747
typeCheck(`svgOptions`, svgOptions, ["object", "undefined"]);
48+
if (svgOptions) {
49+
const {fill, stroke, strokeWidth} = svgOptions;
50+
typeCheck(`svgOptions.fill`, fill, ["string", "undefined"]);
51+
typeCheck(`svgOptions.stroke`, stroke, ["string", "undefined"]);
52+
typeCheck(`svgOptions.strokeWidth`, strokeWidth, ["number", "undefined"]);
53+
}
4854
};

public/blobs.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,54 @@ describe("blobs", () => {
219219
/svgOptions.*object.*null/g,
220220
);
221221
});
222+
223+
runSuite<SvgOptions>({
224+
functionBeingTested: (svgOptions) => svg(genBlobOptions(), svgOptions),
225+
optionsGenerator: genSvgOptions,
226+
})([
227+
// fill
228+
{
229+
name: "should accept valid svgOptions fill",
230+
edit: (svgOptions) => (svgOptions.fill = "red"),
231+
},
232+
{
233+
name: "should accept undefined svgOptions fill",
234+
edit: (svgOptions) => delete svgOptions?.fill,
235+
},
236+
{
237+
name: "should reject broken svgOptions fill",
238+
edit: (svgOptions) => (svgOptions.fill = null as any),
239+
error: /svgOptions.*fill.*string.*null/g,
240+
},
241+
// stroke
242+
{
243+
name: "should accept valid svgOptions stroke",
244+
edit: (svgOptions) => (svgOptions.stroke = "red"),
245+
},
246+
{
247+
name: "should accept undefined svgOptions stroke",
248+
edit: (svgOptions) => delete svgOptions?.stroke,
249+
},
250+
{
251+
name: "should reject broken svgOptions stroke",
252+
edit: (svgOptions) => (svgOptions.stroke = null as any),
253+
error: /svgOptions.*stroke.*string.*null/g,
254+
},
255+
// strokeWidth
256+
{
257+
name: "should accept valid svgOptions strokeWidth",
258+
edit: (svgOptions) => (svgOptions.strokeWidth = 222),
259+
},
260+
{
261+
name: "should accept undefined svgOptions strokeWidth",
262+
edit: (svgOptions) => delete svgOptions?.strokeWidth,
263+
},
264+
{
265+
name: "should reject broken svgOptions strokeWidth",
266+
edit: (svgOptions) => (svgOptions.strokeWidth = NaN),
267+
error: /svgOptions.*strokeWidth.*number.*NaN/g,
268+
},
269+
]);
222270
});
223271
});
224272

0 commit comments

Comments
 (0)