-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
192 lines (169 loc) · 4.69 KB
/
example.ts
File metadata and controls
192 lines (169 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/** Example usage of the simule package. */
import {
make,
arrayOf,
isOneOf,
makeDynamic,
arrayOf as arrayOfDynamic,
isOneOf as isOneOfDynamic,
defineType,
} from "simule";
// Example 1: Node.js version (requires ts-morph, works with TypeScript files)
type Product = {
id: string;
title: string | null;
price: number;
tags?: TagItem[];
inStock: boolean;
};
type TagItem = { name: string; value: number };
// This works in Node.js environments with tsconfig.json
// const fixture = make<Product>("Product", {
// overrides: {
// tags: arrayOf(() => make<TagItem>("TagItem"), { min: 5, max: 100 }),
// title: isOneOf(["Title 1", "Title 2"]),
// price: 9.99,
// },
// });
// Example 2: Browser version (works anywhere, no ts-morph dependency)
interface BrowserProduct {
id: string;
title: string | null;
price: number;
tags?: BrowserTagItem[];
inStock: boolean;
}
interface BrowserTagItem {
name: string;
value: number;
isActive: boolean;
}
// Solution 1: Use defineType helper (RECOMMENDED for complex types)
const ProductTemplate1 = defineType<BrowserProduct>({
id: "", // Will generate UUID
title: "", // Will generate random string
price: 0, // Will generate random number
tags: arrayOf(() => ({ name: "", value: 0, isActive: false }), {
min: 3,
max: 8,
}),
inStock: false, // Will generate random boolean
});
// Solution 2: Provide multiple sample items
const ProductTemplate2: BrowserProduct = {
id: "",
title: "",
price: 0,
tags: [
{ name: "", value: 0, isActive: false },
{ name: "", value: 0, isActive: false },
{ name: "", value: 0, isActive: false },
], // Will generate 3-8 TagItems
inStock: false,
};
// Solution 3: Use arrayOf in overrides
const ProductTemplate3: BrowserProduct = {
id: "",
title: "",
price: 0,
tags: [], // Empty array
inStock: false,
};
// Generate fixtures using different approaches
const browserFixture1 = makeDynamic(ProductTemplate1, {
overrides: {
title: isOneOfDynamic(["Title 1", "Title 2"]),
price: 9.99,
},
});
const browserFixture2 = makeDynamic(ProductTemplate2, {
overrides: {
title: isOneOfDynamic(["Title 1", "Title 2"]),
price: 9.99,
},
});
const browserFixture3 = makeDynamic(ProductTemplate3, {
overrides: {
title: isOneOfDynamic(["Title 1", "Title 2"]),
price: 9.99,
tags: arrayOfDynamic(() => ({ name: "", value: 0, isActive: false }), {
min: 5,
max: 10,
}),
},
});
console.log("Browser Product Fixture 1 (defineType):", browserFixture1);
console.log("Browser Product Fixture 2 (multiple samples):", browserFixture2);
console.log("Browser Product Fixture 3 (overrides):", browserFixture3);
// Example 3: Different types work automatically
interface User {
id: string;
email: string;
profile: {
firstName: string;
lastName: string;
age: number;
preferences: string[];
};
isVerified: boolean;
lastLogin: Date | null;
}
const UserTemplate = defineType<User>({
id: "",
email: "",
profile: {
firstName: "",
lastName: "",
age: 0,
preferences: arrayOf(() => "", { min: 2, max: 5 }), // Array of strings
},
isVerified: false,
lastLogin: null,
});
const userFixture = makeDynamic(UserTemplate, {
overrides: {
email: "user@example.com",
age: 25,
},
});
console.log("User Fixture:", userFixture);
// Example 4: Built-in types work too
const StringArrayTemplate = [""]; // Will generate array of random strings
const NumberArrayTemplate = [0]; // Will generate array of random numbers
const BooleanArrayTemplate = [false]; // Will generate array of random booleans
const stringArray = makeDynamic(StringArrayTemplate);
const numberArray = makeDynamic(NumberArrayTemplate);
const booleanArray = makeDynamic(BooleanArrayTemplate);
console.log("String Array:", stringArray);
console.log("Number Array:", numberArray);
console.log("Boolean Array:", booleanArray);
// Example 5: Complex nested arrays
interface Category {
id: string;
name: string;
products: BrowserProduct[];
subcategories?: Category[];
}
const CategoryTemplate: Category = {
id: "",
name: "",
products: [], // Will be overridden
subcategories: [], // Will be overridden
};
const categoryFixture = makeDynamic(CategoryTemplate, {
overrides: {
products: arrayOfDynamic(() => ProductTemplate1, { min: 2, max: 5 }),
subcategories: arrayOfDynamic(
() => ({
id: "",
name: "",
products: arrayOfDynamic(() => ProductTemplate1, { min: 1, max: 3 }),
subcategories: [],
}),
{ min: 0, max: 3 }
),
},
});
console.log("Complex Category Fixture:", categoryFixture);
// The beauty: NO HARDCODING! Works with ANY type you provide!
// And you have full control over array sizes!