You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove the large similar objects we've been copy-pasting in all of the integration tests (and some Jest unit tests). Instead use a factory pattern to get fake data.
Proposal
Here is my proposal for API, but feel free to alter this as necessary to get everything we need to keep our tests simple.
makeOne returns a single object to be inserted/used, while makeMany returns multiple in an array (array length = first arg value).
The { prop: val } object is where you specify override properties you specifically want. For any properties not there, or if you omit that object, you'll get back the object created by the simpl-schema-mockdoc package.
If simpl-schema-mockdoc does not meet our needs or is buggy, we can copy the relevant code into this data-factory package and update it to work for us.
Work
Create the package
Add the package to Reaction, pass in all the schemas.
Change all of the integration tests in the /tests folder to use this to build their mock data objects.
The text was updated successfully, but these errors were encountered:
Here is more specifically how I envision this looking. This is untested but should be a starting point to see how it works. We will definitely have to make this more robust over time (allow overriding nested props, maybe some helpers for relationships).
import{getMockDoc}from"simpl-schema-mockdoc";exportconstFactory={};/** * @name createFactoryForSchema * @function * @summary Creates Factory[propName] for building fake documents with the given schema. * @param {String} propName The property name to add to the `Factory` object. This should match the * schema variable's name. * @param {SimpleSchema} schema A SimpleSchema instance */exportfunctioncreateFactoryForSchema(propName,schema){if(Factory.hasOwnProperty(propName)){thrownewError(`Factory already has a "${propName}" property`);}Factory[propName]={makeOne(props){constdoc=getMockDoc(schema,'mock',true);// Most likely it is fine to mutate doc, and will be faster. If this causes problems,// can change this to return { ...doc, ...props }Object.assign(doc,props);returndoc;},makeMany(length,props){returnArray.from({ length }).map(()=>this.makeOne(props));}};}
Goal
Remove the large similar objects we've been copy-pasting in all of the integration tests (and some Jest unit tests). Instead use a factory pattern to get fake data.
Proposal
Here is my proposal for API, but feel free to alter this as necessary to get everything we need to keep our tests simple.
Make it an NPM package (maybe
@reactioncommerce/data-factory
?)Depend on https://www.npmjs.com/package/simpl-schema-mockdoc
In the reaction files where schemas are defined, do something like:
This will add
Shop
property to aFactory
object that can be imported in tests, in devtools, anywhere.makeOne
returns a single object to be inserted/used, whilemakeMany
returns multiple in an array (array length = first arg value).The
{ prop: val }
object is where you specify override properties you specifically want. For any properties not there, or if you omit that object, you'll get back the object created by thesimpl-schema-mockdoc
package.If
simpl-schema-mockdoc
does not meet our needs or is buggy, we can copy the relevant code into this data-factory package and update it to work for us.Work
/tests
folder to use this to build their mock data objects.The text was updated successfully, but these errors were encountered: