You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
813 B
44 lines
813 B
'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 };
|