Factory util that creates mock data fixtures based on simpl-schema definitions
npm install @reactioncommerce/data-factoryReaction Data Factory creates a simple fixture factory from SimpleSchema definitions for testing and seeding applications.
``sh`
npm install @reactioncommerce/data-factory
Adding schemas to the Factory object.
` js
import SimpleSchema from 'simpl-schema';
import { createFactoryForSchema } from "@reactioncommerce/data-factory";
const Example = new SimpleSchema({
strProp: String,
boolProp: Boolean,
});
createFactoryForSchema("Example", Example);
`
Creating mock data structures.
` js
import { Factory } from "@reactioncommerce/data-factory";
const mockExample = Factory.Example.makeOne();
// mockExample output
// { _id: "e02993ea96d7", strProp: "mockStrProp", boolProp: true }
const mockExamples = Factory.Example.makeMany(2);
// mockExamples output
// [{ _id: "e02993ea96d7", strProp: "mockStrProp", boolProp: true }, { _id: "3ff4e0634ecc", strProp: "mockStrProp", boolProp: false }]
`
Creating mock data with custom property values.
` js`
const mockExample = Factory.Example.makeOne({ strProp: "Custom Value" });
// mockExample output
// { _id: "e02993ea96d7", strProp: "Custom Value", boolProp: true }
Creating mock data with custom property function.
` js`
const mockExamples = Factory.Example.makeMany(3, { _id: (i) => (i + 100).toString() });
// mockExamples output
// [{ _id: "100", strProp: "mockStrProp", boolProp: true }, { _id: "101", strProp: "mockStrProp", boolProp: false }], { _id: "102", strProp: "mockStrProp", boolProp: false }]
When you call makeOne or makeMany, a full document (including all nested schemas recursively) is automatically built based on the schema. The value of each node is determined like this:
- If mockValue is found in the schema definition, use that value. mockValue is not part of normal SimpleSchema but is added by and checked by this package.defaultValue
- If is found in the schema definition, use that value.autoValue
- If is found in the schema definition, use it to get a value.allowedValues
- If is found in the schema definition, use a random item from the array as the value.faker.js
- If all else fails, use to generate a random value of the type specified in the schema definition. For Number fields, the min and max values are respected when generating a random number. For String fields, the regEx value will be used if it matches one of the SimpleSchema.RegEx.*
Signed-off-by: Jane Doe
`You can sign-off your commit automatically with Git by using
git commit -s if you have your user.name and user.email` set as part of your Git configuration.We ask that you use your real full name (please no anonymous contributions or pseudonyms) and a real email address. By signing-off your commit you are certifying that you have the right to submit it under the Apache License, Version 2.0.
We use the Probot DCO GitHub app to check for DCO sign-offs of every commit.
If you forget to sign-off your commits, the DCO bot will remind you and give you detailed instructions for how to amend your commits to add a signature.