Skip to content

Commit

Permalink
Incorrect initial value for cleaning (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart authored Jul 24, 2024
1 parent 7921b2c commit 0ce5e52
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { depth } from '../../../../utils/array';
export function cleaningBehaviour(
store: LunaticVariablesStore,
cleaning: LunaticSource['cleaning'],
// Value used as default when cleaning a variable
initialValues: Record<string, unknown> = {}
) {
if (!cleaning) {
Expand Down
50 changes: 50 additions & 0 deletions src/use-lunatic/commons/variables/lunatic-variables-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,54 @@ describe('lunatic-variables-store', () => {
expect(variables.get('PRENOM_MISSING', [1])).toEqual('DK');
});
});

describe('makeFromSource', () => {
it('should handle initial data correctly', () => {
const store = LunaticVariablesStore.makeFromSource(
{
components: [],
variables: [
{
name: 'PRENOM',
values: {
EDITED: null,
FORCED: null,
INPUTTED: null,
PREVIOUS: null,
COLLECTED: 'John',
},
variableType: 'COLLECTED',
},
{
name: 'NOM',
values: {
EDITED: null,
FORCED: null,
INPUTTED: null,
PREVIOUS: null,
COLLECTED: '',
},
variableType: 'COLLECTED',
},
],
cleaning: {
NOM: {
PRENOM: 'false',
},
},
},
{
COLLECTED: {
PRENOM: {
COLLECTED: 'Jane',
},
},
},
{ current: () => {} }
);
expect(store.get('PRENOM')).toEqual('Jane');
store.set('NOM', 'Doe');
expect(store.get('PRENOM')).toEqual('John');
});
});
});
16 changes: 9 additions & 7 deletions src/use-lunatic/commons/variables/lunatic-variables-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ export class LunaticVariablesStore {
if (!source.variables) {
return store;
}
const initialValues = Object.fromEntries(
source.variables.map((variable) => [
variable.name,
getInitialVariableValue(variable, data),
])
);
// Source data (picked from "variables" in the source.json)s
const sourceValues: Record<string, unknown> = {};
// Starting data for the form (merged with data.json or injected data)
const initialValues: Record<string, unknown> = {};
for (const variable of source.variables) {
sourceValues[variable.name] = getInitialVariableValue(variable);
initialValues[variable.name] = getInitialVariableValue(variable, data);
}
const getIterationDepth = (name: string) => {
if (name === 'xAxis') return 0;
if (name === 'yAxis') return 1;
Expand All @@ -83,7 +85,7 @@ export class LunaticVariablesStore {
}
}
store.on('change', (e) => changeHandler?.current?.(e.detail));
cleaningBehaviour(store, source.cleaning, initialValues);
cleaningBehaviour(store, source.cleaning, sourceValues);
resizingBehaviour(store, source.resizing);
missingBehaviour(store, source.missingBlock);
return store;
Expand Down

0 comments on commit 0ce5e52

Please sign in to comment.