...
 
Commits (5)
{
"name": "@calderajs/cf-editor-state",
"version": "0.2.1",
"version": "0.3.0",
"private": false,
"license": "MIT",
"main": "dist/index.js",
......
......@@ -84,4 +84,18 @@ describe('conditonalsState', () => {
});
expect(state.getConditional(conditional.id)?.config).toEqual(config);
});
it('Updates state immutably', () => {
const state = conditonalState();
const conditionals = state.getAllConditionals();
state.addConditional(conditional);
expect(conditionals === state.getAllConditionals()).toEqual(false);
state.updateConditional({
...conditional,
type: 'hide',
});
expect(conditionals === state.getAllConditionals()).toEqual(false);
state.removeConditional(conditional.id);
expect(conditionals === state.getAllConditionals()).toEqual(false);
});
});
......@@ -58,7 +58,7 @@ export default function conditionalState() {
/**
* The collection of conditionals
*/
const conditionals: conditionals = [];
let conditionals: conditionals = [];
/**
* Find array index of conditional in collection
......@@ -81,7 +81,7 @@ export default function conditionalState() {
addConditional(conditional: conditional): boolean {
const index = findContionalIndex(conditional.id);
if (undefined === index || 0 > index) {
conditionals.push(conditional);
conditionals = [...conditionals, conditional];
return true;
}
return false;
......@@ -95,7 +95,10 @@ export default function conditionalState() {
if (undefined === index || 0 > index) {
return false;
}
conditionals.splice(index, 1);
conditionals = [
...conditionals.slice(0, index),
...conditionals.slice(index + 1),
];
return true;
},
/**
......@@ -107,7 +110,11 @@ export default function conditionalState() {
if (undefined === index || 0 > index) {
return false;
}
conditionals[index] = conditional;
conditionals = [
...conditionals.slice(0, index),
conditional,
...conditionals.slice(index + 1),
];
return true;
},
/**
......
......@@ -145,4 +145,19 @@ describe('fieldsState', () => {
expect(callback).toBeCalledTimes(1);
expect(callback).toBeCalledWith({ beforeUpdate: field, fieldId: field.ID });
});
it('Updates state immutably', () => {
const state = fieldState();
const fields = state.getAllFields();
state.addField(field);
expect(state.getAllFields() === fields).toEqual(false);
state.updateField({
...field,
label: 'Smells',
});
expect(state.getAllFields() === fields).toEqual(false);
state.removeField(field.ID);
expect(state.getAllFields() === fields).toEqual(false);
});
});
......@@ -50,7 +50,7 @@ export default function(initialFields?: fields) {
/**
* The collection of fields
*/
const fields: fields = initialFields ? initialFields : [];
let fields: fields = initialFields ? initialFields : [];
/**
* Find the index of field in array
......@@ -96,7 +96,7 @@ export default function(initialFields?: fields) {
if (index >= 0) {
return false;
}
fields.push(field);
fields = [...fields, field];
feildEvents.emit('addField', field.ID);
return true;
},
......@@ -122,7 +122,7 @@ export default function(initialFields?: fields) {
return false;
}
const beforeUpdate = fields[index];
fields[index] = field;
fields = [...fields.slice(0, index), field, ...fields.slice(index + 1)];
feildEvents.emit('updateField', { fieldId: field.ID, beforeUpdate });
return true;
},
......@@ -144,7 +144,7 @@ export default function(initialFields?: fields) {
return false;
}
const _field = fields[index];
fields.splice(index, 1);
fields = [...fields.slice(0, index), ...fields.slice(index + 1)];
feildEvents.emit('removeField', _field);
return true;
......