Skip to content

Commit 7ad10c2

Browse files
committed
add type check tests for svgPath func
1 parent d030b64 commit 7ad10c2

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

public/blobs.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import {BlobOptions, svgPath} from "./blobs";
2+
3+
const genOptions = (): BlobOptions => ({
4+
extraPoints: Math.floor(10 * Math.random()),
5+
randomness: Math.floor(10 * Math.random()),
6+
seed: Math.random(),
7+
size: 100 + 200 * Math.random(),
8+
});
9+
10+
interface TestCase<T> {
11+
name: string;
12+
edit: (options: T) => void;
13+
error?: RegExp;
14+
}
15+
16+
describe("blobs", () => {
17+
describe("svgPath", () => {
18+
describe("options", () => {
19+
it("should accept generated blobOptions", () => {
20+
expect(() => svgPath(genOptions())).not.toThrow();
21+
});
22+
23+
it("should reject undefined blobOptions", () => {
24+
expect(() => svgPath(undefined as any)).toThrow(/blobOptions.*object.*undefined/g);
25+
});
26+
27+
it("should reject invalid blobOptions", () => {
28+
expect(() => svgPath(null as any)).toThrow(/blobOptions.*object.*null/g);
29+
});
30+
31+
const testCases: Array<TestCase<BlobOptions>> = [
32+
// seed
33+
{
34+
name: "should accept number blobOptions seed",
35+
edit: (blobOptions) => (blobOptions.seed = 123),
36+
},
37+
{
38+
name: "should accept string blobOptions seed",
39+
edit: (blobOptions) => (blobOptions.seed = "test"),
40+
},
41+
{
42+
name: "should reject undefined blobOptions seed",
43+
edit: (blobOptions) => delete blobOptions.seed,
44+
error: /seed.*string.*number.*undefined/g,
45+
},
46+
{
47+
name: "should reject broken blobOptions seed",
48+
edit: (blobOptions) => (blobOptions.seed = NaN),
49+
error: /seed.*string.*number.*NaN/g,
50+
},
51+
// extraPoints
52+
{
53+
name: "should accept valid blobOptions extraPoints",
54+
edit: (blobOptions) => (blobOptions.extraPoints = 4),
55+
},
56+
{
57+
name: "should reject undefined blobOptions extraPoints",
58+
edit: (blobOptions) => delete blobOptions.extraPoints,
59+
error: /blobOptions.*extraPoints.*number.*undefined/g,
60+
},
61+
{
62+
name: "should reject broken blobOptions extraPoints",
63+
edit: (blobOptions) => (blobOptions.extraPoints = NaN),
64+
error: /blobOptions.*extraPoints.*number.*NaN/g,
65+
},
66+
{
67+
name: "should reject negative blobOptions extraPoints",
68+
edit: (blobOptions) => (blobOptions.extraPoints = -2),
69+
error: /blobOptions.*extraPoints.*invalid/g,
70+
},
71+
// randomness
72+
{
73+
name: "should accept valid blobOptions randomness",
74+
edit: (blobOptions) => (blobOptions.randomness = 3),
75+
},
76+
{
77+
name: "should reject undefined blobOptions randomness",
78+
edit: (blobOptions) => delete blobOptions.randomness,
79+
error: /blobOptions.*randomness.*number.*undefined/g,
80+
},
81+
{
82+
name: "should reject broken blobOptions randomness",
83+
edit: (blobOptions) => (blobOptions.randomness = NaN),
84+
error: /blobOptions.*randomness.*number.*NaN/g,
85+
},
86+
{
87+
name: "should reject negative blobOptions randomness",
88+
edit: (blobOptions) => (blobOptions.randomness = -10),
89+
error: /blobOptions.*randomness.*invalid/g,
90+
},
91+
// size
92+
{
93+
name: "should accept valid blobOptions size",
94+
edit: (blobOptions) => (blobOptions.size = 40),
95+
},
96+
{
97+
name: "should reject undefined blobOptions size",
98+
edit: (blobOptions) => delete blobOptions.size,
99+
error: /blobOptions.*size.*number.*undefined/g,
100+
},
101+
{
102+
name: "should reject broken blobOptions size",
103+
edit: (blobOptions) => (blobOptions.size = NaN),
104+
error: /blobOptions.*size.*number.*NaN/g,
105+
},
106+
{
107+
name: "should reject negative blobOptions size",
108+
edit: (blobOptions) => (blobOptions.size = -1),
109+
error: /blobOptions.*size.*invalid/g,
110+
},
111+
];
112+
113+
for (const testCase of testCases) {
114+
it(testCase.name, () => {
115+
const options = genOptions();
116+
testCase.edit(options);
117+
118+
if (testCase.error) {
119+
// Copy regexp because they are stateful.
120+
const pattern = new RegExp(testCase.error);
121+
expect(() => svgPath(options)).toThrow(pattern);
122+
} else {
123+
expect(() => svgPath(options)).not.toThrow();
124+
}
125+
});
126+
}
127+
});
128+
});
129+
});

0 commit comments

Comments
 (0)