strapi/test/helpers/builder/context.js

44 lines
811 B
JavaScript
Raw Normal View History

2021-01-05 14:24:53 +01:00
'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 };