| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  | const { get } = require('lodash/fp'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | const modelsUtils = require('../models'); | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  | const { sanitize } = require('../../../packages/core/utils'); | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | const actionRegistry = require('./action-registry'); | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  | const { createContext } = require('./context'); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const createTestBuilder = (options = {}) => { | 
					
						
							|  |  |  |   const { initialState } = options; | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |   const ctx = createContext(initialState); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return { | 
					
						
							|  |  |  |     get models() { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       return ctx.state.models; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     get fixtures() { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       return ctx.state.fixtures; | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  |     async sanitizedFixtures(strapi) { | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  |       const { fixtures } = this; | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       for (const key of Object.keys(fixtures)) { | 
					
						
							|  |  |  |         fixtures[key] = await this.sanitizedFixturesFor(key, strapi); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return fixtures; | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sanitizedFixturesFor(modelName, strapi) { | 
					
						
							| 
									
										
										
										
											2022-09-01 17:24:34 +02:00
										 |  |  |       const model = strapi.getModel(modelsUtils.toContentTypeUID(modelName)); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |       const fixtures = this.fixturesFor(modelName); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  |       return sanitize.contentAPI.output(fixtures, model); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fixturesFor(modelName) { | 
					
						
							|  |  |  |       return this.fixtures[modelName]; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |     addAction(code, ...params) { | 
					
						
							|  |  |  |       const actionCreator = get(code, actionRegistry); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ctx.addAction(actionCreator(...params)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |       return this; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |     addContentType(contentType) { | 
					
						
							|  |  |  |       return this.addAction('contentType.create', contentType); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     addContentTypes(contentTypes, { batch = true } = {}) { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       return this.addAction( | 
					
						
							|  |  |  |         batch ? 'contentType.createBatch' : 'contentType.createMany', | 
					
						
							|  |  |  |         contentTypes | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     addComponent(component) { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       return this.addAction('component.create', component); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  |     addFixtures(model, entries) { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       return this.addAction('fixtures.create', model, entries, () => this.fixtures); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async build() { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       for (const action of ctx.state.actions) { | 
					
						
							|  |  |  |         await action.build(ctx); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return this; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  |     async cleanup(options = {}) { | 
					
						
							|  |  |  |       const { enableTestDataAutoCleanup = true } = options; | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       const { models, actions } = ctx.state; | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (enableTestDataAutoCleanup) { | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |         for (const model of models.reverse()) { | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  |           await modelsUtils.cleanupModel(model.uid); | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  |       for (const action of actions.reverse()) { | 
					
						
							|  |  |  |         await action.cleanup(ctx); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-01-05 14:24:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       ctx.resetState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return this; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | module.exports = { createTestBuilder }; |