mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 09:56:44 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			44 lines
		
	
	
		
			811 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			811 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const { merge } = require('lodash/fp');
 | |
| 
 | |
| const getDefaultState = () => ({ actions: [], models: [], fixtures: {} });
 | |
| 
 | |
| const createContext = initialState => {
 | |
|   let state;
 | |
| 
 | |
|   const contextApi = {
 | |
|     get state() {
 | |
|       return state;
 | |
|     },
 | |
| 
 | |
|     addAction(action) {
 | |
|       state.actions.push(action);
 | |
|       return this;
 | |
|     },
 | |
| 
 | |
|     addModel(model) {
 | |
|       state.models.push(model);
 | |
|       return this;
 | |
|     },
 | |
| 
 | |
|     addFixtures(modelName, entries) {
 | |
|       state.fixtures = merge(state.fixtures, { [modelName]: entries });
 | |
|       return this;
 | |
|     },
 | |
| 
 | |
|     resetState() {
 | |
|       return this.setState({ ...getDefaultState(), ...initialState });
 | |
|     },
 | |
| 
 | |
|     setState(newState) {
 | |
|       state = newState;
 | |
|       return this;
 | |
|     },
 | |
|   };
 | |
| 
 | |
|   return contextApi.resetState();
 | |
| };
 | |
| 
 | |
| module.exports = { createContext };
 | 
